Toggle dark mode with Ctrl+Shift+D

Toggles dark mode with Ctrl+Shift+D.

目前為 2025-04-13 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Toggle dark mode with Ctrl+Shift+D
// @description  Toggles dark mode with Ctrl+Shift+D.
// @version      1.0
// @namespace	 https://github.com/yookibooki
// @author       yookibooki
// @license      MIT
// @homepage     https://github.com/yookibooki/userscripts/tree/main/google-search-site-switch
// @supportURL   https://github.com/yookibooki/userscripts/issues
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function() {
    'use strict';

    // Configuration
    const STORAGE_KEY = 'universal-dark-mode-enabled';
    const CSS_CLASS = 'universal-dark-mode-active';

    // Add persistent styles
    GM_addStyle(`
        /* Smooth transition for dark mode toggle */
        html {
            transition: filter 0.3s ease !important;
            min-height: 100vh !important;
        }

        /* Restore media elements to original colors */
        .${CSS_CLASS} img,
        .${CSS_CLASS} video,
        .${CSS_CLASS} iframe,
        .${CSS_CLASS} canvas {
            filter: invert(1) hue-rotate(180deg) !important;
        }

        /* Fix text selection colors */
        .${CSS_CLASS}::selection {
            background: #fff !important;
            color: #000 !important;
        }
    `);

    // Toggle functionality
    function toggleDarkMode() {
        const html = document.documentElement;
        const isActive = html.classList.toggle(CSS_CLASS);

        // Save state
        GM_setValue(STORAGE_KEY, isActive);
    }

    // Keyboard shortcut handler
    function handleShortcut(e) {
        if (e.ctrlKey && e.shiftKey && e.keyCode === 68) { // Ctrl+Shift+D
            toggleDarkMode();
            e.preventDefault();
            e.stopPropagation();
        }
    }

    // Initialize state
    if (GM_getValue(STORAGE_KEY, false)) {
        document.documentElement.classList.add(CSS_CLASS);
    }

    // Event listeners
    document.addEventListener('keydown', handleShortcut, true);

    // Apply inversion filter dynamically to handle CSP issues
    const style = document.createElement('style');
    style.textContent = `
        .${CSS_CLASS} {
            filter: invert(1) hue-rotate(180deg) !important;
            background: #fff !important; /* Create inversion base */
        }
    `;
    document.head.appendChild(style);
})();