Remove #FFFFFF background

Removes #FFFFFF background color and optionally generates a userstyle that does the same. Search terms: remove white background, remove CSS, kill CSS, disable CSS, grey theme, dark theme.

目前为 2022-07-02 提交的版本。查看 最新版本

// ==UserScript==
// @name        Remove #FFFFFF background
// @name:hu        #FFFFFF háttér eltávolítása
// @description	Removes #FFFFFF background color and optionally generates a userstyle that does the same. Search terms: remove white background, remove CSS, kill CSS, disable CSS, grey theme, dark theme.
// @description:hu	Eltávolítja az #FFFFFF háttérszínt és opcionálisan generál egy userstyle-t ami ugyanezt csinálja. Search terms: remove white background, remove CSS, kill CSS, disable CSS, grey theme, dark theme.
// Szia, uram!
// Ön az #FFFFFF forradalom küszöbén áll.
// Tessék már észre venni, hogy az #FFFFFF nem ugyanaz, mint a papíron a fehér szín hanem a monitor maximum fényereje. A jó megoldás nem a HDR szabvány hanem egy világosra állított monitor és közepesen világosra állított weboldalak.
// A userstyle generálás akkor hasznos ha egy weboldal nagyú lassan töltődik be vagy fut le rajta a scipt vagy ha kézzel bele akarsz nyúlni a style-ba. A kód első sorában kell bekapcsolni.
// A kód elején van egy kikommentelt timeout is, ez arra jó, hogy késleltesse a script lefutását a lassan (több lépésben) betöltődő weboldalaknál. 

// @version     11.3
// @grant       none
// @include       *
// @license	CC-0
// @namespace	me.adventuretc
// @homepage	https://gf.qytechs.cn/hu/scripts/445216-remove-ffffff-background/
// ==/UserScript==


//Ismert bugok: 
//	A css kimenetben az osztálynevek közé kéne tegyen egy pontot, de nem tesz. Most ezt írja ki: ".class class class", de ezt kéne: ".class .class .class"

var generateCssOutput = false;


main();
// Rájöttem, hogy ha csak 1x futtatom le és nem 2x, akkor sokkal kevesebb idő betölteni bizonyos honlapokat, pl. Alza, Viszlaysport. Azt hiszem, az az oka, hogy az első körben rámegy egy csomó attribútum az elemekre és a második körben ezeket mind újra feldolgozza a script. Ezt úgy lehetne kikerülni, hogy megcímkézem vagy kikerülöm valahogy azokat amiket már módosítottam.

//https://stackoverflow.com/questions/3586775/what-is-the-correct-way-to-check-for-string-equality-in-javascript
if (window.location.host.trim() == "telex.hu")
{
	setTimeout(function () { 
			main(); 
		}, 3000);
}
if (window.location.host.trim() == "www.messenger.com")
{
	setTimeout(function () { 
			main(); 
		}, 10000);
}
//setTimeout(function () { main(); }, 2000);

function main() {
	
	var cssOutput = "";
	
	var fixedTags = ["main", "article", "body", "div", "span", "html", "textarea", "input", "select", "code", "pre", "section", "li", "ul", "nav", "a", "table"];

	for (var i = 0; i < fixedTags.length; ++i) {
		var v = fixedTags[i];


		for (var y = 0; y < gettag(v).length; ++y) {
			if (!gettag(v)[y])
			{
				continue;
			}

			// element to be observed
			var observed = gettag(v)[y];
			// element to be changed
			var changed = gettag(v)[y];

			var style = window.getComputedStyle(observed);
			//var defaultStyle = window.getDefaultComputedStyle(observed);

			var backgroundColor = style.getPropertyValue("background-color");
			//var defaultBackgroundColor = defaultStyle.getPropertyValue("background-color");


            /*if (observed.classList.contains('mw-body'))
            {
                alert(backgroundColor);
            }*/


			if (backgroundColor === "transparent" && hasStyledBackground(observed) == false) {
				// Kapjuk meg az első színezett szülejét.
				var parent = findUpColoredBackground(observed); //find first parent with color

				// Ha volt színezett szülő.
				if (parent) {
					observed = parent;
				}
				else {
					continue;
				}
			}

			style = window.getComputedStyle(observed);
			//defaultStyle = window.getDefaultComputedStyle(observed);

			backgroundColor = style.getPropertyValue("background-color");
			//defaultBackgroundColor = defaultStyle.getPropertyValue("background-color");

			if (average(rgbArray(backgroundColor)) >= 230) {

				// All links should be reset to color default.
				if (fixedTags[i] == "a") {
					changed.style.color = "-moz-hyperlinktext";

				}
			}
			// debug:
			//console.log(backgroundColor);

			if (hasStyledBackground(observed) && average(rgbArray(backgroundColor)) >= 230) //webpage specified some background and its white
			{
				// Mozilla Color Preference Extensions
				// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value

				changed.style.backgroundColor = "-moz-default-background-color";


				changed.style.color = "-moz-default-color";
				
				
				 if (generateCssOutput)
				 {
					//window.alert(changed.tagName);
					//window.alert(changed.id);
					//window.alert(changed.className);
					
					var selectorText = "";
					
					if (changed.id)
					{
						selectorText += `#${changed.id}`
					}
					else if (changed.className)
					{
						var classes = changed.className.replaceAll(" "," .");
						selectorText += `.${classes}`
						
					}
					else if (changed.tagName)
					{
						selectorText += `${changed.tagName}`
						
					}
					
					
					var text = `${selectorText}
{
	background-color: unset !important;
	color: unset !important;
}`

					// I see duplicates. Remove them.
					if (cssOutput.includes(text) == false)
					{
						cssOutput = cssOutput + "\n"+ text;
					}
				 }



				//if (style.color === defaultStyle.color) //but not foreground
				//{
				//if ( average( rgbArray(backgroundColor) ) > 127  )
				//{
				//changed.style.color = "#000";
				//}
				//else
				//{
				//changed.style.color = "#FFF";
				//}

				//if (backgroundColor === "transparent")
				//{
				//changed.style.color = "#000";
				//}
				//}
			}

		}
	}
	
	 if (generateCssOutput)
	 {
		window.alert("paste this text into a blank userstyle:\n" +cssOutput);
	 }

}

function average(someArr) {
	var sum = 0;

	for (var i = 0; i < someArr.length; ++i) {
		sum += parseInt(someArr[i]);
	}

	return (sum / someArr.length);
}

function rgbArray(rgb) {

	rgb = rgb.replace(/[^\d,]/g, '').split(',');

	return rgb;
}

function gettag(s) {
	return document.getElementsByTagName(s);
}

function findUpColoredBackground(el) {
	while (el.parentNode) {
		el = el.parentNode;
		
		if (hasStyledBackground(el)) //webpage specified some background
		{
			var style = window.getComputedStyle(el);
			var backgroundColor = style.getPropertyValue("background-color");
			if (backgroundColor !== "transparent") {
				 
				return el;
			}
		}
	}
	return null;
}

function hasStyledBackground(element) {
	var style = window.getComputedStyle(element);
	var defaultStyle = window.getDefaultComputedStyle(element);

	var backgroundColor = style.getPropertyValue("background-color");
	var defaultBackgroundColor = defaultStyle.getPropertyValue("background-color");

	var backgroundImage = style.getPropertyValue("background-image");
	var defaultBackgroundImage = defaultStyle.getPropertyValue("background-image");

	if (backgroundColor !== defaultBackgroundColor) {
		return true;
	}

	if (backgroundImage !== defaultBackgroundImage) {
		return true;
	}

	return false;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址