Git-SCM Background Color Changer

Dynamically replace the background color of Git-SCM pages based on the system theme

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Git-SCM Background Color Changer
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Dynamically replace the background color of Git-SCM pages based on the system theme
// @author       mcwindy
// @match        https://git-scm.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  const darkModeBg = '#1B1B1B'; // for dark mode
  const lightModeBg = '#fff'; // for light mode

  // Detection system theme
  const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;

  const bgColor = isDarkMode ? darkModeBg : lightModeBg;

  const bodyElement = document.querySelector('body');
  if (bodyElement) {
    bodyElement.style.background = bgColor;
  }

  // Replace the background attribute in all style sheets
  const styleSheets = document.styleSheets;
  for (let i = 0; i < styleSheets.length; i++) {
    const styleSheet = styleSheets[i];
    try {
      const cssRules = styleSheet.cssRules || styleSheet.rules;
      for (let j = 0; j < cssRules.length; j++) {
        const rule = cssRules[j];
        if (rule.selectorText === 'body' && rule.style.background) {
          rule.style.background = bgColor;
        }
      }
    } catch (e) {
      // Cross-domain style sheet cannot be accessed, ignored
    }
  }
})();