Greasy Fork

Greasy Fork is available in English.

Super Fast Loading Optimized

Boost website loading speed, prevent autoplay for YouTube videos, and lazy load images and videos.

当前为 2024-11-14 提交的版本,查看 最新版本

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

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

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

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

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

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Super Fast Loading Optimized
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Boost website loading speed, prevent autoplay for YouTube videos, and lazy load images and videos.
// @author       Farzan Farhangi
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    const allowAutoPlayWithinMillisecondsOfClick = 1000;
    let lastClickTimeMs = 0;

    // Function to prevent autoplay for YouTube videos
    const preventAutoplayYouTube = () => {
        const videos = document.querySelectorAll('video[data-src]');
        videos.forEach(video => {
            video.pause();
            video.removeAttribute('src');
            video.addEventListener('click', () => {
                if (!video.src) {
                    const source = video.getAttribute('data-src');
                    if (source) {
                        video.src = source;
                        video.play();
                    }
                }
            });
        });
    };

    // Lazy load videos
    const lazyLoadVideos = () => {
        const videos = document.querySelectorAll('video[data-src]');
        const observer = new IntersectionObserver(entries => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const video = entry.target;
                    video.src = video.getAttribute('data-src');
                    video.play();
                    observer.unobserve(video);
                }
            });
        });

        videos.forEach(video => observer.observe(video));
    };

    // Check if autoplay is allowed based on last click time
    const isAutoPlayAllowed = () => (Date.now() - lastClickTimeMs) <= allowAutoPlayWithinMillisecondsOfClick;

    window.addEventListener('click', () => { lastClickTimeMs = Date.now(); });

    // Enable lazy loading for images
    const enableLazyLoadImages = () => {
        const images = document.querySelectorAll('img:not([loading])');
        images.forEach(img => img.setAttribute('loading', 'lazy'));
    };

    // Load scripts asynchronously
    const loadScriptAsync = (url) => {
        const script = document.createElement('script');
        script.src = url;
        script.defer = true; // Load asynchronously without blocking rendering
        document.head.appendChild(script);
    };

    // Load CSS immediately
    const loadCSS = (url) => {
        const link = document.createElement('link');
        link.rel ='stylesheet';
        link.href = url;
        document.head.appendChild(link);
    };

    // Preload critical resources at the start
    const preloadResources = async () => {
        const criticalResources = [
            'https://example.com/styles.css',
            'https://example.com/script.js',
            // Add other resources here as needed
        ];

        criticalResources.forEach(resource => {
            if (resource.endsWith('.css')) {
                loadCSS(resource);
            } else if (resource.endsWith('.js')) {
                loadScriptAsync(resource);
            }
        });
    };

    window.addEventListener('load', () => {
        enableLazyLoadImages();
        preloadResources();
        preventAutoplayYouTube();
        lazyLoadVideos(); // Call lazy loading for videos after page load
    });

})();