YouTube Best Parts Looper

An attempt to implement loop for specified parts of YouTube videos. Doesn't work for background tabs.

// ==UserScript==
// @name         YouTube Best Parts Looper
// @namespace    https://gf.qytechs.cn/en/users/1413127-tumoxep
// @version      1.0
// @description  An attempt to implement loop for specified parts of YouTube videos. Doesn't work for background tabs.
// @license      WTFPL
// @match        https://www.youtube.com/watch*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const bestParts = [
        // replace with yours
        { v: 's6sZzgV5k8w', start: 14210.05, end: 14231.5 },
        { v: 'ZCzTYOaK_Ps', start: 29.9, end: 77.8 },
        { v: '_gNkftpGybU', start: 71.2, end: 136.55 },
    ];

    function enableLoop(video, start, end) {
        function loopCheck() {
            if (video.currentTime >= end) {
                video.currentTime = start;
                video.play();
            }
            requestAnimationFrame(loopCheck);
        }
        requestAnimationFrame(loopCheck);
    }

    function setupLoop() {
        const videoId = new URLSearchParams(window.location.search).get('v');
        if (!bestParts.find(el => el.v === videoId)) {
            return;
        }
        const video = document.querySelector('video');
        if (!video) {
            return;
        }
        bestParts.filter(p => p.v === videoId).forEach(p => enableLoop(video, p.start, p.end));
    }

    setupLoop();
})();

QingJ © 2025

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