Greasy Fork

强制系统字体

让网页强制使用系统字体

当前为 2025-03-02 提交的版本,查看 最新版本

// ==UserScript==
// @name         强制系统字体
// @name:en      System Fonts
// @name:zh-CN   强制系统字体
// @name:zh-TW   強制系統字體
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  让网页强制使用系统字体
// @description:en  Let web pages force the use of system fonts
// @description:zh-CN    让网页强制使用系统字体
// @description:zh-TW    讓網頁強制使用系統字體
// @author       Deepseek
// @match        *://*/*
// @grant        GM_addStyle
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 基础排除规则
    const baseExcludes = [
        'input', 'textarea', 'pre', 'code', 'img','svg','audio','video',
        '[class*="icon"]', '[class*="logo"]',
        '[id*="logo"]', '[id*="icon"]'
    ];

    // 针对Google域名的增强保护
    if (location.hostname.match(/\.google\./)) {
        baseExcludes.push(
            'div[aria-label*="Google"]',
            '[class*="gb_"]',
            '[font-family*="Google Sans"]'
        );
    }

    const excludeSelectors = baseExcludes.join(', ');

    // 核心样式规则
    GM_addStyle(`
        *:not(${excludeSelectors}) {
            font-family: system-ui, sans-serif !important;
        }

        /* Google Logo保留 */
        [class*="gb_"] > svg,
        .logo-subtext,
        [aria-label*="Google"] span {
            font-family: 'Google Sans', system-ui !important;
        }
    `);

    // 智能DOM监听器
    const observer = new MutationObserver(mutations => {
        mutations.forEach(({ addedNodes }) => {
            addedNodes.forEach(node => {
                if (node.nodeType === 1) {
                    node.querySelectorAll('*').forEach(element => {
                        const isExcluded = baseExcludes.some(selector => 
                            element.matches(selector)
                        );
                        
                        // 动态检测Google元素
                        const isGoogleBrand = element.closest('[aria-label*="Google"]') || 
                                            element.matches('[font-family*="Google Sans"]');
                        
                        if (!isExcluded && !isGoogleBrand) {
                            element.style.fontFamily = 'system-ui, sans-serif';
                        }
                    });
                }
            });
        });
    });

    observer.observe(document, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['class', 'id', 'aria-label']
    });
})();