使用 HarmonyOS Sans SC 字体,在 Windows 下获得接近苹方的阅读体验

使用本地 HarmonyOS Sans SC 字体,提升不支持苹方的平台(Windows,说的就是你)阅读体验。需要本地安装字体

  1. // ==UserScript==
  2. // @name 使用 HarmonyOS Sans SC 字体,在 Windows 下获得接近苹方的阅读体验
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.2.1
  5. // @description 使用本地 HarmonyOS Sans SC 字体,提升不支持苹方的平台(Windows,说的就是你)阅读体验。需要本地安装字体
  6. // @author CLDXiang
  7. // @website https://github.com/CLDXiang/tampermonkey
  8. // @license MIT
  9. // @match *://*/*
  10. // @exclude *://*bilibili.com/*
  11. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. "use strict";
  16. (() => {
  17. // src/shared/css.ts
  18. function insertStyle(css, key) {
  19. const style = document.createElement("style");
  20. style.innerHTML = css;
  21. if (key)
  22. style.dataset[key] = "";
  23. document.head.appendChild(style);
  24. }
  25. function insertRemovableStyle(css, key) {
  26. const style = document.createElement("style");
  27. style.innerHTML = css;
  28. document.head.appendChild(style);
  29. if (key)
  30. style.dataset[key] = "";
  31. return {
  32. rm: () => style.remove(),
  33. style
  34. };
  35. }
  36.  
  37. // src/use-harmony-font-local/main.mts
  38. var FONT_NAME = "HarmonyOS Sans SC";
  39. var REPLACE_FONT_REGEX = /["']?(system-ui|-apple-system|PingFang SC|SF Pro SC|Microsoft YaHei|ui-sans-serif)["']?/i;
  40. function modifyFontFamily(fontFamily, forceInsert = false) {
  41. if (REPLACE_FONT_REGEX.test(fontFamily) || forceInsert)
  42. return `"${FONT_NAME}", ${fontFamily}`;
  43. return false;
  44. }
  45. var removeTempStyle = () => {
  46. };
  47. var tempStyle = null;
  48. function insertTempStyle() {
  49. const appElement = document.getElementById("app") || document.body;
  50. const currentFontFamily = window.getComputedStyle(appElement).fontFamily;
  51. const fontFamily = modifyFontFamily(currentFontFamily, true);
  52. const { style, rm } = insertRemovableStyle(`.use-harmony-font-mark, html, body, #app, p, textarea, select, input, button, a { font-family: ${fontFamily} !important; } body { font-weight: 400; -webkit-font-smoothing: antialiased; }`, "harmonyFont");
  53. tempStyle = style;
  54. removeTempStyle = rm;
  55. }
  56. if (document.body)
  57. insertTempStyle();
  58. function executeWhenDocumentReady() {
  59. if (!tempStyle)
  60. insertTempStyle();
  61. let newStyleContent = "";
  62. let selectorHasAppElement = false;
  63. for (let i = 0; i < document.styleSheets.length; i++) {
  64. try {
  65. const sheet = document.styleSheets[i];
  66. for (let j = 0; j < sheet.cssRules.length; j++) {
  67. const rule = sheet.cssRules[j];
  68. if (rule.style && rule.style.fontFamily && !rule.selectorText.startsWith(".use-harmony-font-mark")) {
  69. const newFontFamily = modifyFontFamily(rule.style.fontFamily);
  70. if (newFontFamily) {
  71. let css = `font-family: ${newFontFamily};`;
  72. if (rule.style.fontWeight && Number.parseInt(rule.style.fontWeight, 10) < 400)
  73. css += "font-weight: 400;";
  74. newStyleContent += `${rule.selectorText} { ${css} }
  75. `;
  76. if (!selectorHasAppElement && (rule.selectorText.includes("#app") || rule.selectorText.includes("html") || rule.selectorText.includes("body")))
  77. selectorHasAppElement = true;
  78. }
  79. }
  80. }
  81. } catch {
  82. }
  83. }
  84. if (newStyleContent) {
  85. insertStyle(newStyleContent, "harmonyFont");
  86. if (selectorHasAppElement)
  87. removeTempStyle();
  88. }
  89. }
  90. if (document.readyState === "interactive" || document.readyState === "complete")
  91. executeWhenDocumentReady();
  92. else
  93. document.addEventListener("DOMContentLoaded", executeWhenDocumentReady);
  94. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址