Lazy Load Videos

Accelerate video loading by lazy loading videos

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

// ==UserScript==
// @name         Lazy Load Videos
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Accelerate video loading by lazy loading videos
// @author       tae
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to lazy load videos
    function lazyLoadVideos() {
        const videos = document.querySelectorAll('video');
        const config = {
            rootMargin: '0px 0px 50px 0px',
            threshold: 0.01
        };

        let observer = new IntersectionObserver((entries, self) => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    let video = entry.target;
                    video.src = video.dataset.src;
                    video.load();
                    self.unobserve(video);
                }
            });
        }, config);

        videos.forEach(video => {
            if (video.dataset.src) {
                observer.observe(video);
            }
        });
    }

    // Run the lazy load function on page load
    window.addEventListener('load', lazyLoadVideos);
})();

QingJ © 2025

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