White background replacer

Replaces the background white color with yellow in order to decrease eye strain

As of 2022-11-09. See the latest version.

// ==UserScript==
// @name         White background replacer
// @namespace    http://siavoshkc.com/
// @version      2.0
// @description  Replaces the background white color with yellow 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)
{
    return (
        bg == "white"||
        bg == "#ffffff"||
        bg == "#FFFF"||
        bg == "#FFF"||
        bg == "#fff" ||
        bg.replace(/ /g,'') == "rgb(255,255,255)"||
        bg.replace(/ /g,'').substring(0, 16) == "rgba(255,255,255"
    );
}
function changeColor(style) {
    style.backgroundColor = 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.log("White background replacer...");

    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)){
                        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.log("White background replacer: Caught exception: ".concat(e));
                continue;
            }
        }
    }

};
checkPage() ;