Forked from stefansundin/youtube-global-shortcuts.user.js
Last active
May 20, 2021 12:52
-
-
Save yuryshulaev/b383dbb04256acea2f7a to your computer and use it in GitHub Desktop.
Makes the YouTube shortcuts work even if the player is not focused. Install Greasemonkey/Tampermonkey first, then click [Raw] to install.
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 YouTube global shortcuts | |
// @namespace https://gist.github.com/stefansundin/ | |
// @homepage https://gist.github.com/stefansundin/65e3d6db697636d8e7f1 | |
// @downloadURL https://gist.github.com/stefansundin/65e3d6db697636d8e7f1/raw/youtube-global-shortcuts.user.js | |
// @version 1.1 | |
// @author Stefan Sundin | |
// @description Makes the YouTube shortcuts work even if the player is not focused. | |
// @icon https://www.youtube.com/favicon.ico | |
// @match https://www.youtube.com/* | |
// ==/UserScript== | |
// Space = Pause/play | |
// F = Toggle fullscreen | |
// M = Toggle mute | |
// P = Previous video (if in playlist) | |
// N = Next video (if in playlist) | |
// left and right keys focuses the button, but the first keydown won't trigger seeking unfortunately | |
// P and N already works without this userscript, but you must hold shift. | |
// Use Ctrl+Space to scroll down, Shift+Space to scroll up | |
window.addEventListener('keydown', function (e) { | |
var button = document.getElementsByClassName('ytp-play-button')[0] || document.getElementById('movie_player'); | |
if (!button) { | |
return; | |
} | |
if (['TEXTAREA','INPUT'].indexOf(e.srcElement.tagName) !== -1 || | |
['yt-commentbox-text', 'yt-simplebox-text', 'ytp-progress-bar', 'ytp-volume-panel'].indexOf(e.srcElement.className) !== -1) { | |
return; | |
} | |
if (e.srcElement.classList.contains('html5-video-player')) { | |
return; | |
} | |
var c = String.fromCharCode(e.keyCode).toLowerCase(); | |
if (c === ' ' || e.keyCode === 37 || e.keyCode === 39) { // 37 = left key, 39 = right key | |
if (c === ' ' && (e.shiftKey || e.ctrlKey)) { | |
return; | |
} | |
if (c === ' ') { | |
document.activeElement.blur(); | |
button.click(); | |
button.blur(); | |
e.preventDefault(); | |
} | |
} else if (c === 'n') { | |
document.getElementsByClassName('ytp-next-button')[0].click(); | |
} else if (c === 'p') { | |
document.getElementsByClassName('ytp-prev-button')[0].click(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having trouble getting this to work. Can you please help me figure this out?
I downloaded Tampermonkey and installed it with the Raw button and tampermonkey says that the script is on when i visit a youtube page. But i dont see how i can program/use keyboard shortcuts. I tried the keyboard shortscuts listed in the script, but only the default keys are working when youtube is open. Is there somewhere that i can edit the different keyboard shortcuts? Is there somewhere in the script i need to edit? Or a setting in tampermonkey? I also use Keyboard Maestro, would that be helpful here?
Thanks, Dan