replace-font-sizes

Replace all fonts sizes by preferred ones.

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

  1. // ==UserScript==
  2. // @name replace-font-sizes
  3. // @namespace http://tampermonkey.net/
  4. // @version 202312190930
  5. // @description Replace all fonts sizes 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 fixedWidthFonts = [
  16. 'Monospace',
  17. 'Courier',
  18. 'Courier New',
  19. 'Consolas',
  20. 'Monaco',
  21. 'Menlo',
  22. 'Fire Mono',
  23. 'Liberation Mono',
  24. 'Monospace',
  25. 'Noto Mono',
  26. 'Roboto Mono'
  27. ]
  28.  
  29. var domainFontSettings = [
  30. {
  31. domain: 'twitter.com',
  32. minFontSize: 18,
  33. maxFontSize: 18,
  34. minFixedWidthFontSize: 18,
  35. maxFixedWidthFontSize: 18,
  36. lineHeight: 1.2,
  37. fixedWidthLineHeight: 1.2
  38. },
  39. {
  40. domain: 'github.com',
  41. minFontSize: 17,
  42. maxFontSize: 24,
  43. minFixedWidthFontSize: 17,
  44. maxFixedWidthFontSize: 17,
  45. lineHeight: 1.2,
  46. fixedWidthLineHeight: 1.2
  47. },
  48. {
  49. domain: 'google.com',
  50. minFontSize: 16,
  51. maxFontSize: 22,
  52. minFixedWidthFontSize: 16,
  53. maxFixedWidthFontSize: 22,
  54. lineHeight: 0,
  55. fixedWidthLineHeight: 0
  56. },
  57. {
  58. domain: 'openai.com',
  59. minFontSize: 18,
  60. maxFontSize: 18,
  61. minFixedWidthFontSize: 18,
  62. maxFixedWidthFontSize: 18,
  63. lineHeight: 1.4,
  64. fixedWidthLineHeight: 1.2
  65. }
  66. ]
  67.  
  68. function adjustFontSize (node) {
  69. let currentDomain = window.location.hostname
  70. let fontFamily = window.getComputedStyle(node).fontFamily.toLowerCase()
  71. let fontSize = parseFloat(window.getComputedStyle(node).fontSize)
  72. let isFixedWidth = fixedWidthFonts.some(font =>
  73. fontFamily.includes(font.toLowerCase())
  74. )
  75.  
  76. let applicableSettings = domainFontSettings.find(setting =>
  77. currentDomain.includes(setting.domain)
  78. )
  79. if (!applicableSettings) return
  80.  
  81. let newFontSize = 0
  82. let newLineHeight = 0
  83.  
  84. let {
  85. minFontSize,
  86. maxFontSize,
  87. minFixedWidthFontSize,
  88. maxFixedWidthFontSize,
  89. lineHeight,
  90. fixedWidthLineHeight
  91. } = applicableSettings
  92.  
  93. if (isFixedWidth) {
  94. if (fontSize < minFixedWidthFontSize) {
  95. newFontSize = minFixedWidthFontSize
  96. }
  97. if (fontSize > maxFixedWidthFontSize) {
  98. newFontSize = maxFixedWidthFontSize
  99. }
  100. if (fixedWidthLineHeight) {
  101. newLineHeight = fixedWidthLineHeight
  102. }
  103. } else {
  104. if (fontSize < minFontSize) {
  105. newFontSize = minFontSize
  106. }
  107. if (fontSize > maxFontSize) {
  108. newFontSize = maxFontSize
  109. }
  110. if (lineHeight) {
  111. newLineHeight = lineHeight
  112. }
  113. }
  114.  
  115. if (newFontSize) {
  116. node.style.fontSize = `${newFontSize}px`
  117. }
  118. if (newLineHeight) {
  119. node.style.lineHeight = `${newLineHeight}em`
  120. }
  121. }
  122.  
  123. function observeDOMChanges () {
  124. const observer = new MutationObserver(mutations => {
  125. mutations.forEach(mutation => {
  126. mutation.addedNodes.forEach(node => {
  127. if (node.nodeType === Node.ELEMENT_NODE) {
  128. adjustFontSize(node)
  129. node.querySelectorAll('*').forEach(adjustFontSize)
  130. }
  131. })
  132. if (mutation.type === 'attributes') {
  133. adjustFontSize(mutation.target)
  134. }
  135. })
  136. })
  137. observer.observe(document, {
  138. childList: true,
  139. subtree: true,
  140. attributes: true
  141. })
  142. }
  143.  
  144. window.addEventListener('load', () => {
  145. document.querySelectorAll('*').forEach(adjustFontSize)
  146. observeDOMChanges()
  147. })
  148.  
  149. document.addEventListener('DOMContentLoaded', () => {
  150. document.querySelectorAll('*').forEach(adjustFontSize)
  151. })
  152. })()