Greasy Fork

Greasy Fork is available in English.

禁止 Web 延迟加载图片(一次性加载)

在 Web 页面中直接显示图片,禁止延迟加载,一次性加载所有图片

当前为 2023-11-22 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         禁止 Web 延迟加载图片(一次性加载)
// @name:en      Disable web lazy loading images (one-time loading)
// @description  在 Web 页面中直接显示图片,禁止延迟加载,一次性加载所有图片
// @description:en Display images directly on the web page, disable lazy loading, load all images at once.
// @version      0.8.2
// @author       DUN
// @match        *://*/*
// @grant        none
// @run-at       document-start
// @namespace    http://greasyfork.icu/users/662094
// ==/UserScript==

(function() {
    'use strict';

    function isExcluded(url) {
        const excludedKeywords = [
        ];
        return excludedKeywords.some(keyword => url.includes(keyword));
    }

    function getAbsoluteImageUrl(relativeOrAbsoluteUrl) {
        if (relativeOrAbsoluteUrl.startsWith('http') || relativeOrAbsoluteUrl.startsWith('//')) {
            return relativeOrAbsoluteUrl.startsWith('//') ? window.location.protocol + relativeOrAbsoluteUrl : relativeOrAbsoluteUrl;
        } else {
            const a = document.createElement('a');
            a.href = relativeOrAbsoluteUrl;
            return a.href;
        }
    }

    function processSrcset(srcsetAttribute) {
        return srcsetAttribute.split(',').map(srcsetPart => {
            let [url, descriptor] = srcsetPart.trim().split(/\s+/);
            url = getAbsoluteImageUrl(url);
            return isExcluded(url) ? '' : `${url} ${descriptor}`;
        }).filter(part => part).join(', ');
    }

    function updateImageSource(imgElement) {
        const srcAttribute = imgElement.getAttribute("data-src") || imgElement.getAttribute("data-original");
        const srcsetAttribute = imgElement.getAttribute("data-srcset");

        if (srcAttribute && !isExcluded(srcAttribute)) {
            imgElement.src = getAbsoluteImageUrl(srcAttribute);
        }

        if (srcsetAttribute) {
            const processedSrcset = processSrcset(srcsetAttribute);
            if (processedSrcset) {
                imgElement.srcset = processedSrcset;
            }
        }

        imgElement.removeAttribute('data-src');
        imgElement.removeAttribute('data-srcset');
        imgElement.removeAttribute('srcset');
        imgElement.removeAttribute('sizes');
        imgElement.classList.remove('lazyload', 'lazy');
    }

    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            if (mutation.type === 'attributes' && (mutation.attributeName === 'data-src' || mutation.attributeName === 'data-srcset')) {
                updateImageSource(mutation.target);
            } else if (mutation.type === 'childList' && mutation.addedNodes.length) {
                mutation.addedNodes.forEach(node => {
                    if (node.tagName === 'IMG') {
                        updateImageSource(node);
                    } else if (node.querySelectorAll) {
                        node.querySelectorAll('img').forEach(updateImageSource);
                    }
                });
            }
        });
    });

    observer.observe(document.documentElement, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['data-src', 'data-srcset']
    });

    window.addEventListener('DOMContentLoaded', () => {
        document.querySelectorAll('img').forEach(updateImageSource);
    });
})();