Dark Mode Toggle (Shortcut Only)

Adds a dark mode toggle to any website via keyboard shortcut with persistence

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Dark Mode Toggle (Shortcut Only)
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Adds a dark mode toggle to any website via keyboard shortcut with persistence
// @author       Drewby123
// @match        *://*/*
// @license      MIT
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Create a style element for dark mode
    const darkModeStyle = document.createElement('style');
    darkModeStyle.id = 'dark-mode-style';
    darkModeStyle.textContent = `
        html {
            filter: invert(1) hue-rotate(180deg);
            background: #111 !important;
        }
        img, video {
            filter: invert(1) hue-rotate(180deg) !important;
        }
    `;

    // Load dark mode state from localStorage
    let darkModeEnabled = localStorage.getItem('darkModeEnabled') === 'true';
    if (darkModeEnabled) {
        document.head.appendChild(darkModeStyle);
    }

    // Toggle functionality
    const toggleDarkMode = () => {
        darkModeEnabled = !darkModeEnabled;
        if (darkModeEnabled) {
            document.head.appendChild(darkModeStyle);
        } else {
            if (document.getElementById('dark-mode-style')) {
                document.getElementById('dark-mode-style').remove();
            }
        }
        localStorage.setItem('darkModeEnabled', darkModeEnabled);
    };

    // Add keyboard shortcut (Alt + N) to toggle dark mode
    document.addEventListener('keydown', (event) => {
        if (event.altKey && event.key === 'n') {
            toggleDarkMode();
        }
    });
})();