Enhanced Faster Webpage Loading (Optimized)

Optimize webpage loading with native prefetching, priority hints, and lazy loading.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Enhanced Faster Webpage Loading (Optimized)
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  Optimize webpage loading with native prefetching, priority hints, and lazy loading.
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    const EXCLUDED_LINKS = ['/login', '/logout', '/signout', '/signin', '/account', '/auth'];

    // Lazy Load Images, Videos, and Iframes
    function setupLazyLoading(root = document) {
        const lazyElements = root.querySelectorAll("img[data-src], video[data-src], iframe[data-src]");

        function loadLazyElement(el) {
            if (el.dataset.src) {
                el.src = el.dataset.src;
                if (el.tagName.toUpperCase() === "VIDEO") el.load();
                el.removeAttribute("data-src");
            }
        }

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

    // Prefetch and Prioritize Links
    function enhancePrefetching() {
        document.querySelectorAll("a[href]").forEach(link => {
            const href = link.href;
            if (!EXCLUDED_LINKS.some(excluded => href.includes(excluded)) && !link.hasAttribute("data-no-prefetch")) {
                const prefetch = document.createElement("link");
                prefetch.rel = "prefetch";
                prefetch.href = href;
                prefetch.as = "document";
                document.head.appendChild(prefetch);

                // Use Priority Hints (if supported)
                link.setAttribute("importance", "high");
            }
        });
    }

    // Watch for dynamically added content
    function observeDynamicContent() {
        const observer = new MutationObserver(mutations => {
            mutations.forEach(mutation => {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === 1) {
                        setupLazyLoading(node);
                        enhancePrefetching();
                    }
                });
            });
        });
        observer.observe(document.body, { childList: true, subtree: true });
    }

    // Initialize Enhancements
    function init() {
        setupLazyLoading();
        enhancePrefetching();
        observeDynamicContent();
    }

    if (document.readyState === "loading") {
        document.addEventListener("DOMContentLoaded", init);
    } else {
        init();
    }
})();