Force Microsoft YaHei Font (No-Flash Safe)

无闪烁强制微软雅黑,安全排除图标/公式/控件,站点/页面级排除在脚本头完成

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Force Microsoft YaHei Font (No-Flash Safe)
// @namespace    https://tampermonkey.net/
// @version      1.2.1
// @description  无闪烁强制微软雅黑,安全排除图标/公式/控件,站点/页面级排除在脚本头完成
// @match        *://*/*

// ===== 站点 / 页面排除 =====
// @exclude-match  *://www.google.com/*
// @exclude-match  *://accounts.google.com/*
// @exclude-match  *://docs.google.com/*
// @exclude-match  *://example.com/special/page

// ===== 运行时机 =====
// @run-at       document-start
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const FONT_FALLBACK = [
        '"Microsoft YaHei"',
        '"微软雅黑"',
        '"PingFang SC"',
        'system-ui',
        'sans-serif'
    ].join(',');

    const EXCLUDE_SELECTORS = `
        code, pre, kbd, samp, tt,
        input, textarea, select, option, button,
        [role="button"], [type="button"], [type="submit"], [type="reset"],

        i, i[class],
        svg, svg *, canvas,
        [class*="icon"], [class^="icon-"], [class*="glyph"],
        [class^="fa"], .fa, .fas, .far, .fab, .fal, .fad,
        .material-icons, .material-symbols-outlined,
        .material-symbols-rounded, .material-symbols-sharp,
        .iconfont, [class*="iconfont"],

        math, mjx-container, .MathJax,
        .katex, .katex *,

        .monaco-editor, .monaco-editor *,
        .CodeMirror, .CodeMirror *,
        .ace_editor, .ace_editor *,

        nav, nav *, menu, menu *,
        [role="menu"], [role="menuitem"],
        [role="toolbar"], [role="tab"], [role="tabpanel"],

        progress, meter, summary, details,
        [draggable="true"],
        [aria-hidden="true"]
    `.replace(/\s+/g, ' ');

    const style = document.createElement('style');
    style.id = 'tm-force-yahei-font';
    style.textContent = `
        html, body {
            font-family: ${FONT_FALLBACK} !important;
        }
        body *:not(${EXCLUDE_SELECTORS}) {
            font-family: ${FONT_FALLBACK} !important;
        }
    `;

    document.documentElement.appendChild(style);
})();