Global Darkmode

Turn only bright websites to dark.

目前為 2021-11-02 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Global Darkmode
// @description    Turn only bright websites to dark.

// @name:ko        글로벌 다크모드
// @description:ko 밝은 색의 웹 사이트들만 어둡게 만듭니다.

// @namespace      https://ndaesik.tistory.com/
// @version        2021.11.02.14:07
// @author         ndaesik
// @icon           https://www.iconsdb.com/icons/preview/gray/moon-4-xxl.png
// @include        *
// ==/UserScript==

// Turn light page to dark
var styles = `
html[w_root] {background:#FFF!important}
html[white] body {background:none!important}
html[white] * {text-shadow:0 0 .1px}
html[white],
html[white] :is(i, img, image, embed, video, canvas, option, object, frame, :fullscreen:not(iframe), iframe:not(:fullscreen), body frameset),
html[white] body>* [style*="url("]:not([style*="cursor:"]):not([type="text"]) {filter:invert(1)hue-rotate(180deg)!important}
html[white] video:fullscreen,
html[white] body>* [style*="url("]:not([style*="cursor:"]) :not(#⁠){filter:unset!important}

html[white]:not(#⁠) :is(canvas, option, object, frame, body frameset) :is(i, img, image, embed, video),
html[white]:not(#⁠) video:fullscreen{filter:unset!important}
`

const styleSheet = document.createElement("style")
styleSheet.type = "text/css"
styleSheet.innerText = styles
document.head.appendChild(styleSheet)

// Detect page is whether light or dark
function wd(target) {
    const v = getComputedStyle(document.querySelector(target), null).getPropertyValue("background-color").match(/\d+/g);
    const c = (parseInt(v[0]) + parseInt(v[1]) + parseInt(v[2])) > 500 || parseInt(v[3]) == 0; // get sum of rgb and check | return true when it's invisible
    if ( c == true ) { return true; }
    return false;
}
function n(n){document.documentElement.setAttribute(n, "");}

// set attribute
if ( self === top ) {
    if ( wd("html") && wd("body") && window.parent.document.body.offsetHeight !== 0 ) {
        n("white");
        n("w_root");
    } else;
};

if ( self !== top ) {
    if ( !( wd("html") && wd("body") ));
    else n("white");
};

if (window.parent.document.body.offsetHeight == 0) {
    let max;
    document.querySelectorAll('body > *').forEach(e=>{
        if (max == null) max = e;
        else if (e.scrollHeight > max.scrollHeight) max = e;
    });
    const v2 = getComputedStyle(max, null).getPropertyValue("background-color").match(/\d+/g);
    const c2 = (parseInt(v2[0]) + parseInt(v2[1]) + parseInt(v2[2])) > 500 || parseInt(v2[3]) == 0;
    if ( c2 == true ) {
        if ( wd("html") ) {
            n("white");
            n("w_root");
        } else { n("white"); }
    }
    else;
};