Greasy Fork

Greasy Fork is available in English.

CC98图片尺寸限制

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         CC98图片尺寸限制
// @namespace    https://i.jself.top/
// @description  将CC98论坛中尺寸过大的图片进行缩放
// @version      1.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 editTimes = 0;

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

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

            if (heightInPixels > maxHeight) {
                const newWidth = (maxHeight / heightInPixels) * widthInPixels;
                if (editTimes > 0) {
                    const newWidth = (maxHeight / img.style.height) * img.style.width;
                }
                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);
})();