Lazy Load Image

Apply lazy loading to all images.

2025-01-06 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Lazy Load Image
// @namespace    lazy-load-image
// @version      2025-01-06
// @description  Apply lazy loading to all images.
// @author       Ace
// @run-at       document-end
// @match        *://*/*
// @icon         https://cdn-icons-png.flaticon.com/512/13159/13159509.png
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Debug mode: Set to true to enable logs
    const DEBUG = false;

    // Debug logging function
    const log = (...args) => {
        if (DEBUG) {
            console.log('[LazyLoadImage]', ...args);
        }
    };

    log('Script started');

    // Process existing images
    document.querySelectorAll('img').forEach(img => {
        if (!img.loading || img.loading !== 'lazy') {
            img.loading = 'lazy';
            log('Processed existing img:', img.src);
        }
    });

    // Debouncing logic with requestAnimationFrame
    let imgQueue = [];
    let isScheduled = false;

    const processQueue = () => {
        log('Processing queued images:', imgQueue.length);
        imgQueue.forEach(img => {
            if (!img.loading || img.loading !== 'lazy') {
                img.loading = 'lazy'; // Apply lazy loading
                log('Applied lazy loading to new img:', img.src);
            }
        });
        imgQueue = []; // Clear the queue
        isScheduled = false; // Allow new frame scheduling
    };

    // Observe dynamically added images
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === 1 && node.tagName === 'IMG') {
                    imgQueue.push(node); // Add image to the queue
                    log('New img detected:', node.src || 'No src');
                }
            });
        });

        // Schedule processing for the next frame if not already scheduled
        if (!isScheduled && imgQueue.length > 0) {
            isScheduled = true;
            requestAnimationFrame(processQueue);
            log('Scheduled processing for next frame');
        }
    });

    // Start observing the DOM
    observer.observe(document.body, { childList: true, subtree: true });
    log('MutationObserver started');

})();