修改网页复选框颜色

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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');
        }
    });
})();