Greasy Fork

Greasy Fork is available in English.

钦定字体

指定网页的 UI 字体和代码字体,忽略图标

当前为 2025-09-05 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         钦定字体
// @description  指定网页的 UI 字体和代码字体,忽略图标
// @license      MIT
// @namespace    http://tampermonkey.net/
// @version      0.6
// @author       chenh
// @match        *://*/*
// @grant        GM_addStyle
// @run-at       document-start
// ==/UserScript==

;(() => {
  'use strict'

  // UI 字体
  const UI_FONT = 'Microsoft YaHei'
  // 代码字体
  const CODE_FONT = 'Consolas'

  // 忽略项,用于忽略字体图标
  const GLOBAL_IGNORES = [
    'i',
    '[class*=ifont]',
    '[class*=ifont] *',
    '[class*=vjs]',
    '[class*=vjs] *',
    '[class*=icon]',
    '[class*=icon] *',
    '[class*=photo]',
    '[class*=photo] *',
    '[class*=image]',
    '[class*=image] *',
    '[class*=picture]',
    '[class*=picture] *',
  ]

  // UI 选择器
  const UI_SELECTORS = ['*']
  // 代码选择器
  const CODE_SELECTORS = [
    'pre',
    'pre *',
    'code',
    'code *',
    '.diff-viewer',
    '.diff-viewer *',
    '[id*=highlighted]',
    '[id*=highlighted] *',
    '[class*=highlighted]',
    '[class*=highlighted] *',
  ]

  /**
   * 将选择器和忽略结合成选择器
   * @param {string[]} selectors 普通选择器
   * @param {string[]} ignores 忽略项选择器
   * @returns {string} 选择器字符串
   */
  function buildIgnoreStyle(selectors, ignores) {
    return selectors.map(sel => `${sel}:not(${ignores.join(', ')})`).join(', ')
  }

  // 设置 CSS
  GM_addStyle(`
    ${buildIgnoreStyle(UI_SELECTORS, [...CODE_SELECTORS, ...GLOBAL_IGNORES])} {
      font-family: '${UI_FONT}' !important;
    }

    ${buildIgnoreStyle(CODE_SELECTORS, GLOBAL_IGNORES)} {
      font-family: '${CODE_FONT}' !important;
      -webkit-font-feature-settings : "calt" on;
    }
  `)
})()