// ==UserScript==
// @name More YouTube Hotkeys
// @namespace https://greasyfork.org/en/users/85671-jcunews
// @version 1.1.3
// @license AGPLv3
// @author jcunews
// @description Adds more keyboard shortcuts for YouTube. The list of all new shortcuts is added into new "More Shortcuts" section on YouTube's "Keyboard shortcuts" popup which can be accessed via "?" or SHIFT+/ key (on U.S. keyboards).
// @match *://www.youtube.com/*
// @grant none
// @run-at document-start
// ==/UserScript==
(ch => {
//=== CONFIGURATION BEGIN
/*
`key` is the key name. If it's a letter, it must be in uppercase.
`mods` is a zero or up to 3 modifier key characters (in any order): `A`=Alt, `C`=Control, `S`=Shift. Character case is ignored.
e.g. "" (no modifier key), "s" (Shift), "Cs" (Control+Shift), "aSc" (Control+Shift+Alt).
`desc` is the hotkey description which will be added onto YouTube's Hotkey List Popup (accessible via `?` or `SHIFT+/` key).
If this property is empty or doesn't exist, the hotkey won't be included in YouTube's Hotkey List Popup.
`keys` is an optional custom text representation for the keyboard keys which is useful to represent multiple hotkeys.
`func` is the JavaScript function to execute with the activated hotkey object as the first argument.
*/
var hotkeys = [
{key: "`", mods: "", desc: "Toggle guide / sidebar", func: a => eleClick('#guide-button')},
{key: ")", mods: "S", desc: "Seek to specific point in the video (SHIFT+7 advances to 75% of duration)", keys: "SHIFT+0..SHIFT+9", func: a => videoSeekTo(0.05)},
{key: "!", mods: "S", func: a => videoSeekTo(0.15)},
{key: "@", mods: "S", func: a => videoSeekTo(0.25)},
{key: "#", mods: "S", func: a => videoSeekTo(0.35)},
{key: "$", mods: "S", func: a => videoSeekTo(0.45)},
{key: "%", mods: "S", func: a => videoSeekTo(0.55)},
{key: "^", mods: "S", func: a => videoSeekTo(0.65)},
{key: "&", mods: "S", func: a => videoSeekTo(0.75)},
{key: "*", mods: "S", func: a => videoSeekTo(0.85)},
{key: "(", mods: "S", func: a => videoSeekTo(0.95)},
{key: "J", mods: "S", desc: "Rewind video by 30 seconds", func: a => videoSeekBy(-30)},
{key: "L", mods: "S", desc: "Fast forward video by 30 seconds", func: a => videoSeekBy(30)},
{key: "E", mods: "", desc: "Toggle like video", func: a => eleClick('#text[aria-label*=" like"]')},
{key: "E", mods: "S", desc: "Toggle dislike video", func: a => eleClick('#text[aria-label*=" dislike"]')},
{key: "H", mods: "", desc: "Share video", func: a => eleClick('#text[aria-label="Share"]')},
{key: "V", mods: "", desc: "Save video into playlist", func: a => eleClick('#text[aria-label="Save"]')},
{key: "U", mods: "", desc: "Toggle subscription", func: a => eleClick('paper-button.ytd-subscribe-button-renderer')},
{key: "Y", mods: "", desc: "Toggle subscription notification", func: a => eleClick('ytd-watch-flexy #notification-preference-toggle-button > .ytd-subscribe-button-renderer')},
{key: "R", mods: "", desc: "Toggle replay chat", func: a => eleClick('#show-hide-button.ytd-live-chat-frame > ytd-toggle-button-renderer.ytd-live-chat-frame')},
{key: "X", mods: "", desc: "Toggle autoplay of next non-playlist video", func: a => eleClick('paper-toggle-button.ytd-compact-autoplay-renderer')},
{key: "V", mods: "S", desc: "Go to user/channel video page", func: a => navUser("Videos", "videos")},
{key: "Y", mods: "S", desc: "Go to user/channel playlists page", func: a => navUser("Playlists", "playlists")}
];
//=== CONFIGURATION END
var baseKeys = {};
("~`!1@2#3$4%5^6&7*8(9)0_-+={[}]:;\"'|\\<,>.?/").split("").forEach((c, i, a) => {
if ((i & 1) === 0) baseKeys[c] = a[i + 1];
});
function eleClick(s, e) {
(e = document.querySelector(s)) && e.click();
}
function videoSeekBy(t, v) {
(v = document.querySelector('.html5-video-player')) && v.seekBy(t);
}
function videoSeekTo(p, v) {
(v = document.querySelector('.html5-video-player')) && v.seekTo(v.getDuration() * p);
}
function navUser(tn, tp, a) {
if (!(new RegExp(`^/(channel|user)/[^/]+/${tp}$`)).test(location.pathname)) {
if (!Array.from(document.querySelectorAll('.paper-tab')).some(e => {
if (e.textContent.trim() === tn) {
e.parentNode.click();
return true;
}
})) {
if (
(a = document.querySelector('#owner-name > a')) &&
((a = a.pathname + "/" + tp) !== location.pathname)
) nav.navigate({commandMetadata: {webCommandMetadata: {url: a, webPageType: "WEB_PAGE_TYPE_BROWSE"}}}, false);
}
}
}
function checkHotkeyPopup(a, b, c, d, e) {
if ((a = document.querySelector("#sections.ytd-hotkey-dialog-renderer")) && !a.querySelector(".more-hotkeys")) {
a.appendChild(b = (d = a.firstElementChild).cloneNode(false));
a.appendChild(d.cloneNode(false));
b.classList.add("more-hotkeys");
b.appendChild(d.firstElementChild.cloneNode(false)).textContent = "More Hotkeys";
c = b.appendChild((d = d.children[1]).cloneNode(false));
c.innerHTML = "";
d = d.firstElementChild;
hotkeys.forEach((h, e, f) => {
if (h.desc) {
e = c.appendChild(d.cloneNode(true));
e.firstElementChild.textContent = h.desc;
if (!(f = h.keys)) {
if (h.shift) {
f = h.key + " (" + (h.ctrl ? "CTRL+" : "") + (h.shift ? "SHIFT+" : "") + (h.alt ? "ALT+" : "") + (h.shift ? baseKeys[h.key] || h.key.toLowerCase() : h.key) + ")";
} else f = h.key.toLowerCase();
}
e.children[1].textContent = f;
}
});
} if (--ch) setTimeout(checkHotkeyPopup, 100);
}
function editable(e) {
var r = false;
while (e) {
if (e.contentEditable === "true") return true;
e = e.parentNode;
}
return r;
}
hotkeys.forEach(h => {
var a = h.mods.toUpperCase().split("");
h.shift = a.includes("S");
h.ctrl = a.includes("C");
h.alt = a.includes("A");
});
addEventListener("keydown", (ev, a) => {
if ((a = document.activeElement) && (editable(a) || (a.tagName === "INPUT") || (a.tagName === "TEXTAREA"))) return;
if ((ev.key === "?") && ev.shiftKey && !ev.ctrlKey && !ev.altKey) {
ch = 10;
setTimeout(checkHotkeyPopup, 100);
}
hotkeys.forEach(h => {
if ((ev.key.toUpperCase() === h.key) && (ev.shiftKey === h.shift) && (ev.ctrlKey === h.ctrl) && (ev.altKey === h.alt)) {
ev.preventDefault();
("function" === typeof h.func) && h.func(h);
}
});
}, true);
})();