// ==UserScript==
// @name         🥇人事人才服务网chinahrt.cn刷课-自动切换视频【影刃】
// @namespace    https://doc.yingren.xyz
// @icon         https://doc.yingren.xyz/img/logo2.jpg
// @version      1.2
// @description  人事人才服务网chinahrt.cn刷课,功能:视频播放结束后自动切换下个视频,支持呼伦贝尔市专业技术人员继续教育、鄂尔多斯市专业技术人员继续教育
// @author       影刃
// @match        *://*.chinahrt.cn/*
// @grant        none
// @run-at       document-start
// @license      GPL 3
// ==/UserScript==
(function() {
    'use strict';

    // 初始化视频自动切换
    function initVideoAutoSwitch() {
        const video = document.querySelector("video");
        const navLinks = Array.from(document.querySelectorAll("#cc > li > a"));

        if (!video || !navLinks.length) return;

        console.log("找到了video元素")

        video.addEventListener('ended', () => {
            const currentIndex = navLinks.findIndex(link =>
                                                    link.classList.contains("current")
                                                   );
            if (currentIndex === -1 || currentIndex === navLinks.length - 1) return;

            const nextLink = navLinks[currentIndex + 1];
            nextLink?.click();
        });
    }

    // 添加防抖处理
    let isSwitching = false;
    function nextVideo() {
        if (isSwitching) return;
        isSwitching = true;

        try {
            initVideoAutoSwitch();
        } finally {
            isSwitching = false;
        }
    }

    // 初始化并添加错误处理
    try {
        nextVideo();
    } catch (error) {
        console.error('视频自动切换初始化失败:', error);
    }
})();