Created
December 14, 2017 12:23
-
-
Save fcannizzaro/0498b27f0bb403e1ad0d4b83b3138a23 to your computer and use it in GitHub Desktop.
use media key to play/pause HTML5 video
This file contains 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
// ==UserScript== | |
// @name Media Keyboard | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author Francesco Cannizzaro | |
// @match *://*/* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
document.addEventListener('keydown', function(event) { | |
var video = document.querySelectorAll('video'); | |
if (![178, 179].includes(event.keyCode)) return; | |
video.forEach(video => { | |
if (video.currentTime > 0 && !video.ended) { | |
switch (event.keyCode) { | |
case 179: | |
if (video.paused) video.play(); | |
else video.pause(); | |
break; | |
case 178: | |
video.pause(); | |
video.currentTime = 0; | |
} | |
} | |
}); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment