YouTube blend webapp window decoration/theme color with header

Set theme color based on YouTube header's background color, and make the header color stay persistent when theater mode is toggled. (Works both for dark and light mode)

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube blend webapp window decoration/theme color with header
// @namespace    https://greasyfork.org/users/1257389
// @version      1.0.00
// @description  Set theme color based on YouTube header's background color, and make the header color stay persistent when theater mode is toggled. (Works both for dark and light mode)
// @author       dvirzxc
// @match        https://www.youtube.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to check if YouTube is in dark mode
    function isDarkMode() {
        return document.documentElement.getAttribute('dark') !== null;
    }

    // Function to inject CSS based on mode
    function injectCSS() {
        const darkModeCSS = `
            div.style-scope.ytd-masthead {
                background-color: #212121 !important;
            }
            yt-icon {
                color: #f3f3f3 !important;
            }
            div.style-scope.ytd-searchbox {
                background-color: #212121 !important;
            }
        `;

        const lightModeCSS = `
            div.style-scope.ytd-masthead {
                background-color: #ffffff !important;
            }
            yt-icon {
                color: #000000 !important;
            }
            div.style-scope.ytd-searchbox {
                background-color: #ffffff !important;
            }
        `;

        const style = document.createElement('style');
        style.type = 'text/css';

        if (isDarkMode()) {
            style.textContent = darkModeCSS;
        } else {
            style.textContent = lightModeCSS;
        }

        // Remove any previously injected style elements
        document.querySelectorAll('style[name="YouTubeDarkModeToggle"]').forEach(styleElement => {
            styleElement.remove();
        });

        // Inject the new style element
        style.setAttribute('name', 'YouTubeDarkModeToggle');
        document.head.appendChild(style);
    }

    // Check and inject CSS when the page loads
    injectCSS();

    // Listen for changes in dark mode and inject CSS accordingly
    const observer = new MutationObserver(injectCSS);
    observer.observe(document.documentElement, { attributes: true });
})();