调整网页字体和行间距倍数

脚本菜单可用于调整网页的字体和行间距倍数

当前为 2024-10-15 提交的版本,查看 最新版本

// ==UserScript==
// @name 调整网页字体和行间距倍数
// @author ChatGPT
// @version 7.0
// @description 脚本菜单可用于调整网页的字体和行间距倍数
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-end
// @namespace https://gf.qytechs.cn/uers/452911
// ==/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() {
        if (fontMultiplier !== 1 || lineHeightMultiplier !== 1) {
            enlargeFontSizeAndLineHeight();
        }
    }

    function applyFontSizeAndLineHeightChanges() {
        if (fontMultiplier !== 1 || lineHeightMultiplier !== 1) {
            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(() => {
            if (fontMultiplier !== 1 || lineHeightMultiplier !== 1) {
                applyFontSizeAndLineHeightChanges();
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    function updateMenu() {
        GM_registerMenuCommand(isEnabled ? "禁用字体放大" : "启用字体放大", function () {
            isEnabled = !isEnabled;
            GM_setValue(storageKey + "_enabled", isEnabled);
            if (isEnabled && (fontMultiplier !== 1 || lineHeightMultiplier !== 1)) {
                window.addEventListener("resize", checkFontSizeAndLineHeight);

                // 重新存储原始字体和行间距大小,确保立即生效
                storeOriginalSizes();
                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);
            }
            // 更新菜单项
            updateMenu();
        });
    }

    GM_registerMenuCommand("调整字体大小", function () {
        var newFontMultiplier = prompt(
            "请输入字体大小倍数",
            fontMultiplier.toString()
        );
        if (newFontMultiplier !== null) {
            fontMultiplier = parseFloat(newFontMultiplier);
            GM_setValue(storageKey + "_font_multiplier", fontMultiplier);
            if (fontMultiplier !== 1 || lineHeightMultiplier !== 1) {
                applyFontSizeAndLineHeightChanges();
            }
        }
    });

    GM_registerMenuCommand("调整行间距", function () {
        var newLineHeightMultiplier = prompt(
            "请输入行间距倍数",
            lineHeightMultiplier.toString()
        );
        if (newLineHeightMultiplier !== null) {
            lineHeightMultiplier = parseFloat(newLineHeightMultiplier);
            GM_setValue(storageKey + "_line_height_multiplier", lineHeightMultiplier);
            if (fontMultiplier !== 1 || lineHeightMultiplier !== 1) {
                applyFontSizeAndLineHeightChanges();
            }
        }
    });

    // 自动应用字体和行间距放大功能
    if (isEnabled && (fontMultiplier !== 1 || lineHeightMultiplier !== 1)) {
        storeOriginalSizes();  // 存储原始尺寸
        applyFontSizeAndLineHeightChanges();  // 应用字体和行间距放大
        observeDOMChanges();  // 监控DOM变化
        window.addEventListener("resize", checkFontSizeAndLineHeight);  // 监听窗口大小变化
    }

    // 初始化菜单
    updateMenu();

})();
// ==UserScript==
// @name 调整网页字体和行间距倍数
// @author ChatGPT
// @version 7.0
// @description 脚本菜单可用于调整网页的字体和行间距倍数
// @match *://*/*
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-end
// @namespace https://gf.qytechs.cn/users/452911
// ==/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 rootLineHeight = parseFloat(getComputedStyle(document.documentElement).lineHeight);

   const elementsToScale = document.querySelectorAll("*");
   elementsToScale.forEach((element) => {
     const originalSize = originalSizes[element];
     if (originalSize) {
       const fontSize = originalSize.fontSize;
       if (fontMultiplier !== 1) {
         element.style.fontSize = `${(fontSize / rootFontSize) * (rootFontSize * fontMultiplier)}px`;
       }
       
       const lineHeight = originalSize.lineHeight;
       if (lineHeightMultiplier !== 1) {
         element.style.lineHeight = `${(lineHeight / rootLineHeight) * (rootLineHeight * lineHeightMultiplier)}px`;
       }
     }
   });

   if (fontMultiplier !== 1) {
     document.documentElement.style.fontSize = `${rootFontSize * fontMultiplier}px`;
   }
   if (lineHeightMultiplier !== 1) {
     document.documentElement.style.lineHeight = `${rootLineHeight * lineHeightMultiplier}px`;
   }
 }

 function applyFontSizeAndLineHeightChanges() {
   const elementsToScale = document.querySelectorAll("*");
   elementsToScale.forEach((element) => {
     const originalSize = originalSizes[element];
     if (originalSize) {
       const fontSize = originalSize.fontSize;
       if (fontMultiplier !== 1) {
         element.style.fontSize = `${fontSize * fontMultiplier}px`;
       } else {
         element.style.fontSize = ""; // 重置为默认大小
       }

       const lineHeight = originalSize.lineHeight;
       if (lineHeightMultiplier !== 1) {
         element.style.lineHeight = `${lineHeight * lineHeightMultiplier}px`;
       } else {
         element.style.lineHeight = ""; // 重置为默认行间距
       }
     }
   });

   if (fontMultiplier !== 1) {
     document.documentElement.style.fontSize = `${parseFloat(getComputedStyle(document.documentElement).fontSize) * fontMultiplier}px`;
   } else {
     document.documentElement.style.fontSize = ""; // 重置根字体大小
   }

   if (lineHeightMultiplier !== 1) {
     document.documentElement.style.lineHeight = `${parseFloat(getComputedStyle(document.documentElement).lineHeight) * lineHeightMultiplier}px`;
   } else {
     document.documentElement.style.lineHeight = ""; // 重置根行间距
   }
 }

 function observeDOMChanges() {
   const observer = new MutationObserver(() => {
     if (fontMultiplier !== 1 || lineHeightMultiplier !== 1) {
       applyFontSizeAndLineHeightChanges();
     }
   });

   observer.observe(document.body, {
     childList: true,
     subtree: true
   });
 }

 if (isEnabled && (fontMultiplier !== 1 || lineHeightMultiplier !== 1)) {
   window.addEventListener("resize", applyFontSizeAndLineHeightChanges);
   storeOriginalSizes();
   applyFontSizeAndLineHeightChanges();
   observeDOMChanges();
 }

 GM_registerMenuCommand(isEnabled ? "禁用字体放大" : "启用字体放大", function () {
   isEnabled = !isEnabled;
   GM_setValue(storageKey + "_enabled", isEnabled);
   if (isEnabled && (fontMultiplier !== 1 || lineHeightMultiplier !== 1)) {
     window.addEventListener("resize", applyFontSizeAndLineHeightChanges);
     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", applyFontSizeAndLineHeightChanges);
   }
 });

 GM_registerMenuCommand("调整字体大小", function () {
   var newFontMultiplier = prompt(
     "请输入字体大小倍数",
     fontMultiplier.toString()
   );
   if (newFontMultiplier !== null) {
     fontMultiplier = parseFloat(newFontMultiplier);
     GM_setValue(storageKey + "_font_multiplier", fontMultiplier);
     // 刷新页面以应用新的字体设置
     location.reload();
   }
 });

 GM_registerMenuCommand("调整行间距", function () {
   var newLineHeightMultiplier = prompt(
     "请输入行间距倍数",
     lineHeightMultiplier.toString()
   );
   if (newLineHeightMultiplier !== null) {
     lineHeightMultiplier = parseFloat(newLineHeightMultiplier);
     GM_setValue(storageKey + "_line_height_multiplier", lineHeightMultiplier);
     // 刷新页面以应用新的行间距设置
     location.reload();
   }
 });

})();

QingJ © 2025

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