// ==UserScript== // @name 调整网页字体和行间距倍数 // @author ChatGPT // @version 6.7 // @description 脚本菜单可用于调整网页的字体和行间距倍数 // @match *://*/* // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // @run-at document-end // @namespace https://greasyfork.org/users/452911 // @downloadURL none // ==/UserScript== (function () { "use strict"; var storageKey = window.location.hostname; var isEnabled = GM_getValue(storageKey + "_enabled", true); var fontMultiplier = GM_getValue(storageKey + "_font_multiplier", 1); var lineHeightMultiplier = GM_getValue(storageKey + "_line_height_multiplier", 1); var originalSizes = {}; function storeOriginalSizes() { const elements = document.querySelectorAll("*"); elements.forEach((element) => { originalSizes[element] = { fontSize: parseFloat(getComputedStyle(element).fontSize), lineHeight: parseFloat(getComputedStyle(element).lineHeight), }; }); } function enlargeFontSizeAndLineHeight() { const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize); const newRootFontSize = rootFontSize * fontMultiplier; const rootLineHeight = parseFloat(getComputedStyle(document.documentElement).lineHeight); const newRootLineHeight = rootLineHeight * lineHeightMultiplier; const elementsToScale = document.querySelectorAll("*"); elementsToScale.forEach((element) => { const originalSize = originalSizes[element]; if (originalSize) { const fontSize = originalSize.fontSize; element.style.fontSize = `${(fontSize / rootFontSize) * newRootFontSize}px`; const lineHeight = originalSize.lineHeight; element.style.lineHeight = `${(lineHeight / rootLineHeight) * newRootLineHeight}px`; } }); document.documentElement.style.fontSize = `${newRootFontSize}px`; document.documentElement.style.lineHeight = `${newRootLineHeight}px`; } function checkFontSizeAndLineHeight() { enlargeFontSizeAndLineHeight(); } function applyFontSizeAndLineHeightChanges() { enlargeFontSizeAndLineHeight(); const elementsToScale = document.querySelectorAll("*"); elementsToScale.forEach((element) => { const originalSize = originalSizes[element]; if (originalSize) { const fontSize = originalSize.fontSize; element.style.fontSize = `${fontSize * fontMultiplier}px`; const lineHeight = originalSize.lineHeight; element.style.lineHeight = `${lineHeight * lineHeightMultiplier}px`; } }); } function observeDOMChanges() { const observer = new MutationObserver(() => { applyFontSizeAndLineHeightChanges(); }); observer.observe(document.body, { childList: true, subtree: true }); } if (isEnabled) { window.addEventListener("resize", checkFontSizeAndLineHeight); storeOriginalSizes(); applyFontSizeAndLineHeightChanges(); observeDOMChanges(); } GM_registerMenuCommand(isEnabled ? "禁用字体放大" : "启用字体放大", function () { isEnabled = !isEnabled; GM_setValue(storageKey + "_enabled", isEnabled); if (isEnabled) { window.addEventListener("resize", checkFontSizeAndLineHeight); applyFontSizeAndLineHeightChanges(); observeDOMChanges(); } else { document.documentElement.style.fontSize = ""; document.documentElement.style.lineHeight = ""; const elementsToReset = document.querySelectorAll("*"); elementsToReset.forEach((element) => { element.style.fontSize = ""; element.style.lineHeight = ""; }); window.removeEventListener("resize", checkFontSizeAndLineHeight); } }); GM_registerMenuCommand("调整字体大小", function () { var newFontMultiplier = prompt( "请输入字体大小倍数", fontMultiplier.toString() ); if (newFontMultiplier !== null) { fontMultiplier = parseFloat(newFontMultiplier); GM_setValue(storageKey + "_font_multiplier", fontMultiplier); applyFontSizeAndLineHeightChanges(); } }); GM_registerMenuCommand("调整行间距", function () { var newLineHeightMultiplier = prompt( "请输入行间距倍数", lineHeightMultiplier.toString() ); if (newLineHeightMultiplier !== null) { lineHeightMultiplier = parseFloat(newLineHeightMultiplier); GM_setValue(storageKey + "_line_height_multiplier", lineHeightMultiplier); applyFontSizeAndLineHeightChanges(); } }); // 监听页面刷新事件,应用字体和行间距的调整 window.addEventListener("load", applyFontSizeAndLineHeightChanges); })();