四川干部网络学院视频自动监控与标签页管理

检测视频暂停状态,暂停10秒并存在已完成,则关闭当前页并播放下一个标签页的视频; 使用方法:(先把要看的视频打开,看完第一个,自动关闭当前页面看下一个)

// ==UserScript==
// @name         四川干部网络学院视频自动监控与标签页管理
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  检测视频暂停状态,暂停10秒并存在已完成,则关闭当前页并播放下一个标签页的视频; 使用方法:(先把要看的视频打开,看完第一个,自动关闭当前页面看下一个)
// @author       Thruon
// @match        https://web.scgb.gov.cn/*
// @include      https://web.scgb.gov.cn/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let pausedSeconds = 0; // 记录视频暂停的秒数
    let checkInterval; // 状态检测的间隔计时器
    const MAX_PAUSED_TIME = 5; // 最大允许暂停时间(秒)

    function initVideoDetection() {
        console.log('初始化视频检测');
        pausedSeconds = 0; // 重置计数器

        // 设置状态检测间隔
        checkInterval = setInterval(() => {
          reloadPage();
          if (document.visibilityState === 'visible' && !document.querySelector('.ivu-tag-color-white')) {
              document.querySelector('#videoPlayer_html5_api').play()
          }else{
                document.querySelector('#videoPlayer_html5_api').pause()
          }
        if (document.querySelector('#videoPlayer_html5_api').paused && document.visibilityState === 'visible') {
            pausedSeconds++;
            console.log(`视频已暂停 ${pausedSeconds} 秒`);

            // 如果暂停超过设定时间
            if (pausedSeconds >= MAX_PAUSED_TIME) {
                console.log(`视频暂停超过 ${MAX_PAUSED_TIME} 秒,进行标签页管理...`);
                clearInterval(checkInterval);
                attemptTabManagement();
            }
        } else {
            // 视频正在播放,重置暂停计数器
            pausedSeconds = 0;
        }
        }, 1000); // 每秒检查一次
    }

    function attemptTabManagement() {
        console.log('关闭当前标签页');
        setTimeout(() => {
            window.close();
        }, 1000);
    }

    function reloadPage() {
        let video = document.querySelector('#videoPlayer_html5_api');
        video.addEventListener('timeupdate', function() {
            // 计算播放进度百分比
            let progress = (video.currentTime / video.duration) * 100;
            // 如果播放完成(进度>=100%),刷新页面
            if (progress >= 100) {
                location.reload();
            }
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initVideoDetection);
    } else {
        initVideoDetection();
    };


})();