Skip to content

Instantly share code, notes, and snippets.

@fcannizzaro
Created December 14, 2017 12:23
Show Gist options
  • Save fcannizzaro/0498b27f0bb403e1ad0d4b83b3138a23 to your computer and use it in GitHub Desktop.
Save fcannizzaro/0498b27f0bb403e1ad0d4b83b3138a23 to your computer and use it in GitHub Desktop.
use media key to play/pause HTML5 video
// ==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