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 提交的版本,查看 最新版本

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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);
        });
    }
})();