Lazy Load Videos

Accelerate video loading by lazy loading videos

目前為 2024-10-19 提交的版本,檢視 最新版本

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

(function() {
    'use strict';

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

        let observer;
        if ('IntersectionObserver' in window) {
            observer = new IntersectionObserver((entries, self) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        let video = entry.target;
                        video.src = video.dataset.src;
                        video.load();
                        video.pause(); // Ensure the video is paused before loading
                        self.unobserve(video);
                    }
                });
            }, config);

            videos.forEach(video => {
                if (video.dataset.src) {
                    observer.observe(video);
                }
            });
        } else {
            // Fallback for browsers that don't support IntersectionObserver
            videos.forEach(video => {
                if (video.dataset.src) {
                    video.src = video.dataset.src;
                    video.load();
                    video.pause(); // Ensure the video is paused before loading
                }
            });
        }
    }

    window.addEventListener('DOMContentLoaded', lazyLoadVideos);
})();

QingJ © 2025

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