Super Fast Loading

Prevent autoplay for YouTube, add download button with quality selection, lazy load images and videos, and boost website loading speed with multiple concurrent connections.

目前為 2024-11-25 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Super Fast Loading
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Prevent autoplay for YouTube, add download button with quality selection, lazy load images and videos, and boost website loading speed with multiple concurrent connections.
// @author       Farzan Farhangi
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// ==/UserScript==

(function() {
    'use strict';

    // Settings for autoplay prevention and lazy loading
    const allowAutoPlayWithinMillisecondsOfClick = 1000;
    let lastClickTimeMs = 0;

    // Function to create download with multiple connections
    const downloadWithMultipleConnections = (url, connections = 18) => {
        const chunkSize = Math.ceil(url.length / connections);
        const promises = [];
        for (let i = 0; i < connections; i++) {
            const start = i * chunkSize;
            const end = start + chunkSize - 1;
            promises.push(
                fetch(url, {
                    headers: { 'Range': `bytes=${start}-${end}` }
                })
            );
        }
        return Promise.all(promises)
            .then(responses => Promise.all(responses.map(res => res.blob())))
            .then(blobs => new Blob(blobs));
    };

    // 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) {
                        downloadWithMultipleConnections(source, 32).then(blob => {
                            video.src = URL.createObjectURL(blob);
                            video.play();
                        });
                    }
                }
            });
        });
    };

    // Lazy load videos with multiple connections
    const lazyLoadVideos = () => {
        const videos = document.querySelectorAll('video[data-src]');
        const observer = new IntersectionObserver(entries => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    const video = entry.target;
                    downloadWithMultipleConnections(video.getAttribute('data-src'), 32).then(blob => {
                        video.src = URL.createObjectURL(blob);
                        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 with multiple connections
    const loadScriptAsync = (url) => {
        downloadWithMultipleConnections(url, 18).then(blob => {
            const script = document.createElement('script');
            script.src = URL.createObjectURL(blob);
            script.defer = true;
            document.head.appendChild(script);
        });
    };

    // Load CSS immediately with multiple connections
    const loadCSS = (url) => {
        downloadWithMultipleConnections(url, 18).then(blob => {
            const link = document.createElement('link');
            link.rel = 'stylesheet';
            link.href = URL.createObjectURL(blob);
            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);
            }
        });
    };

    // Add custom CSS styles for the download button
    GM_addStyle(`
        #custom-download-button {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #3399ff; /* Pastel blue */
            color: white;
            border: none;
            padding: 10px 28px;
            border-radius: 25px;
            font-size: 14px;
            cursor: pointer;
            z-index: 1000;
        }

        #modal-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 2000;
        }

        #modal-content {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            width: 80%;
            max-width: 600px;
            overflow: hidden;
        }

        /* Adjust iframe settings */
        #modal-content iframe {
            width: 100%;
            height: 400px;
            border: none;
        }
    `);

    // Create download button
    function createDownloadButton() {
        const downloadButton = document.createElement('button');
        downloadButton.id = 'custom-download-button';
        downloadButton.innerText = 'VIDEO DOWNLOAD';
        downloadButton.onclick = showModal;

        // Add the button to the YouTube video controls
        const interval = setInterval(() => {
            const controls = document.querySelector('.ytp-right-controls');
            if (controls && !document.getElementById('custom-download-button')) {
                controls.insertBefore(downloadButton, controls.firstChild);
                clearInterval(interval);
            }
        }, 1000);
    }

    // Display modal
    function showModal() {
        const overlay = document.createElement('div');
        overlay.id = 'modal-overlay';
        overlay.onclick = hideModal;

        const modalContent = document.createElement('div');
        modalContent.id = 'modal-content';

        // Create iframe for y2mate site
        const iframe = document.createElement('iframe');
        const videoID = window.location.href.split('v=')[1]?.split('&')[0];
        if (videoID) {
            iframe.src = `https://www.y2mate.com/youtube/${videoID}`;
        }
        modalContent.appendChild(iframe);
        overlay.appendChild(modalContent);
        document.body.appendChild(overlay);

        overlay.style.display = 'flex';
    }

    // Hide modal
    function hideModal() {
        const overlay = document.getElementById('modal-overlay');
        if (overlay) {
            overlay.remove();
        }
    }

    // Disable autoplay for YouTube videos
    function disableAutoPlay() {
        const player = document.querySelector('video');
        if (player) {
            player.autoplay = false;
            player.pause();
        }
    }

    // Run code when page loads
    window.addEventListener('yt-navigate-finish', () => {
        disableAutoPlay();
        createDownloadButton();
    });

    // Execute code on initial load
    window.onload = () => {
        disableAutoPlay();
        createDownloadButton();
        enableLazyLoadImages();
        preloadResources();
        preventAutoplayYouTube();
        lazyLoadVideos();
    };
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址