Enhanced Faster Webpage Loading

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

ของเมื่อวันที่ 02-01-2025 ดู เวอร์ชันล่าสุด

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);
        });
    }
})();