More YouTube Hotkeys

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).

Από την 27/05/2019. Δείτε την τελευταία έκδοση.

// ==UserScript==
// @name         More YouTube Hotkeys
// @namespace    https://greasyfork.org/en/users/85671-jcunews
// @version      1.0.1
// @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 => {
  var hotkeys = [
    {key: "`", mods: "", name: "Toggle guide / sidebar",
      func: a => (a = document.querySelector('#guide-button')) && a.click()},
    {key: "J", mods: "S", name: "Rewind video by 30 seconds",
      func: a => (a = document.querySelector('.html5-video-player')) && a.seekBy(-30)},
    {key: "L", mods: "S", name: "Fast forward video by 30 seconds",
      func: a => (a = document.querySelector('.html5-video-player')) && a.seekBy(30)},
    {key: "E", mods: "", name: "Toggle like video",
      func: a => (a = document.querySelector('#text[aria-label*=" like"]')) && a.click()},
    {key: "E", mods: "S", name: "Toggle dislike video",
      func: a => (a = document.querySelector('#text[aria-label*=" dislike"]')) && a.click()},
    {key: "H", mods: "", name: "Share video",
      func: a => (a = document.querySelector('#text[aria-label="Share"]')) && a.click()},
    {key: "V", mods: "", name: "Save video into playlist",
      func: a => (a = document.querySelector('#text[aria-label="Save"]')) && a.click()},
    {key: "U", mods: "", name: "Toggle subscription",
      func: a => (a = document.querySelector('.ytd-subscribe-button-renderer')) && a.click()},
    {key: "Y", mods: "", name: "Toggle subscription notification",
      func: a => (a = document.querySelector('#notification-preference-toggle-button > .ytd-subscribe-button-renderer')) && a.click()},
    {key: "R", mods: "", name: "Toggle replay chat",
      func: a => (a = document.querySelector('#show-hide-button.ytd-live-chat-frame > ytd-toggle-button-renderer.ytd-live-chat-frame')) && a.click()},
    {key: "X", mods: "", name: "Toggle autoplay of next non-playlist video",
      func: a => (a = document.querySelector('paper-toggle-button.ytd-compact-autoplay-renderer')) && a.click()},
    {key: "V", mods: "S", name: "Go to user/channel video page",
      func: a => navUser("Videos", "videos")},
    {key: "Y", mods: "S", name: "Go to user/channel playlists page",
      func: a => navUser("Playlists", "playlists")}
  ];
  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 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) => {
        e = c.appendChild(d.cloneNode(true));
        e.firstElementChild.textContent = h.name;
        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);

})();