Greasy Fork

任意网站去黑白

去除任意网站的黑白样式

目前为 2022-12-03 提交的版本。查看 最新版本

// ==UserScript==
// @name 任意网站去黑白
// @namespace http://tampermonkey.net/
// @version 3.0
// @description 去除任意网站的黑白样式
// @author share121
// @match *://*/*
// @license MIT
// @grant none
// @homepageURL https://greasyfork.org/zh-CN/scripts/455866
// @supportURL https://greasyfork.org/zh-CN/scripts/455866-%E4%BB%BB%E6%84%8F%E7%BD%91%E7%AB%99%E5%8E%BB%E9%BB%91%E7%99%BD/feedback
// @run-at document-body
// ==/UserScript==

function grayToColorful() {
    document
        .querySelector("head")
        .appendChild(document.createElement("style")).innerText =
        ".grayToColorful{filter:grayscale(0)!important;-webkit-filter:grayscale(0)!important;-moz-filter:grayscale(0)!important;-ms-filter:grayscale(0)!important;-o-filter:grayscale(0)!important;filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)!important;}";
    document.querySelectorAll("*").forEach(function (e) {
        let s = getComputedStyle(e),
            g = function (t) {
                if (!Boolean(t)) {
                    return false;
                }
                return /grayscale/i.test(t);
            };
        if (
            g(s.filter) ||
            g(s.webkitFilter) ||
            g(s.mozFilter) ||
            g(s.msFilter) ||
            g(s.oFilter)
        ) {
            e.classList.add("grayToColorful");
        }
    });
}

if (document.readyState === "loading") {
    // 此时加载尚未完成
    let style_ = document.createElement("style");
    style_.id = "grayToColorful";
    style_.innerText =
        "*{filter:grayscale(0)!important;-webkit-filter:grayscale(0)!important;-moz-filter:grayscale(0)!important;-ms-filter:grayscale(0)!important;-o-filter:grayscale(0)!important;filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=0)!important;}";
    document.querySelector("head").appendChild(style_);
    document.addEventListener("DOMContentLoaded", function () {
        document
            .querySelector("head")
            .removeChild(document.getElementById("grayToColorful"));
        grayToColorful();
    });
} else {
    // 此时 DOMContentLoaded 已经被触发
    grayToColorful();
}