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+.

  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.20240223.3
  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. const misskey_forks = [
  24. "Misskey",
  25. "Calckey",
  26. "FoundKey",
  27. "Firefish",
  28. "Iceshrimp",
  29. "Sharkey",
  30. "CherryPick",
  31. "Tanukey",
  32. ];
  33. for (const fork of misskey_forks) {
  34. if (aName === fork) return;
  35. }
  36. }
  37. const newFontFamilyName = 'Noto Serif CJK JP';
  38. // const oldFontFamilyNames = [
  39. // "Roboto",
  40. // "Google Sans",
  41. // "Droid Sans",
  42. // "MotoyaLMaru",
  43. // "MotoyaLCedar",
  44. // "Noto Sans JP",
  45. // "Noto Sans CJK JP",
  46. // "SEC CJK JP",
  47. // "Droid Sans Japanese"
  48. // ]
  49. const font = new FontFace(newFontFamilyName, `local(${newFontFamilyName})`);
  50. font.load().then(() => {document.fonts.add(font)});
  51. const myHead = document.getElementsByTagName('head')[0];
  52. const myCss = document.createElement('style');
  53. myCss.id = 'set_serif_font_style';
  54. myCss.insertAdjacentHTML('beforeend', `
  55. @font-face {font-family: ${newFontFamilyName}; src: local(${newFontFamilyName});}
  56. @font-face {font-family: "Roboto"; src: local(${newFontFamilyName});}
  57. @font-face {font-family: "Google Sans"; src: local(${newFontFamilyName});}
  58. @font-face {font-family: "Droid Sans"; src: local(${newFontFamilyName});}
  59. @font-face {font-family: "MotoyaLMaru"; src: local(${newFontFamilyName});}
  60. @font-face {font-family: "MotoyaLCedar"; src: local(${newFontFamilyName});}
  61. @font-face {font-family: "Noto Sans JP"; src: local(${newFontFamilyName});}
  62. @font-face {font-family: "Noto Sans CJK JP"; src: local(${newFontFamilyName});}
  63. @font-face {font-family: "SEC CJK JP"; src: local(${newFontFamilyName});}
  64. @font-face {font-family: "Droid Sans Japanese"; src: local(${newFontFamilyName});}
  65. *:not(pre):not(span):not(code):not(samp){font-family:'USERFONT-${newFontFamilyName}', Charis SIL Compact, Noto Serif CJK JP, Noto Serif, Droid Serif, serif;}
  66. `);
  67. myHead.appendChild(myCss);
  68. };
  69.  
  70. if (document.readyState === "loading") {
  71. document.addEventListener("DOMContentLoaded", setSerifFont);
  72. } else {
  73. setSerifFont();
  74. }
  75. })();