Dark mode that avoids inverting already dark sites, except Google, keeps images intact
目前為
// ==UserScript==
// @name Smart Dark Mode zzz
// @namespace https://github.com/EastSun5566
// @version 0.1.0
// @description Dark mode that avoids inverting already dark sites, except Google, keeps images intact
// @author Michael Wang + improved
// @license MIT
// @match *://*/*
// @grant none
// ==/UserScript==
(function () {
const excludeDomains = [
'google.com',
'youtube.com',
'maps.google.com'
];
if (excludeDomains.some(d => location.hostname.includes(d))) return;
function getAvgBrightness() {
const elems = document.querySelectorAll('body, body *');
let total = 0, count = 0;
elems.forEach(el => {
const cs = getComputedStyle(el);
const bg = cs.backgroundColor;
if (!bg || bg === 'rgba(0, 0, 0, 0)' || bg === 'transparent') return;
const rgb = bg.match(/\d+/g)?.map(Number) || [];
if (rgb.length === 3) {
total += (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000;
count++;
}
});
if (count === 0) return 255;
return total / count;
}
const avg = getAvgBrightness();
if (avg > 150) {
const style = document.createElement('style');
style.textContent = `
html {
filter: invert(1) hue-rotate(180deg) contrast(1.1) brightness(0.9) !important;
}
img, video, picture, canvas, iframe, embed {
filter: none !important; //
}
`;
document.head.appendChild(style);
}
})();