White background replacer

Replaces the background white color with a darker one in order to decrease eye strain

当前为 2023-01-31 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         White background replacer
// @namespace    http://siavoshkc.com/
// @version      2.4
// @description  Replaces the background white color with a darker one in order to decrease eye strain
// @author       siavoshkc
// @match        *://*/*
// @grant        none
// @license      MIT
// @run-at       document-idle
// ==/UserScript==
const goodBgColors = ["#b0edc4", "#79d2a6", "#80e5ff", "#79C664", "#64C6B6","#4DD7C0","#B1CE61", "#61CEB2", "#66C0A9",
                      "#6BA6D2", "#4295D3", "#7190DB", "#D97FD5", "#D97FC0","#D97F7F", "#7FD7D9", "#7FD997", "#87D97F",
                      "#D9BD7F", "#DDA279", "#73CA70", "#7092CA", "#7CCA70", "#ACCA70,", "#9B70CA", "#70CA82", "#7599E0"]
const INTERVAL = 120000
var currentGoodBgColor = 0

function isWhite(bg)
{
    const rgb = bg.match(/[0-9]+/g);
    const hsl = bg.match(/hsl\(\s*(\d+)\s*,\s*(\d+(?:\.\d+)?%)\s*,\s*(\d+(?:\.\d+)?%)\)/)
    return (
        bg == "white"||
        bg == "#ffffff"||
        bg == "#FFFF"||
        bg == "#FFF"||
        bg == "#fff"||
        (hsl && Number(hsl[3].replace('%','')) > 89) ||
        (rgb && rgb[0] > 230 && rgb[1] > 230 && rgb[2] >230)
    );
}
function changeColor(style) {
    console.debug("White background replacer: Trying to change background color ", style.backgroundColor)
    style.backgroundColor = goodBgColors[currentGoodBgColor % goodBgColors.length]
    console.debug("White background replacer: Changing one style background-color to ", goodBgColors[currentGoodBgColor % goodBgColors.length]);
    if(style.color == style.backgroundColor) style.color = "black";
    if(currentGoodBgColor === Number.MAX_SAFE_INTEGER) currentGoodBgColor = 0;
    else currentGoodBgColor++;
    setTimeout(changeColor, INTERVAL, style)

}
function checkPage()
{
    'use strict';
    console.debug("White background replacer: Running...");

    if(document.styleSheets.length === 0) changeColor(document.body.style);
    else
    {
        for (let sheet of document.styleSheets) {
            try{
                let rules= sheet.cssRules;

                for (let rule of rules) {

                    if( rule?.style && (
                        isWhite(rule.style?.backgroundColor) ||
                        isWhite(rule.style.getPropertyValue('--bg')) ||
                        isWhite(rule.style.getPropertyValue('--bg-color')) ||
                        isWhite(rule.style.getPropertyValue('--background-color')) ||
                        isWhite(rule.style.getPropertyValue('--background'))
                      ))
                    {
                        changeColor(rule.style);
                    }
                    if (rule?.cssRules) {
                        for (let innerRule of rule.cssRules) {
                            if( innerRule?.style && isWhite(innerRule.style?.backgroundColor)){
                                changeColor(innerRule.style);
                            }
                        }
                    }

                }
            }
            catch(e)
            {
                console.warn("White background replacer: Caught exception: ".concat(e));
                continue;
            }
        }
    }

};
checkPage() ;