'use strict';
// ==UserScript==
// @name White background replacer
// @namespace http://siavoshkc.com/
// @version 2.6
// @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", "#009900", "#6666ff","#e29d9d", "#7FD7D9", "#7FD997", "#87D97F",
"#D9BD7F", "#DDA279", "#73CA70", "#7092CA", "#7CCA70", "#ACCA70,", "#ffffb3", "#70CA82", "#c4e87d"]
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"||
bg == "#fdfdfd" ||
(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 iterateRules(cssRules) {
if(!cssRules) return
for (let rule of cssRules) {
try {
if(checkStyle(rule?.style)){
changeColor(rule?.style)
}
iterateRules(rule.cssRules)
} catch(e) {
console.warn("WBR: Caught exception: ", e)
}
}
}
function checkStyle(style) {
if(!style) return
return (
isWhite(style.background) ||
isWhite(style.backgroundColor) ||
isWhite(style.getPropertyValue('--bg')) ||
isWhite(style.getPropertyValue('--bg-color')) ||
isWhite(style.getPropertyValue('--background-color')) ||
isWhite(style.getPropertyValue('--background')) ||
isWhite(style.getPropertyValue('--lighting-color')) ||
isWhite(style.getPropertyValue('--theme-background-color')) ||
isWhite(style.getPropertyValue('--theme-content-background-color'))
)
}
function checkPage() {
console.debug("White background replacer: Running...", window.getComputedStyle(document.documentElement));
if(checkStyle(window.getComputedStyle(document.documentElement))){
console.debug("Global background detected")
//changeColor(document.body.style)
try {
var all = document.styleSheets,
s = all[all.length - 1],
l = s.cssRules.length;
console.debug("insertRule", s.insertRule)
if (s.insertRule) {
s.insertRule('#content {background-color: DarkGrey !important}', l)
console.debug("Global rule inserted")
}
} catch(e) {
console.debug("WBR: Caught exception: ", e)
const s = document.createElement('style');
s.innerHTML = '#content {background-color: Tan !important;}';
document.body.appendChild(s);
console.debug("Global element appended")
}
}
if(document.styleSheets?.length > 0) {
for (let sheet of document.styleSheets) {
try {
iterateRules(sheet.cssRules)
} catch(e) {
console.warn("WBR: Caught exception: ", e)
}
}
}
}
checkPage()