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-04-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Font substitution: Noto Serif CJK JP using local fonts for Android9+
  3. // @name:ja フォント置換:Noto Serif CJK JP Android9以降搭載のローカルフォント
  4. // @namespace https://github.com/yzrsng
  5. // @description Userscript to use Local JP Serif font on all websites in Android browser for Android 9+.
  6. // @description:ja ウェブページでローカルの日本語明朝体フォントを使うユーザースクリプト。Android 9以降用。
  7. // @version 0.1.20230408.1
  8. // @license CC0-1.0
  9. // @include http://*
  10. // @include https://*
  11. // @match *://*/*
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. const setSerifFont = () => {
  19. // skip Misskey, Calckey, FoundKey?
  20. const applicationNameElm = document.head.querySelector('meta[name="application-name"][content]');
  21. if (applicationNameElm) {
  22. const aName = applicationNameElm.content;
  23. if (aName === "Misskey" || aName === "Calckey" || aName === "FoundKey") return;
  24. }
  25. const newFontFamilyName = 'Noto Serif CJK JP';
  26. // const oldFontFamilyNames = [
  27. // "Roboto",
  28. // "Google Sans",
  29. // "Droid Sans",
  30. // "MotoyaLMaru",
  31. // "MotoyaLCedar",
  32. // "Noto Sans JP",
  33. // "Noto Sans CJK JP",
  34. // "SEC CJK JP",
  35. // "Droid Sans Japanese"
  36. // ]
  37. const font = new FontFace(newFontFamilyName, `local(${newFontFamilyName})`);
  38. font.load().then(() => {document.fonts.add(font)});
  39. const myHead = document.getElementsByTagName('head')[0];
  40. const myCss = document.createElement('style');
  41. myCss.id = 'set_serif_font_style';
  42. myCss.insertAdjacentHTML('beforeend', `
  43. @font-face {font-family: ${newFontFamilyName}; src: local(${newFontFamilyName});}
  44. @font-face {font-family: "Roboto"; src: local(${newFontFamilyName});}
  45. @font-face {font-family: "Google Sans"; src: local(${newFontFamilyName});}
  46. @font-face {font-family: "Droid Sans"; src: local(${newFontFamilyName});}
  47. @font-face {font-family: "MotoyaLMaru"; src: local(${newFontFamilyName});}
  48. @font-face {font-family: "MotoyaLCedar"; src: local(${newFontFamilyName});}
  49. @font-face {font-family: "Noto Sans JP"; src: local(${newFontFamilyName});}
  50. @font-face {font-family: "Noto Sans CJK JP"; src: local(${newFontFamilyName});}
  51. @font-face {font-family: "SEC CJK JP"; src: local(${newFontFamilyName});}
  52. @font-face {font-family: "Droid Sans Japanese"; src: local(${newFontFamilyName});}
  53. *:not(pre):not(span):not(code):not(samp){font-family:'USERFONT-${newFontFamilyName}', Charis SIL Compact, Noto Serif CJK JP, Noto Serif, Droid Serif, serif;}
  54. `);
  55. myHead.appendChild(myCss);
  56. };
  57.  
  58. if (document.readyState === "loading") {
  59. document.addEventListener("DOMContentLoaded", setSerifFont);
  60. } else {
  61. setSerifFont();
  62. }
  63. })();