Browser Background Color Controller

Change the background color in all web pages to protect your eyes. we map different original color to different new colors automatically to make it looks better than others which only replace the original color with only one color.

От 28.06.2022. Виж последната версия.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey, Greasemonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да инсталирате разширение, като например Tampermonkey .

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Violentmonkey.

За да инсталирате този скрипт, трябва да имате инсталирано разширение като Tampermonkey или Userscripts.

За да инсталирате скрипта, трябва да инсталирате разширение като Tampermonkey.

За да инсталирате този скрипт, трябва да имате инсталиран скриптов мениджър.

(Вече имам скриптов мениджър, искам да го инсталирам!)

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да инсталирате разширение като Stylus.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

За да инсталирате този стил, трябва да имате инсталиран мениджър на потребителски стилове.

(Вече имам инсталиран мениджър на стиловете, искам да го инсталирам!)

// ==UserScript==
// @name           Browser Background Color Controller
// @namespace      Background Color
// @description    Change the background color in all web pages to protect your eyes. we map different original color to different new colors automatically to make it looks better than others which only replace the original color with only one color.
// @version        2.3
// @include        http*
// @include        ftp
// @exclude
// @license MIT
// @grant    GM_addStyle
// ==/UserScript==

// Threshold
var Rth = 230;
var Gth = 230;
var Bth = 230;
var color = "#FDF6E3" //New color
var renderClasses = ["nH"];


function parseRGB(RGB) {
    if (RGB == undefined) return[0, 0, 0, 0];
    var rgba = RGB.replace(/[rgba\(\)]/g, '');
    var bg = rgba.split(",");
    if (bg < 3) return[0, 0, 0, 0];
    return [parseInt(bg[0]), parseInt(bg[1]), parseInt(bg[2]), parseInt(bg[3])];
}

function adjust(color, amount) {
        return '#' + color.replace(/^#/, '').replace(/../g, color => ('0'+Math.min(255, Math.max(0, parseInt(color, 16) + Math.floor(amount))).toString(16)).substr(-2));
    }

function realBackgroundColor(elem) {
    var transparent = 'rgba(0, 0, 0, 0)';
    if (!elem) return transparent;

    var computed = getComputedStyle(elem);
    if (computed.backgroundImage !== "none") {
        return undefined;
    }

    var bg = computed.backgroundColor;
    if (bg === transparent) {
        return realBackgroundColor(elem.parentElement);
    } else {
        return bg;
    }
}



function changeElementsColor() {
    var allTags = document.getElementsByTagName("*");
    var classes = [];
    var colors = []
    var hover = [].slice.call(document.querySelectorAll( ":hover" ));
    for (var i = 0; i < allTags.length; i++) {
        try{
            if (allTags[i].style.backgroundColor == color || allTags[i].style.bgColorAlreadySet == true) {
                continue;
            }
            allTags[i].style.bgColorAlreadySet = true;
            var rbgcolor = realBackgroundColor(allTags[i]);
            var RGB = parseRGB(rbgcolor);
            if (RGB[0] > Rth && RGB[1] > Gth && RGB[2] > Bth || allTags[i].tagName == "BODY") {
                //console.log(Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
                var acolor = adjust(color, Math.max(-10, (RGB[0] + RGB[1] + RGB[2]) / 3 - 255));
                allTags[i].style.backgroundColor = acolor;
                //console.log(allTags[i].classList.map(x=>`.${x}{background-color: #FDF6E3}\n`))
                //for (var j = 0; j < allTags[i].classList.length; ++j){
                //    classes.push(`.${allTags[i].classList[j]}{background-color: ${acolor}}\n`);
                //}
            }
        }catch(err) {
            console.log(err);
        }
    }
    //console.log(classes);
    //GM_addStyle(classes.join(""));
    //GM_addStyle(renderClasses.map(x=>`.${x}{background-color: ${color}}\n`).join(""));
}

function handleAutoPage() {
    var observer = new window.MutationObserver(function(mutations) {
        var currentLen = document.getElementsByTagName("*").length;
        if (mutations[0].addedNodes) {
                setTimeout(function() {
                    if(document.getElementsByTagName("*").length == currentLen){
                        console.log(document.getElementsByTagName("*").length)
                        changeElementsColor();
                    }
                }, 50);
        }
    });
    observer.observe(document, {
        childList: true,
        subtree: true
    });
}


(function() {
    changeElementsColor();
    handleAutoPage();

})();