Greasy Fork

Greasy Fork is available in English.

缩小标题字体,缩小图片,并允许通过快捷键取消更改

摸鱼的艺术,更新hover时恢复图片大小, Alt+Shift+F 取消更改

当前为 2024-11-28 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         缩小标题字体,缩小图片,并允许通过快捷键取消更改
// @namespace    http://tampermonkey.net/
// @version      3.3
// @description  摸鱼的艺术,更新hover时恢复图片大小, Alt+Shift+F 取消更改
// @author       chenjiamian
// @match        http://*/*
// @match        https://*/*
// @exclude      *.png
// @exclude      *.jpg
// @exclude      *.gif
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let isEnabled = true;
    const originalFontSizes = new WeakMap();
    let originalStyles = new Map(); // 用于存储原始样式

    // 保存元素的原始样式
    function saveOriginalStyle(element) {
        if (!originalStyles.has(element)) {
            originalStyles.set(element, {
                fontSize: element.style.fontSize,
                fontWeight: element.style.fontWeight,
                maxWidth: element.style.maxWidth,
                height: element.style.height
            });
        }
    }

    // 恢复元素的原始样式
    function restoreOriginalStyle(element) {
        if (originalStyles.has(element)) {
            const styles = originalStyles.get(element);
            element.style.fontSize = styles.fontSize;
            element.style.fontWeight = styles.fontWeight;
            element.style.maxWidth = styles.maxWidth;
            element.style.height = styles.height;
            originalStyles.delete(element);
        }
    }

    // 记录原始字体大小
    function recordOriginalFontSizes() {
        document.querySelectorAll('*').forEach(ele => {
            if (!['HTML', 'HEAD', 'BODY'].includes(ele.tagName)) {
                const fontSize = parseFloat(getComputedStyle(ele).fontSize);
                originalFontSizes.set(ele, fontSize);
                saveOriginalStyle(ele);
            }
        });
    }

    function addCSS(cssText) {
        const style = document.createElement('style');
        style.textContent = cssText;
        (document.head || document.documentElement).appendChild(style);
    }

    function isDescendantOfPhotoShowViewer(element) {
        let parent = element.parentElement;
        while (parent) {
            if (parent.id === 'photoShowViewer') {
                return true;
            }
            parent = parent.parentElement;
        }
        return false;
    }

    function processElements() {
        if (!isEnabled) return; // 如果禁用,则不处理元素

        document.querySelectorAll('*').forEach(ele => {
            if (!['HTML', 'HEAD', 'BODY'].includes(ele.tagName)) {
                const originalFontSize = originalFontSizes.get(ele) || 16;
                saveOriginalStyle(ele); // 保存原始样式
                if (originalFontSize > 17) {
                    ele.style.fontWeight = 'bold';
                }
                 else{
                     ele.style.fontWeight = '';
                 }
                ele.style.fontSize = '16px';
            }

            if (ele.tagName === 'IMG' && !isDescendantOfPhotoShowViewer(ele)) {
                const imgWidth = parseFloat(getComputedStyle(ele).width);
                if (imgWidth > 500) {
                    ele.dataset.originalWidth = ele.width;
                    ele.dataset.originalHeight = ele.height;
                     saveOriginalStyle(ele); // 保存原始样式
                    ele.style.setProperty('max-width', '150px', 'important');
                    ele.style.setProperty('height', 'auto', 'important');
                }
            }
        });
    }

    // 恢复所有元素的原始样式
    function restoreAllOriginalStyles() {
        document.querySelectorAll('*').forEach(ele => {
            restoreOriginalStyle(ele);
        });
        originalFontSizes.clear();
    }

    // 使用 MutationObserver 监听 DOM 变化
    const observer = new MutationObserver((mutations) => {
        if (!isEnabled) return; // 如果禁用,则不处理元素
        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);
                        saveOriginalStyle(node);
                        processElements();
                    }
                });
            }
        });
    });

    // 快捷键处理函数
    function handleKeyPress(event) {
        if (event.altKey && event.shiftKey && event.key === 'F') {
            isEnabled = !isEnabled;
            if (isEnabled) {
                originalStyles = new Map(); // 重新启用时清空原始样式
                recordOriginalFontSizes();
                processElements();
                 console.log('字体和图片缩小已启用');
            } else {
                restoreAllOriginalStyles();
                 console.log('字体和图片缩小已禁用,已恢复原始样式');
            }
        }
    }

    // 确保在 DOM 加载完成后执行初始化
    function initialize() {
        recordOriginalFontSizes();
        processElements();
        observer.observe(document.body, { childList: true, subtree: true });
        window.addEventListener('keydown', handleKeyPress);
         console.log('字体和图片缩小脚本已启用,按 Alt+Shift+F 切换');
    }

    // 在 DOMContentLoaded 事件触发时执行初始化
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initialize);
    } else {
        // 如果 DOMContentLoaded 已经触发,立即执行初始化
        initialize();
    }

    // 使用 requestAnimationFrame 平滑应用样式
    function update() {
        if(isEnabled){
             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();
    };
})();