修改网页复选框颜色

修改所有网页文字与复选框颜色,兼容 Trusted Types 安全策略

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         修改网页复选框颜色
// @namespace    https://greasyfork.org/zh-CN/scripts/461245
// @version      1.0.1
// @description  修改所有网页文字与复选框颜色,兼容 Trusted Types 安全策略
// @author       nosora
// @match        *://*/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function addGlobalStyle(css) {
        const head = document.head || document.getElementsByTagName('head')[0];
        if (!head) return;
        const style = document.createElement('style');
        style.type = 'text/css';
        // ✅ 使用 textContent 代替 innerHTML,避免 TrustedHTML 报错
        style.textContent = css;
        head.appendChild(style);
    }

    document.addEventListener('DOMContentLoaded', function() {
        // 修改选中框的背景色 & 选中文字的颜色
        const selectionColor = 'rgba(0, 0, 0, 0.05)';
        const fontColor = '#6FB7FF';

        // 添加全局样式
        addGlobalStyle(`
            ::selection {
                background-color: ${selectionColor} !important;
                color: ${fontColor} !important;
            }

            input[type="checkbox"],
            input[type="radio"] {
                accent-color: ${fontColor} !important;
            }
        `);

        // 强制覆盖已有的 checkbox / radio
        const elementsToOverride = document.querySelectorAll('body input[type="checkbox"], body input[type="radio"]');
        for (const el of elementsToOverride) {
            el.style.setProperty('background-color', selectionColor, 'important');
            el.style.setProperty('color', fontColor, 'important');
        }
    });
})();