Greasy Fork

Greasy Fork is available in English.

CC98图片尺寸限制

将CC98论坛中尺寸过大的图片进行缩放

当前为 2024-08-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         CC98图片尺寸限制
// @namespace    https://i.jself.top/
// @description  将CC98论坛中尺寸过大的图片进行缩放
// @version      1.0.1
// @icon         https://www.cc98.org/static/98icon.ico
// @author       jself
// @match        https://www.cc98.org/topic/*
// @match        https://www-cc98-org-s.webvpn.zju.edu.cn:8001/*
// @grant        none
// @license      GPL
// ==/UserScript==

(function() {
    'use strict';

    function resizeImages() {
        const images = document.querySelectorAll('img');
        images.forEach(img => {
            const cssWidth = window.getComputedStyle(img).width;
            const widthInPixels = parseFloat(cssWidth);
            const cssHeight = window.getComputedStyle(img).height;
            const heightInPixels = parseFloat(cssHeight);

            //在这里自定义尺寸限制
            const maxWidth = 750;
            const maxHeight = 300;

            if (widthInPixels > maxWidth) {
                const originalHeight = img.naturalHeight;
                const newHeight = (maxWidth / widthInPixels) * originalHeight;
                img.style.width = `${maxWidth}px`;
                img.style.height = `${newHeight}px`;
            }

            if (heightInPixels > maxHeight) {
                const originalWidth = img.naturalWidth;
                const newWidth = (maxHeight / heightInPixels) * originalWidth;
                img.style.height =`${maxHeight}px`;
                img.style.width = `${newWidth}px`;
            }
        });
    }

    const observer = new MutationObserver(() => {
        resizeImages();
    });

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

    window.addEventListener('load', resizeImages);
})();