YouTube auto dark mode

Automatically switch the dark mode according to the system settings, which uses the official style

  1. // ==UserScript==
  2. // @name YouTube auto dark mode
  3. // @version 1.0.0
  4. // @description Automatically switch the dark mode according to the system settings, which uses the official style
  5. // @namespace https://youtube.com/
  6. // @match https://www.youtube.com/*
  7. // @icon https://www.google.com/s2/favicons?sz=64&domain=youtube.com
  8. // @author bowencool
  9. // @license MIT
  10. // @homepageURL https://gf.qytechs.cn/scripts/460040
  11. // @supportURL https://github.com/bowencool/Tampermonkey-Scripts/issues
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function () {
  16. "use strict";
  17.  
  18. window
  19. .matchMedia("(prefers-color-scheme: dark)")
  20. .addEventListener("change", (e) => {
  21. toggle(e.matches);
  22. });
  23.  
  24. function toggle(
  25. isDarkMode = window.matchMedia("(prefers-color-scheme: dark)").matches
  26. ) {
  27. const currentTheme = document.querySelector("html").getAttribute("dark");
  28. console.log({ isDarkMode, currentTheme });
  29. if (isDarkMode) {
  30. if (currentTheme !== null) return;
  31. document.querySelector("html").setAttribute("dark", "");
  32. } else {
  33. if (currentTheme === null) return;
  34. document.querySelector("html").removeAttribute("dark");
  35. }
  36. }
  37. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址