Replace Monospace Font Family With Brower Default Font

Replace Monospace Font Family With Brower Default Font In All Webpage

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

  1. // ==UserScript==
  2. // @name Replace Monospace Font Family With Brower Default Font
  3. // @namespace https://github.com/yahweh042
  4. // @version 1
  5. // @description Replace Monospace Font Family With Brower Default Font In All Webpage
  6. // @author Merlin Hsu
  7. // @match *://*/*
  8. // @grant GM_addStyle
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. "use strict";
  14.  
  15. // add tag css
  16. const css = `code, pre { font-family: monospace !important; }`
  17. GM_addStyle(css)
  18.  
  19. const pattern = new RegExp("(monospace|SourceCodeProMac)");
  20. const monospaceFont = "monospace";
  21. const observer = new MutationObserver(() => {
  22. // console.log("Replace Font Family Begin")
  23. const elements = document.querySelectorAll("*");
  24. // console.log(`elements length = ${elements.length}`)
  25. for (const element of elements) {
  26. let style = window.getComputedStyle(element);
  27. let fontFamily = style.getPropertyValue("font-family");
  28. if (pattern.test(fontFamily)) {
  29. element.style.fontFamily = monospaceFont;
  30. }
  31. let font = style.getPropertyValue("font");
  32. if (pattern.test(font) !== -1) {
  33. element.style.font = monospaceFont;
  34. }
  35. }
  36. });
  37. const body = document.body;
  38. observer.observe(body, {
  39. childList: true,
  40. subtree: true,
  41. });
  42. })();