Replace Monospace Font Family With Brower Default Font

Replace Monospace Font Family With Brower Default Font In All Webpage

  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 (fontFamily === monospaceFont) {
  29. continue;
  30. }
  31. if (pattern.test(fontFamily)) {
  32. element.style.fontFamily = monospaceFont;
  33. }
  34. let font = style.getPropertyValue("font");
  35. if (pattern.test(font) !== -1) {
  36. element.style.font = monospaceFont;
  37. }
  38. }
  39. });
  40. const body = document.body;
  41. observer.observe(body, {
  42. childList: true,
  43. subtree: true,
  44. });
  45. })();