蓉城先锋 cddyjy.com

2024/10/22 09:16:00

当前为 2024-10-22 提交的版本,查看 最新版本

// ==UserScript==
// @name        蓉城先锋 cddyjy.com
// @namespace   cddyjy
// @match       https://rcxf.cddyjy.com/dywkt/educationTraining/detailspage*
// @grant       none
// @version     2.0
// @author      compass
// @description 2024/10/22 09:16:00
// ==/UserScript==

(function() {
    'use strict';

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function waitForElement(selector, timeout = 10000) {
        return new Promise((resolve, reject) => {
            let timer = 0;
            const interval = setInterval(() => {
                const element = document.querySelector(selector);
                if (element) {
                    clearInterval(interval);
                    resolve(element);
                } else if (timer >= timeout) {
                    clearInterval(interval);
                    reject(new Error(`Timeout waiting for element: ${selector}`));
                } else {
                    timer += 100;
                }
            }, 100);
        });
    }

    async function findIncompleteCourse() {
        const rows = document.querySelectorAll('.ant-table-tbody tr.ant-table-row-level-0');

        for (const row of rows) {
            const progressCell = row.querySelector('.ant-table-row-cell-ellipsis[title="100.00%"]');
            const progressText = progressCell ? progressCell.getAttribute('title') : '';
            if (progressText !== '100.00%') {
                const studyButton = row.querySelector('.ant-table-fixed-columns-in-body a');
                if (studyButton) {
                    return studyButton;
                }
            }
        }

        throw new Error('No incomplete courses found.');
    }

    async function goToNextPage() {
        const nextPageButton = document.querySelector('.ant-pagination-next:not(.ant-pagination-disabled) .ant-pagination-item-link');
        if (nextPageButton) {
            nextPageButton.click();
            console.log("Clicked next page button.");
            await sleep(1000); // 等待页面加载
            console.log("Continuing with next page...");
        } else {
            console.log("Reached the last page. Ending script execution.");
            return false; // 没有下一页,返回 false 结束脚本
        }
    }

    async function checkUrlAndExecute() {
        console.log("Checking current URL...");
        if (window.location.href.match(/https:\/\/rcxf\.cddyjy\.com\/dywkt\/educationTraining\/detailspage/)) {
            console.log("Current URL matches the expected pattern. Executing autoStudy...");
            await autoStudy();
        } else {
            console.log("Current URL does not match the expected pattern. Skipping script execution.");
        }
    }

    async function autoStudy() {
        try {
            console.log("Searching for the first incomplete course...");
            let studyButton;

            try {
                studyButton = await findIncompleteCourse();
                console.log("Found the first incomplete course.");
            } catch (error) {
                console.log("All courses on this page are completed.");
                const nextPageResult = await goToNextPage();

                // 如果有下一页,则重新查找课程
                studyButton = await findIncompleteCourse();
            }

            studyButton.click();

            // 等待视频加载
            console.log("Waiting for video player to load...");
            const videoPlayer = await waitForElement('#my-player_html5_api', 20000); // 使用 id 查找视频元素
            console.log("Video player loaded.");

            // 确保视频已准备好播放
            console.log("Waiting for video to be ready...");
            await new Promise((resolve) => {
                videoPlayer.oncanplay = () => {
                    console.log("Video is ready to play.");
                    resolve();
                };
            });

            // 播放视频
            videoPlayer.play();
            console.log("Video started playing.");

            // 等待视频播放结束
            console.log("Waiting for video to end...");
            await new Promise((resolve) => {
                videoPlayer.addEventListener('ended', () => {
                    console.log("Video ended.");
                    resolve();
                });
            });

            // 关闭提示框
            console.log("Waiting for the modal dialog to appear...");
            const closeDialogButton = await waitForElement('.ant-modal-close', 20000); // 等待关闭按钮出现
            if (closeDialogButton) {
                closeDialogButton.click();
                console.log("Closed dialog using close button.");
            } else {
                // 如果关闭按钮不存在,则寻找确定按钮
                const confirmButton = await waitForElement('.ant-btn', 20000); // 等待确定按钮出现
                if (confirmButton) {
                    confirmButton.click();
                    console.log("Closed dialog using confirm button.");
                }
            }

            // 关闭视频播放弹窗
            console.log("Waiting for the video modal to appear...");
            const closeVideoPopupButton = await waitForElement('.ant-modal-close', 20000); // 再次等待关闭按钮出现
            if (closeVideoPopupButton) {
                closeVideoPopupButton.click();
                console.log("Closed video popup using close button.");
            }

            // 继续查找下一个课程
            console.log("Sleeping for 1 second before continuing...");
            await sleep(1000); // 等待一段时间让页面重新加载
            console.log("Continuing with next course...");

            // 在每次递归调用前检查URL
            await checkUrlAndExecute();
        } catch (error) {
            console.error(error.message);
            // 在发生错误时重试
            console.log("Retrying in 5 seconds...");
            setTimeout(autoStudy, 5000);
        }
    }

    // 初始化时检查URL并执行
    checkUrlAndExecute();
})();

QingJ © 2025

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