replace-fonts

Replace all fonts by preferred ones.

目前为 2023-12-19 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name replace-fonts
  3. // @namespace http://tampermonkey.net/
  4. // @version 202312190930
  5. // @description Replace all fonts by preferred ones.
  6. // @author Rafael David Tinoco
  7. // @match http*://*/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. ;(function () {
  13. 'use strict'
  14.  
  15. var fontsToOpenSans = [
  16. 'Calibri',
  17. 'Cambria',
  18. 'Candara',
  19. 'Constantia',
  20. 'Corbel',
  21. 'Georgia',
  22. 'Segoe UI',
  23. 'Trebuchet MS',
  24. 'Verdana',
  25. 'sans',
  26. 'sans-serif',
  27. 'serif',
  28. 'Avenir',
  29. 'Avenir Next',
  30. 'Comic Sans MS',
  31. 'Comic Sans',
  32. 'Lucida Grande',
  33. 'Lucida Sans',
  34. 'Roboto'
  35. ]
  36. var fontsToCousine = [
  37. 'Monospace',
  38. 'Noto Mono',
  39. 'Consolas',
  40. 'Courier New',
  41. 'Courier',
  42. 'Monaco',
  43. 'Menlo',
  44. 'Fira Mono',
  45. 'Liberation Mono',
  46. 'Roboto Mono'
  47. ]
  48. var fontsToLiberationSans = [
  49. 'Arial',
  50. 'Times New Roman',
  51. 'Arial Narrow',
  52. 'Tahoma',
  53. 'Helvetica',
  54. 'Helvetica Neue',
  55. 'San Francisco',
  56. 'Fira Sans',
  57. 'Noto Sans'
  58. ]
  59.  
  60. function adjustElementStyles (node) {
  61. let fontFamily = window
  62. .getComputedStyle(node)
  63. .fontFamily.replace(/["']/g, '')
  64. let site = window.location.hostname
  65.  
  66. fontFamily.split(',').some(font => {
  67. if (fontsToOpenSans.includes(font.trim())) {
  68. node.style.fontFamily = '"Open Sans", sans-serif'
  69. return true
  70. } else if (fontsToCousine.includes(font.trim())) {
  71. node.style.fontFamily = 'Cousine, monospace'
  72. return true
  73. } else if (fontsToLiberationSans.includes(font.trim())) {
  74. node.style.fontFamily = '"Liberation Sans", sans-serif'
  75. return true
  76. }
  77. return false
  78. })
  79. }
  80.  
  81. function observeDOMChanges () {
  82. const observer = new MutationObserver(mutations => {
  83. mutations.forEach(mutation => {
  84. mutation.addedNodes.forEach(node => {
  85. if (node.nodeType === Node.ELEMENT_NODE) {
  86. adjustElementStyles(node)
  87. node.querySelectorAll('*').forEach(adjustElementStyles)
  88. }
  89. })
  90. })
  91. })
  92. observer.observe(document, { childList: true, subtree: true })
  93. }
  94.  
  95. window.addEventListener('load', () => {
  96. document.querySelectorAll('*').forEach(adjustElementStyles)
  97. observeDOMChanges()
  98. })
  99.  
  100. document.addEventListener('DOMContentLoaded', () => {
  101. document.querySelectorAll('*').forEach(adjustElementStyles)
  102. })
  103. })()