Font 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-01-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

(function() {
  'use strict';

  const setSerifFont = () => {
    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();
  }
})();