Font substitution: Noto Serif CJK JP using local fonts for Android9+

Userscript to use Local JP Serif font on all websites in Android browser for Android 9+.

目前為 2023-12-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           Font substitution: Noto Serif CJK JP using local fonts for Android9+
// @name:ja        フォント置換:Noto Serif CJK JP Android9以降搭載のローカルフォント
// @namespace      https://github.com/yzrsng
// @description    Userscript to use Local JP Serif font on all websites in Android browser for Android 9+.
// @description:ja ウェブページでローカルの日本語明朝体フォントを使うユーザースクリプト。Android 9以降用。
// @version        0.1.20230723.1
// @license        CC0-1.0
// @include        http://*
// @include        https://*
// @match          *://*/*
// @grant          none
// ==/UserScript==

(function() {
  'use strict';

  const setSerifFont = () => {
    // skip Misskey, Calckey, FoundKey?
    const applicationNameElm = document.head.querySelector('meta[name="application-name"][content]');
    if (applicationNameElm) {
      const aName = applicationNameElm.content;
      if (aName === "Misskey" || aName === "Calckey" || aName === "FoundKey" || aName === "Firefish") return;
    }
    const newFontFamilyName = 'Noto Serif CJK JP';
    // const oldFontFamilyNames = [
    //   "Roboto",
    //   "Google Sans",
    //   "Droid Sans",
    //   "MotoyaLMaru",
    //   "MotoyaLCedar",
    //   "Noto Sans JP",
    //   "Noto Sans CJK JP",
    //   "SEC CJK JP",
    //   "Droid Sans Japanese"
    // ]
    const font = new FontFace(newFontFamilyName, `local(${newFontFamilyName})`);
    font.load().then(() => {document.fonts.add(font)});
    const myHead = document.getElementsByTagName('head')[0];
    const myCss = document.createElement('style');
    myCss.id = 'set_serif_font_style';
    myCss.insertAdjacentHTML('beforeend', `
    @font-face {font-family: ${newFontFamilyName}; src: local(${newFontFamilyName});}
    @font-face {font-family: "Roboto"; src: local(${newFontFamilyName});}
    @font-face {font-family: "Google Sans"; src: local(${newFontFamilyName});}
    @font-face {font-family: "Droid Sans"; src: local(${newFontFamilyName});}
    @font-face {font-family: "MotoyaLMaru"; src: local(${newFontFamilyName});}
    @font-face {font-family: "MotoyaLCedar"; src: local(${newFontFamilyName});}
    @font-face {font-family: "Noto Sans JP"; src: local(${newFontFamilyName});}
    @font-face {font-family: "Noto Sans CJK JP"; src: local(${newFontFamilyName});}
    @font-face {font-family: "SEC CJK JP"; src: local(${newFontFamilyName});}
    @font-face {font-family: "Droid Sans Japanese"; src: local(${newFontFamilyName});}
    *:not(pre):not(span):not(code):not(samp){font-family:'USERFONT-${newFontFamilyName}', Charis SIL Compact, Noto Serif CJK JP, Noto Serif, Droid Serif, serif;}
    `);
    myHead.appendChild(myCss);
  };

  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", setSerifFont);
  } else {
    setSerifFont();
  }
})();