网页深色模式切换

按Ctrl+Alt+D快捷键切换网页深色模式

// ==UserScript==
// @name         网页深色模式切换
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  按Ctrl+Alt+D快捷键切换网页深色模式
// @author       AI助手
// @match        *://*/*
// @grant        none
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const DARK_MODE_STYLE_ID = '__tampermonkey_dark_mode_style__';
    const STORAGE_KEY = 'darkModeEnabled';

    function applyDarkMode() {
        let style = document.getElementById(DARK_MODE_STYLE_ID);
        if (!style) {
            style = document.createElement('style');
            style.id = DARK_MODE_STYLE_ID;
            document.head.appendChild(style);
        }
        style.innerHTML = `
            html {
                filter: invert(1) hue-rotate(180deg);
            }
            body {
                background-color: #333;
            }
            img, video, .invert-exclude {
                filter: invert(1) hue-rotate(180deg);
            }
        `;
    }

    function removeDarkMode() {
        const style = document.getElementById(DARK_MODE_STYLE_ID);
        if (style) {
            style.remove();
        }
    }

    function toggleDarkMode() {
        const isEnabled = localStorage.getItem(STORAGE_KEY) === 'true';
        if (isEnabled) {
            removeDarkMode();
            localStorage.setItem(STORAGE_KEY, 'false');
        } else {
            applyDarkMode();
            localStorage.setItem(STORAGE_KEY, 'true');
        }
    }

    // 页面加载时检查并恢复深色模式状态
    if (localStorage.getItem(STORAGE_KEY) === 'true') {
        applyDarkMode();
    }

    document.addEventListener('keydown', function(e) {
        // Ctrl+Alt+D
        if (e.ctrlKey && e.altKey && !e.shiftKey && !e.metaKey && (e.key === 'd' || e.key === 'D')) {
            e.preventDefault();
            toggleDarkMode();
        }
    });

})();