Font replacer

Replace fonts hard coded by websites

目前為 2022-01-02 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Font replacer
// @namespace   font-replacer
// @grant       none
// @version     1.0
// @author      Coxxs
// @description Replace fonts hard coded by websites
// @include     *
// @license     MIT
// @run-at      document-start
// ==/UserScript==

function applyCSS(css) {
  if (!document.head) {
    const observer = new MutationObserver((mutations, observer) => {
      if (!document.head) return
      addCSS(css)
      observer.disconnect()
    })
    observer.observe(document.documentElement, { childList: true, subtree: true })
    return
  }
  addCSS(css)
}

function addCSS(css) {
  let style = document.createElement("style")
  style.innerHTML = css
  document.head.prepend(style)
}

class Replacer {
  constructor() {
    this.css = []
  }
  add(from, to) {
    for (const font of to) {
      let extra = []
      if (font.weight) {
        extra += `font-weight:${font.weight};`
      }
      this.css.push(`@font-face{font-family:"${from}";${extra}src:local("${font['name']}")}`)}
    }
  toString() {
    return this.css.join('')
  }
}

let replacer = new Replacer()

/**** Replace rules START *****/

const HarmonyOSSans = [
  { name: 'HarmonyOS Sans SC' },
]
const MiSans = [
  { name: 'MiSans Thin', weight: 100 },
  { name: 'MiSans ExtraLight', weight: 200 },
  { name: 'MiSans Light', weight: 300 },
  { name: 'MiSans Regular', weight: 400 },
  { name: 'MiSans Medium', weight: 500 },
  { name: 'MiSans Demibold', weight: 600 },
  { name: 'MiSans Bold', weight: 700 },
  { name: 'MiSans Heavy', weight: 900 },
]
const SourceHanSans = [
  { name: 'Source Han Sans SC' },
]

const to = SourceHanSans
replacer.add("宋体", to)
replacer.add("SimSun", to)
replacer.add("新宋体", to)
replacer.add("NSimSun", to)
replacer.add("华文中宋", to)
replacer.add("STZhongsong", to)
replacer.add("微软雅黑", to)
replacer.add("Microsoft YaHei", to)
replacer.add("Microsoft JhengHei", to)
replacer.add("黑体", to)
replacer.add("SimHei", to)
replacer.add("华文细黑", to)
replacer.add("STXihei", to)
replacer.add("STHeiTi", to)
replacer.add("幼圆", to)

/**** Replace rules END *****/

applyCSS(replacer.toString())