// ==UserScript== // @name 缩小标题字体,缩小图片 // @namespace http://tampermonkey.net/ // @version 2.2 // @description 摸鱼的艺术,更新hover时恢复图片大小 // @author chenjiamian // @match http://*/* // @match https://*/* // @exclude *.png // @exclude *.jpg // @exclude *.gif // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 存储原始字体大小 const originalFontSizes = new WeakMap(); // 在应用通用 CSS 之前,先记录所有元素的原始字体大小 function recordOriginalFontSizes() { document.querySelectorAll('*').forEach(ele => { if (!['HTML', 'HEAD', 'BODY'].includes(ele.tagName)) { const fontSize = parseFloat(getComputedStyle(ele).fontSize); originalFontSizes.set(ele, fontSize); } }); } function addCSS(cssText) { const style = document.createElement('style'); style.textContent = cssText; (document.head || document.documentElement).appendChild(style); } function processElements() { document.querySelectorAll('*').forEach(ele => { if (!['HTML', 'HEAD', 'BODY'].includes(ele.tagName)) { const originalFontSize = originalFontSizes.get(ele) || 16; if (originalFontSize > 16) { ele.style.fontWeight = 'bold'; } } if (ele.tagName === 'IMG') { const imgWidth = parseFloat(getComputedStyle(ele).width); if (imgWidth > 500) { ele.dataset.originalWidth = ele.width; ele.dataset.originalHeight = ele.height; // 设置新的内联样式 ele.style.setProperty('max-width', '150px', 'important'); ele.style.setProperty('height', 'auto', 'important'); } } }); } // 使用 MutationObserver 监听 DOM 变化 const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (mutation.type === 'childList') { mutation.addedNodes.forEach((node) => { if (node.nodeType === Node.ELEMENT_NODE) { // 为新添加的元素记录原始字体大小 const fontSize = parseFloat(getComputedStyle(node).fontSize); originalFontSizes.set(node, fontSize); processElements(); } }); } }); }); // 确保在 DOM 加载完成后执行初始化 function initialize() { recordOriginalFontSizes(); // 提前注入通用 CSS const commonCSS = ` * { font-size: 16px !important; } img { max-width: 150px; height: auto; } img:hover { max-width: none; height: auto; } `; addCSS(commonCSS); processElements(); observer.observe(document.body, { childList: true, subtree: true }); } // 在 DOMContentLoaded 事件触发时执行初始化 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initialize); } else { // 如果 DOMContentLoaded 已经触发,立即执行初始化 initialize(); } // 使用 requestAnimationFrame 平滑应用样式 function update() { processElements(); requestAnimationFrame(update); } requestAnimationFrame(update); // 处理动态加载的内容 window.addEventListener('hashchange', processElements); window.addEventListener('popstate', processElements); const originalPushState = history.pushState; history.pushState = function() { originalPushState.apply(this, arguments); processElements(); }; })();