dark mode

auto dark mode

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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