Last active
January 8, 2021 01:10
-
-
Save luislobo14rap/6f1d3bc1802b7586f57ac02cd9421aab to your computer and use it in GitHub Desktop.
addAudioAndVideoShortcuts.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// addAudioAndVideoShortcuts.js v1.1 | |
function addAudioAndVideoShortcuts(videos) { | |
if (typeof videos == 'undefined') { | |
// if defined | |
videos = document.querySelectorAll('audio:not(.key-shortcuts), video:not(.key-shortcuts)'); | |
} else if (typeof videos.length == 'undefined') { | |
// if node | |
videos = [videos]; | |
}; | |
videos.forEach(function(element) { | |
document.body.addEventListener('keydown', function(event) { | |
if (event.code == 'ArrowLeft' || event.keyCode == 37) { | |
element.currentTime = element.currentTime - 5; | |
}; | |
}, false); | |
document.body.addEventListener('keydown', function(event) { | |
if (event.code == 'ArrowRight' || event.keyCode == 39) { | |
element.currentTime = element.currentTime + 5; | |
}; | |
}, false); | |
document.body.addEventListener('keydown', function(event) { | |
if (event.code == 'Space' || event.keyCode == 32) { | |
if( element.paused ){ | |
element.play(); | |
}else{ | |
element.pause(); | |
}; | |
}; | |
}, false); | |
element.classList.add('key-shortcuts'); | |
}); | |
setTimeout(function() { | |
addAudioAndVideoShortcuts(); | |
}, 1000); | |
}; | |
addAudioAndVideoShortcuts(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment