Enhanced Faster Webpage Loading

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

目前為 2025-01-02 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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