- youtube.com - Turn On Dark Mode

Automatically switch on dark mode. Useful for incognito tabs.

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

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

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.com - Turn On Dark Mode
// @namespace   Yury Ershov
// @match       *://www.youtube.com/*
// @run-at      document-idle
// @noframes
// @grant       none
// @version     1.0
// @author      Yury Ershov
// @description Automatically switch on dark mode. Useful for incognito tabs.
// @inject-into content
// ==/UserScript==

(function() {
  'use strict';

  let IsDark = ()=>!!document.querySelector("html[dark]");
  let IsSpinner = ()=>{
    let e = document.querySelector("ytd-popup-container iron-dropdown div#spinner");
    return !e || getComputedStyle(e).display != "none";
  }
  let HasSettings = ()=>!!document.querySelector("button#button[aria-label=Settings]");
  let ClickSettings = ()=>
    [...document.querySelectorAll("button#button[aria-label=Settings]")].forEach(e=>e.click());
  let ClickDarkModeMenu = ()=>
    [...document.querySelectorAll("ytd-toggle-theme-compact-link-renderer")].
      filter(e=>e.innerText=="Dark theme: Off").
      forEach(e=>e.click());
  let ClickDarkModeSwitch = ()=>
    [...document.querySelectorAll("div#submenu div#caption-container")].
      filter(e=>e.innerText=="DARK THEME").
      forEach(e=>
        [...e.querySelectorAll("div#toggleButton")].forEach(e=>e.click()))

  if (IsDark()) return;

  let WaitSettings = ()=>{
    if (!HasSettings()) {
      setTimeout(WaitSettings, 300);
      return;
    }
    ClickSettings();
    let WaitNoSpinner = ()=>{
      if (IsSpinner()) {
        setTimeout(WaitNoSpinner, 1000);
        return;
      }
      ClickDarkModeMenu();
      ClickDarkModeSwitch();
      setTimeout(()=>document.body.click(), 500);
    };
    setTimeout(WaitNoSpinner, 500);
  };

  setTimeout(WaitSettings, 1000);
})();