YouTube 影片自動停檢測)

檢測 YouTube 影片是否暫停,如果暫停則自動刷新頁面

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

// ==UserScript==
// @name         YouTube 影片自動停檢測)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  檢測 YouTube 影片是否暫停,如果暫停則自動刷新頁面
// @license MIT
// @match        https://www.youtube.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function checkVideoStatus() {
        const video = document.querySelector('video');
        if (video) {
            // 等待 1 秒後檢查影片狀態
            setTimeout(() => {
                if (video.paused) {
                    // 如果影片處於暫停狀態,刷新頁面
                    location.reload();
                }
                // 如果影片正在播放,不做任何處理
            }, 1000);
        }
    }

    // 使用 MutationObserver 監聽 URL 變化
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'childList') {
                const currentUrl = window.location.href;
                // 檢查是否為影片頁面
                if (currentUrl.includes('watch?v=')) {
                    checkVideoStatus();
                }
            }
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 初始檢查
    if (window.location.href.includes('watch?v=')) {
        checkVideoStatus();
    }
})();

QingJ © 2025

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