// ==UserScript== // @name 稍微放大电脑网站的字体 // @author ChatGPT定制 // @version 2 // @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'; // 获取当前网站URL,并根据其生成一个唯一的存储键 var storageKey = window.location.href.replace(/\/|\./g, '_'); // 根据存储键获取已保存的设置(如果存在) var isEnabled = GM_getValue(storageKey, true); function enlargeFontSize() { // 获取当前根元素的字体大小 const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize); // 计算新的根元素字体大小 const newRootFontSize = rootFontSize * 1.04; // 将新的根元素字体大小应用到根元素上 document.documentElement.style.fontSize = `${newRootFontSize}px`; // 获取当前根元素的行高 const rootLineHeight = parseFloat(getComputedStyle(document.documentElement).lineHeight); // 计算新的根元素行高 const newRootLineHeight = rootLineHeight * 1.08; // 将新的根元素行高应用到根元素上 document.documentElement.style.lineHeight = `${newRootLineHeight}px`; // 修改其他元素的字体大小和行高,以相对于新根元素字体大小和行高的方式进行缩放 const elementsToScale = document.querySelectorAll('*'); elementsToScale.forEach(element => { const fontSize = parseFloat(getComputedStyle(element).fontSize); element.style.fontSize = `${fontSize / rootFontSize * newRootFontSize}px`; const lineHeight = parseFloat(getComputedStyle(element).lineHeight); element.style.lineHeight = `${lineHeight / rootLineHeight * newRootLineHeight}px`; }); } function checkFontSize() { const width = window.innerWidth; if (width > 700) { enlargeFontSize(); } } // 根据保存的设置来启用或禁用字体放大功能 if (isEnabled) { window.addEventListener('resize', checkFontSize); checkFontSize(); } // 创建油猴菜单项,在菜单中添加“启用”和“禁用”选项 GM_registerMenuCommand(isEnabled ? '禁用字体放大' : '启用字体放大', function() { isEnabled = !isEnabled; GM_setValue(storageKey, isEnabled); if (isEnabled) { window.addEventListener('resize', checkFontSize); checkFontSize(); } 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', checkFontSize); } }); })();