Font Size Controller

Control the font size of any webpage with a slider.

  1. // ==UserScript==
  2. // @name Font Size Controller
  3. // @description Control the font size of any webpage with a slider.
  4. // @version 1.1.3
  5. // @namespace https://github.com/amazing-fish
  6. // @grant GM_setValue
  7. // @grant GM_getValue
  8. // @license MIT
  9. // @include *
  10. // ==/UserScript==
  11.  
  12.  
  13.  
  14. window.addEventListener('load', function() {
  15. var isControllerEnabled = GM_getValue("isControllerEnabled", false);
  16.  
  17. // Function to create a button
  18. function createButton(id, text, left, onclick, display = 'none') {
  19. var button = document.createElement('button');
  20. button.textContent = text;
  21. button.style = `position: fixed; top: 3px; background: rgba(0, 0, 0, 0.2); color: #fff; padding: 0px; font-size: 12px; left: ${left}px; z-index: 9999; display: ${display}; border: none`;
  22. button.id = id;
  23. button.onclick = onclick;
  24. return button;
  25. }
  26.  
  27. // Create slider element
  28. var slider = document.createElement('input');
  29. slider.type = 'range';
  30. slider.min = '10';
  31. slider.max = '32';
  32. slider.value = GM_getValue("fontSize", '16');
  33. slider.id = 'fontSlider';
  34. slider.style = 'position: fixed; top: 0; left: 0; z-index: 9999; display: none';
  35.  
  36. // Create font size display element
  37. var fontSizeDisplay = document.createElement('div');
  38. fontSizeDisplay.id = 'fontSizeDisplay';
  39. fontSizeDisplay.textContent = 'Font Size: ' + slider.value + 'px';
  40. fontSizeDisplay.style = 'position: fixed; top: 3px; left: 140px; z-index: 9999; font-size: 10px; display: none';
  41.  
  42. // Append slider and display to body
  43. document.body.appendChild(slider);
  44. document.body.appendChild(fontSizeDisplay);
  45.  
  46. // Function to update font sizes
  47. function updateFontSizes() {
  48. var all = document.getElementsByTagName("*");
  49. for (var i=0, max=all.length; i < max; i++) {
  50. // Exclude the control buttons
  51. if (!(all[i].id === 'fontSizeDisplay' || all[i].id === 'fontSlider' || all[i].id === 'enableButton' || all[i].id === 'disableButton' || all[i].id === 'expandButton' || all[i].id === 'hideButton')) {
  52. all[i].style.fontSize = slider.value + "px";
  53. }
  54. }
  55. }
  56.  
  57. // Create tooltip element
  58. var tooltip = document.createElement('div');
  59. tooltip.id = 'sliderTooltip';
  60. tooltip.style = 'position: fixed; top: 25px; left: 0; z-index: 10000; background: rgba(0, 0, 0, 0.5); color: #fff; padding: 1px; font-size: 5px; display: none';
  61. document.body.appendChild(tooltip);
  62.  
  63. // Event listener for slider
  64. slider.onmousemove = function(event) {
  65. fontSizeDisplay.textContent = 'Font Size: ' + this.value + 'px';
  66. tooltip.textContent = this.value;
  67. tooltip.style.left = event.pageX + 'px';
  68. tooltip.style.display = 'block';
  69. if (isControllerEnabled) {
  70. updateFontSizes();
  71. GM_setValue("fontSize", this.value);
  72. }
  73. tooltip.style.fontSize = '10px'
  74.  
  75. }
  76.  
  77. slider.onchange = function() {
  78. if (isControllerEnabled) {
  79. updateFontSizes();
  80. GM_setValue("fontSize", this.value);
  81. }
  82. }
  83.  
  84. // Hide tooltip when mouse is not over the slider
  85. slider.onmouseout = function() {
  86. tooltip.style.display = 'none';
  87. }
  88.  
  89. // Create buttons
  90. var expandButton = createButton('expandButton', '展开', 3, function() {
  91. ['fontSlider', 'fontSizeDisplay', 'hideButton'].forEach(function(id) {
  92. var element = document.getElementById(id);
  93. if(element) element.style.display = 'block';
  94. });
  95.  
  96. if (isControllerEnabled) {
  97. document.getElementById('disableButton').style.display = 'block';
  98. } else {
  99. document.getElementById('enableButton').style.display = 'block';
  100. }
  101.  
  102. this.style.display = 'none';
  103. }, 'block');
  104.  
  105. var hideButton = createButton('hideButton', '收起', 250, function() {
  106. ['fontSlider', 'fontSizeDisplay', 'hideButton', 'disableButton', 'enableButton'].forEach(function(id) {
  107. var element = document.getElementById(id);
  108. if(element) element.style.display = 'none';
  109. });
  110. document.getElementById('expandButton').style.display = 'block';
  111. });
  112.  
  113. var enableButton = createButton('enableButton', '开启', 220, function() {
  114. isControllerEnabled = true;
  115. GM_setValue("isControllerEnabled", isControllerEnabled);
  116. updateFontSizes();
  117. this.style.display = 'none';
  118. document.getElementById('disableButton').style.display = 'block';
  119. });
  120.  
  121. var disableButton = createButton('disableButton', '关闭', 220, function() {
  122. isControllerEnabled = false;
  123. GM_setValue("isControllerEnabled", isControllerEnabled);
  124. this.style.display = 'none';
  125. document.getElementById('enableButton').style.display = 'block';
  126. });
  127.  
  128. // Append buttons to body
  129. [expandButton, hideButton, enableButton, disableButton].forEach(function(button) {
  130. document.body.appendChild(button);
  131. });
  132.  
  133. // Initial states
  134. if (isControllerEnabled) updateFontSizes();
  135. });

QingJ © 2025

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