Skip to content

Instantly share code, notes, and snippets.

@aleohl
Forked from fcannizzaro/media-keyboard.user.js
Created October 29, 2021 21:48
Show Gist options
  • Save aleohl/ce2eba7fc3c2fd942f3d6cc242464258 to your computer and use it in GitHub Desktop.
Save aleohl/ce2eba7fc3c2fd942f3d6cc242464258 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