dark mode

auto dark mode

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         dark mode
// @namespace    http://tampermonkey.net/
// @version      2025-05-09
// @description  auto dark mode
// @author       Anc
// @match        *://*/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @run-at		 document.start
// @grant        GM.addStyle
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Add meta tag
    let meta = document.createElement('meta');
    meta.name = "theme-color";
    meta.content = "#000";
    meta.media = "(prefers-color-scheme: dark)";
    document.head.append(meta);

    function isDarkColor(rgbString) {
        const match = rgbString.match(/\d+/g);
        if (!match || match.length < 3) return false; // 非 RGB 格式视为浅色或无法判断
        const [r, g, b] = match.map(Number);
        const brightness = 0.299 * r + 0.587 * g + 0.114 * b;
        return brightness < 128;
    }

    function getEffectiveBackgroundColor(element) {
        while (element) {
            const style = window.getComputedStyle(element);
            const bgColor = style.backgroundColor;
            const bgImage = style.backgroundImage;

            if (bgImage && bgImage !== 'none') {
                console.log('背景图:', bgImage);
                return 'image'; // 有背景图,视为特殊处理
            }

            if (bgColor && bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent') {
                return bgColor;
            }

            element = element.parentElement;
        }
        return 'rgb(255, 255, 255)'; // 默认白色
    }

    function checkIfBackgroundIsDark() {
        const targets = [document.body, document.documentElement];
        for (const el of targets) {
            const result = getEffectiveBackgroundColor(el);
            if (result === 'image') {
                console.log('无法判断背景图颜色,请人工确认');
                return false;
            }
            if (isDarkColor(result)) {
                console.log('背景是深色');
                return true;
            }
        }
        console.log('背景是浅色');
        return false;
    }

    let isDark = checkIfBackgroundIsDark();

    if(!isDark) {
        // Add style
        GM.addStyle(`
                @media (prefers-color-scheme: dark) {
                :root {
                        filter: invert(1) hue-rotate(180deg);
                    }
	            figure,img,video,iframe,div[style*=image]{
                        filter: invert(1) hue-rotate(180deg);
                        opacity:1;
                    }

                figure img {
                        filter: unset;
                    }
                }
            `)
    }
})();