YouTube Usability

Disable frequently mistyped shortcuts (e.g. 0-9 number keys) and auto-looping on shorts.

Stan na 14-04-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name YouTube Usability
// @description Disable frequently mistyped shortcuts (e.g. 0-9 number keys) and auto-looping on shorts.
// @license MIT
// @icon https://m.youtube.com/static/apple-touch-icon-114x114-precomposed.png
// @namespace michaelsande.rs
// @version 2024.04.14
// @match https://www.youtube.com/*
// @match https://m.youtube.com/*
// @run-at document-start
// ==/UserScript==

// Tested on Firefox Greasemonkey and Violentmonkey, and Safari Userscripts.
(() => {
  "use strict";
  const beforeLoad = () => {
    const ALLOWED_MODIFIER_KEYS = ["Alt", "Control", "Meta", "OS"];
    const DISABLED_KEYS = new Set(
      "0123456789kjl".split("").concat(["Home", "End"])
    );

    window.addEventListener(
      "keydown",
      event => {
        if (
          DISABLED_KEYS.has(event.key) &&
          !ALLOWED_MODIFIER_KEYS.some(event.getModifierState.bind(event)) &&
          !event.isComposing
        ) {
          event.stopImmediatePropagation();
        }
      },
      true
    );

    window.addEventListener("load", afterLoad);
  };

  const afterLoad = () => {
    const pageManager = document.getElementById("page-manager");
    if (pageManager !== null) {
      const disableShortLooping = () => {
        pageManager
          .querySelector("#shorts-player video")
          ?.removeAttribute("loop");
      };

      disableShortLooping();

      // Shorts player gets inserted dynamically by YouTube when navigating, so
      // can't be used as target.
      new MutationObserver(disableShortLooping).observe(pageManager, {
        attributeFilter: ["loop"],
        childList: true,
        subtree: true,
      });
    }
  };

  beforeLoad();
})();