Font Size Controller

Control the font size of any webpage with a slider.

目前为 2023-11-06 提交的版本。查看 最新版本

// ==UserScript==
// @name     Font Size Controller
// @description   Control the font size of any webpage with a slider.
// @version  1.1.1
// @namespace https://github.com/amazing-fish
// @grant    GM_setValue
// @grant    GM_getValue
// @license  MIT
// @include  *
// ==/UserScript==


window.addEventListener('load', function() {
  var isControllerEnabled = GM_getValue("isControllerEnabled", false);

  // Function to create a button
  function createButton(id, text, left, onclick, display = 'none') {
    var button = document.createElement('button');
    button.textContent = text;
    button.style = `position: fixed; top: 0; left: ${left}px; z-index: 9999; display: ${display}`;
    button.id = id;
    button.onclick = onclick;
    return button;
  }

  // Create slider element
  var slider = document.createElement('input');
  slider.type = 'range';
  slider.min = '10';
  slider.max = '32';
  slider.value = GM_getValue("fontSize", '16');
  slider.id = 'fontSlider';
  slider.style = 'position: fixed; top: 0; left: 0; z-index: 9999; display: none';

  // Create font size display element
  var fontSizeDisplay = document.createElement('div');
  fontSizeDisplay.id = 'fontSizeDisplay';
  fontSizeDisplay.textContent = 'Font Size: ' + slider.value + 'px';
  fontSizeDisplay.style = 'position: fixed; top: 3px; left: 140px; z-index: 9999; font-size: 10px; display: none';

  // Append slider and display to body
  document.body.appendChild(slider);
  document.body.appendChild(fontSizeDisplay);

  // Function to update font sizes
  function updateFontSizes() {
    var all = document.getElementsByTagName("*");
    for (var i=0, max=all.length; i < max; i++) {
      // Exclude the control buttons
      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')) {
        all[i].style.fontSize = slider.value + "px";
      }
    }
  }

  // Event listener for slider
  slider.oninput = function() {
    fontSizeDisplay.textContent = 'Font Size: ' + this.value + 'px';
    updateFontSizes();
    GM_setValue("fontSize", this.value);
  }

  // Show the current value when mouse is over the slider
  slider.onmouseover = function() {
    this.title = 'Value: ' + this.value;
  }

  // Create buttons
  var expandButton = createButton('expandButton', '展开', 0, function() {
    ['fontSlider', 'fontSizeDisplay', 'hideButton'].forEach(function(id) {
      var element = document.getElementById(id);
      if(element) element.style.display = 'block';
    });

    if (isControllerEnabled) {
      document.getElementById('disableButton').style.display = 'block';
    } else {
      document.getElementById('enableButton').style.display = 'block';
    }

    this.style.display = 'none';
  }, 'block');

  var hideButton = createButton('hideButton', '收起', 220, function() {
    ['fontSlider', 'fontSizeDisplay', 'hideButton', 'disableButton', 'enableButton'].forEach(function(id) {
      var element = document.getElementById(id);
      if(element) element.style.display = 'none';
    });
    document.getElementById('expandButton').style.display = 'block';
  });

  var enableButton = createButton('enableButton', '开启', 270, function() {
    isControllerEnabled = true;
    GM_setValue("isControllerEnabled", isControllerEnabled);
    updateFontSizes();
    this.style.display = 'none';
    document.getElementById('disableButton').style.display = 'block';
  });

  var disableButton = createButton('disableButton', '关闭', 270, function() {
    isControllerEnabled = false;
    GM_setValue("isControllerEnabled", isControllerEnabled);
    this.style.display = 'none';
    document.getElementById('enableButton').style.display = 'block';
  });

  // Append buttons to body
  [expandButton, hideButton, enableButton, disableButton].forEach(function(button) {
    document.body.appendChild(button);
  });

  // Initial states
  if (isControllerEnabled) updateFontSizes();
});

QingJ © 2025

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