Skip to content

Instantly share code, notes, and snippets.

@ozturkkl
Last active June 3, 2024 16:38
Show Gist options
  • Save ozturkkl/0d72d3747fbc5510df981b7c1495f959 to your computer and use it in GitHub Desktop.
Save ozturkkl/0d72d3747fbc5510df981b7c1495f959 to your computer and use it in GitHub Desktop.
Youtube Shortcuts Focus Fix
// ==UserScript==
// @name Kemal's Youtube Keyboard Shortcut Fix
// @namespace http://tampermonkey.net/
// @version 2024-05-17
// @description Fix annoying inconsistent arrow key shortcuts
// @author Kemal
// @match https://gist.github.com/ozturkkl/0d72d3747fbc5510df981b7c1495f959
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant window.onurlchange
// @run-at document-idle
// @match https://*.youtube.com/*
// @match https://*.youtu.be/*
// ==/UserScript==
const FOCUS_ELEMENT = "#ytd-player video";
const MOUSEUP_ELEMENT = "#ytd-player";
(function () {
"use strict";
if (window.onurlchange === null) {
// feature is supported
window.addEventListener("urlchange", (info) => {
handleClick();
handleKeyPress();
});
}
handleClick();
handleKeyPress();
})();
function handleClick() {
try {
document
.querySelector(MOUSEUP_ELEMENT)
.addEventListener("mouseup", function (e) {
setTimeout(() => {
document.querySelector(FOCUS_ELEMENT).focus();
}, 250);
});
} catch (e) {
setTimeout(() => {
handleClick();
}, 1000);
}
}
function handleKeyPress() {
document.addEventListener("keyup", function (e) {
if (e.target.tagName !== "BODY") return;
if (e.code === "KeyT" || e.code === "KeyF") {
document.querySelector(FOCUS_ELEMENT).focus();
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment