Greasy Fork

Greasy Fork is available in English.

Super Fast Loading

Boost website loading speed and video playback while preventing autoplay. This script enhances loading times for images and videos, making it ideal for users with limited internet access. Although it can't increase your internet speed, it ensures a smoother browsing experience. Try this code for improved speed!

目前为 2024-11-13 提交的版本。查看 最新版本

// ==UserScript==
// @name         Super Fast Loading
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Boost website loading speed and video playback while preventing autoplay. This script enhances loading times for images and videos, making it ideal for users with limited internet access. Although it can't increase your internet speed, it ensures a smoother browsing experience. Try this code for improved speed!
// @author       Farzan Farhangi
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// @homepage     http://greasyfork.icu/en/scripts/517138-super-fast-loading
// ==/UserScript==

(function() {
    'use strict';

    const cacheKeyPrefix = 'cachedResource_';
    const simulatedSpeedFactor = 1; // Simulate actual speed for better performance

    // Load cached resources
    const loadCachedResource = (url) => {
        const cachedData = GM_getValue(cacheKeyPrefix + url);
        if (cachedData) {
            const script = document.createElement('script');
            script.textContent = cachedData;
            document.head.appendChild(script);
            return true;
        }
        return false;
    };

    // Fetch and cache resources with simulated delay
    const fetchAndCacheResource = async (url) => {
        try {
            await new Promise(resolve => setTimeout(resolve, 1000 / simulatedSpeedFactor)); // Simulate network delay
            const response = await new Promise((resolve, reject) => {
                GM_xmlhttpRequest({
                    method: "GET",
                    url: url,
                    onload: resolve,
                    onerror: reject
                });
            });
            if (response.status === 200) {
                GM_setValue(cacheKeyPrefix + url, response.responseText);
                const script = document.createElement('script');
                script.textContent = response.responseText;
                document.head.appendChild(script);
            }
        } catch (error) {
            console.error(`Failed to fetch resource: ${url}`, error);
        }
    };

    // Preload critical resources for faster initial load
    const preloadCriticalResources = () => {
        const criticalResources = [
            '/index.html',
            '/styles.css',
            '/script.js',
            // Add other critical resources as needed
        ];

        criticalResources.forEach(url => {
            fetchAndCacheResource(url);
        });
    };

    // Optimize initial loading by fetching resources in parallel
    const optimizeInitialLoad = () => {
        const initialResources = [
            '/index.html',
            '/styles.css',
            '/script.js',
            // Add any other resources that should be loaded initially
        ];

        initialResources.forEach(url => {
            fetchAndCacheResource(url); // Fetch each resource simultaneously for faster loading
        });
    };

    preloadCriticalResources();
    optimizeInitialLoad();
})();