// ==UserScript== // @name 增强版网站字体放大器 // @namespace http://tampermonkey.net/ // @version 0.5 // @license MIT // @description 为任意网站(默认支持 Nodeseek, DeepFlood)提供独立的字体放大、开关和重置功能,优化阅读体验。 // @author 你的名字 // @match https://www.nodeseek.com/* // @match https://www.deepflood.com/* // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // @downloadURL none // ==/UserScript== (function() { 'use strict'; const hostname = window.location.hostname; const defaultScale = 1.5; // 根据域名生成独立的存储键 const scaleKey = `fontScale_${hostname}`; const enabledKey = `fontEnabled_${hostname}`; let scale = GM_getValue(scaleKey, defaultScale); let isEnabled = GM_getValue(enabledKey, true); const styleElementId = 'custom-font-styler'; // 需要放大字体的元素选择器列表 const elementsToEnlarge = [ // Nodeseek '.post-title', '.post-content', '.reply-content', '.forum-title', '.post-author', '.forum-description', // DeepFlood & Common '.article-title', '.article-content', '.comment-content', 'h1', 'h2', 'p', 'article', '.prose' ]; // 动态更新或创建样式 function applyStyles() { // 如果禁用,则移除样式标签并返回 const existingStyleElement = document.getElementById(styleElementId); if (!isEnabled) { if (existingStyleElement) { existingStyleElement.remove(); } return; } let styleContent = ''; elementsToEnlarge.forEach(selector => { // 使用 CSS 自定义属性来避免重复计算 styleContent += ` ${selector} { font-size: ${scale}em !important; line-height: 1.6em !important; } `; }); // 如果样式标签已存在,则更新内容,否则创建新的 if (existingStyleElement) { existingStyleElement.innerHTML = styleContent; } else { const styleEl = document.createElement('style'); styleEl.id = styleElementId; styleEl.innerHTML = styleContent; document.head.appendChild(styleEl); } } // 更新菜单状态 function registerCommands() { // 油猴菜单不支持动态刷新,所以我们用旧方法,修改后需刷新 GM_registerMenuCommand(`${isEnabled ? '✅' : '❌'} 字体放大 (点击切换)`, () => { isEnabled = !isEnabled; GM_setValue(enabledKey, isEnabled); location.reload(); // 简单起见,直接刷新页面来应用开关状态 }); GM_registerMenuCommand(`设置字体放大倍率 (当前: ${scale}x)`, () => { let newScale = prompt(`为 ${hostname} 设置放大倍率 (例如 1.5):`, scale); if (newScale !== null) { newScale = parseFloat(newScale); if (!isNaN(newScale) && newScale > 0) { GM_setValue(scaleKey, newScale); location.reload(); } else { alert('请输入一个有效的正数倍率。'); } } }); GM_registerMenuCommand('恢复默认设置', () => { GM_setValue(scaleKey, defaultScale); GM_setValue(enabledKey, true); location.reload(); }); } // --- Main Execution --- registerCommands(); if (isEnabled) { // 使用 CSS em 单位进行相对缩放,更灵活 // 我们不再需要 MutationObserver,因为CSS是全局应用的,对动态加载的内容同样有效 // 只需要在页面加载时注入一次样式即可 applyStyles(); } })();