Enhanced Faster Webpage Loading

Preload subsequent pages, lazy load images and videos, and use Pjax for faster webpage loading with robust error handling.

Verzia zo dňa 02.01.2025. Pozri najnovšiu verziu.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==UserScript==
// @name         Enhanced Faster Webpage Loading
// @namespace    http://tampermonkey.net/
// @version      1.8
// @description  Preload subsequent pages, lazy load images and videos, and use Pjax for faster webpage loading with robust error handling.
// @author       Tae
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    // Lazy load images, videos, and multimedia
    document.addEventListener("DOMContentLoaded", function () {
        let lazyElements = Array.from(document.querySelectorAll("img.lazy, video.lazy, iframe.lazy"));

        function loadLazyElement(lazyElement) {
            const dataSrc = lazyElement.dataset.src;
            if (!dataSrc) return;

            lazyElement.src = dataSrc;
            if (lazyElement.tagName === "VIDEO") {
                lazyElement.load();
            }
            lazyElement.classList.remove("lazy");
        }

        if ("IntersectionObserver" in window) {
            const observer = new IntersectionObserver((entries, observer) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        loadLazyElement(entry.target);
                        observer.unobserve(entry.target);
                    }
                });
            });

            lazyElements.forEach(element => observer.observe(element));
        } else {
            // Fallback for browsers without IntersectionObserver
            const lazyLoadFallback = () => {
                lazyElements = lazyElements.filter(lazyElement => {
                    if (
                        lazyElement.getBoundingClientRect().top < window.innerHeight &&
                        lazyElement.getBoundingClientRect().bottom > 0 &&
                        getComputedStyle(lazyElement).display !== "none"
                    ) {
                        loadLazyElement(lazyElement);
                        return false; // Remove from list
                    }
                    return true; // Keep in list
                });
                if (lazyElements.length === 0) {
                    document.removeEventListener("scroll", lazyLoadFallback);
                    window.removeEventListener("resize", lazyLoadFallback);
                    window.removeEventListener("orientationchange", lazyLoadFallback);
                }
            };

            document.addEventListener("scroll", lazyLoadFallback);
            window.addEventListener("resize", lazyLoadFallback);
            window.addEventListener("orientationchange", lazyLoadFallback);
        }
    });

    // Prefetch subsequent pages
    document.addEventListener("mouseover", event => {
        const target = event.target;
        if (target.tagName === "A" && target.href) {
            const link = target.href;
            if (/relevant-part|pattern/.test(link)) { // Refine prefetch condition
                const prefetchLink = document.createElement("link");
                prefetchLink.rel = "prefetch";
                prefetchLink.href = link;
                document.head.appendChild(prefetchLink);
            }
        }
    });

    // Load and initialize Pjax
    if (typeof Pjax === "undefined") {
        const script = document.createElement("script");
        script.src = "https://cdnjs.cloudflare.com/ajax/libs/pjax/0.2.8/pjax.min.js";
        script.onload = initializePjax;
        document.head.appendChild(script);
    } else {
        initializePjax();
    }

    function initializePjax() {
        const pjax = new Pjax({
            elements: "a[href]", // Target links
            selectors: ["title", ".content"], // Update these elements
            cacheBust: true, // Cache busting for fresh content
        });

        // Pjax event listeners
        document.addEventListener("pjax:send", () => console.log("Pjax: Request sent."));
        document.addEventListener("pjax:complete", () => {
            console.log("Pjax: Request complete.");
            reinitializeLazyLoading(); // Reinitialize lazy loading for new content
        });
        document.addEventListener("pjax:error", event => {
            console.error("Pjax: Error occurred.", event);
            // Fallback to full reload
            window.location.href = event.request.responseURL || window.location.href;
        });
    }

    function reinitializeLazyLoading() {
        const newLazyElements = Array.from(document.querySelectorAll("img.lazy, video.lazy, iframe.lazy:not([src])"));
        newLazyElements.forEach(element => {
            if (!element.dataset.src) return; // Skip invalid elements
            const observer = new IntersectionObserver((entries, observer) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        element.src = element.dataset.src;
                        observer.unobserve(element);
                    }
                });
            });
            observer.observe(element);
        });
    }
})();