Smart Dark Mode zzz

Dark mode that avoids inverting already dark sites, except Google, keeps images intact

当前为 2025-12-11 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
  }
})();