YouTube Dark Mode Toggle

Adds a toggle button to switch YouTube between dark and light text mode

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YouTube Dark Mode Toggle
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description Adds a toggle button to switch YouTube between dark and light text mode
n // @author       You
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Create toggle button
    const toggle = document.createElement('button');
    toggle.innerText = 'Toggle Dark Mode';
    toggle.style.position = 'fixed';
    toggle.style.bottom = '10px'; // bottom-left corner
    toggle.style.left = '10px';
    toggle.style.zIndex = '9999';
    toggle.style.padding = '10px';
    toggle.style.backgroundColor = '#000';
    toggle.style.color = '#fff';
    toggle.style.border = 'none';
    toggle.style.borderRadius = '5px';
    toggle.style.cursor = 'pointer';
    document.body.appendChild(toggle);

    let darkMode = false;

    toggle.addEventListener('click', () => {
        darkMode = !darkMode;

        if (darkMode) {
            document.body.style.backgroundColor = '#111';
            document.body.style.color = '#fff';
            document.querySelectorAll('*').forEach(el => {
                if (el.tagName !== 'SCRIPT' && el.tagName !== 'STYLE') {
                    el.style.color = '#fff';
                }
            });
        } else {
            document.body.style.backgroundColor = '#fff';
            document.body.style.color = '#000';
            document.querySelectorAll('*').forEach(el => {
                if (el.tagName !== 'SCRIPT' && el.tagName !== 'STYLE') {
                    el.style.color = '#000';
                }
            });
        }
    });
})();