Greasy Fork

Greasy Fork is available in English.

网页深色模式切换

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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();
        }
    });

})();