- youtube.com - Turn On Dark Mode

Automatically switch on dark mode. Useful for incognito tabs.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
})();