Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.