日升通

一起学习

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         日升通
// @namespace    日升通
// @version      0.3
// @description  一起学习
// @author       zusheng
// @match        *://*.chaoxing.com/mycourse/studentstudy*
// @icon         https://www.google.com/s2/favicons?domain=chaoxing.com
// @grant        none
// @license MIT
// ==/UserScript==
(function () {
    'use strict';

    // 未完成的列表
    const undoneList = []

    /**
         * 查找是否有未完成的章节
         */
    function findUndoneList() {
        document.querySelectorAll(".chapter #coursetree ul li .posCatalog_level ul li").forEach(item => {
            if (item.querySelector('.prevTips') && item.querySelector('.prevTips').classList?.length > 0) {
                let content = item.querySelector('.prevTips').classList.value
                let reg = new RegExp(/icon_Completed/)
                if (!reg.test(content)) undoneList.push(item)
            } else {
                undoneList.push(item)
            }
        })
        console.log('找到' + undoneList.length + '个未完成章节')
        if (undoneList.length > 0) delUndoneList()
    }

    /**
         * 正式刷课
         */
    function delUndoneList() {
        // 选中目录
        console.log('@click, 点击切换任务')
        undoneList[0]?.querySelector('div > .posCatalog_name').click()

        // 完成一个,下一步
        function nextSteps() {
            undoneList.shift()
            if (undoneList.length > 0) {
                setTimeout(() => {
                    delUndoneList()
                }, 6 * 1000)
            }
        }

        setTimeout(async () => {
            // 本页iframe列表
            // const iframeList = document.documentElement.querySelector('#iframe').contentDocument.querySelectorAll('.ans-attach-online')
            const iframeList = document.documentElement.querySelector('#iframe').contentDocument.querySelectorAll('.wrap .ans-cc p')
            // DOM WORKS LI
            const workList = document.documentElement.querySelectorAll('#prev_tab .prev_ul li')
            // 执行完本页窗口,开始查看是否存在本章其余任务
            if (iframeList && iframeList.length > 0) {
                // 完成 iframe 窗口内的任务
                await rushWorks(iframeList)
                if (workList > 1) await nextWorks(workList)
                nextSteps()
            } else {
                // 下一章
                nextSteps()
            }
        }, 6 * 1000)
    }

    /**
         * 执行本页任务
         */
    function rushWorks(iframeList) {
        return new Promise(resolve => {
            let i = 0
            run()
            console.log('任务总数:', iframeList?.length)

            function run() {
                const currentIframe = iframeList[i]
                if (iframeList?.length > i) {
                    console.log('当前任务:', i + 1)
                    // DOM BUTTON
                    const pptFlag = currentIframe?.querySelector('iframe')?.contentDocument?.querySelector('#navigation #ext-gen1045')
                    // DOM VIDEO
                    const videoFlag = currentIframe?.querySelector('iframe')?.contentDocument?.querySelector('#video_html5_api')
                    // 标记是否完成
                    const complete = [...currentIframe?.querySelector('.ans-attach-ct').classList].includes('ans-job-finished')
                    if (pptFlag) {
                        checkPPT(pptFlag).then(() => run())
                    } else if (videoFlag) {
                        if (!complete) {
                            checkVideo(videoFlag).then(() => run())
                        } else {
                            i++
                            run()
                        }
                    }
                } else {
                    resolve()
                }
                i++;
            }
        })
    }

    /**
         * 检测并自动播放PPT
         * @return {Promise<unknown>}
         * @param pptFlag 下一页按钮
         */
    function checkPPT(pptFlag) {
        return new Promise(resolve => {
            let timer = setInterval(() => { // 一秒换一张PPT
                if (pptFlag?.style?.visibility === 'hidden') {
                    // 当翻到最后一页PPT时,清除定时器
                    clearInterval(timer)
                    resolve()
                } else {
                    // 下一页PPT
                    pptFlag.click()
                }
            }, 1000)
            })
    }

    /**
         * 检测并自动播放视频
         * @param videoFlag
         * @return {Promise<unknown>}
         */
    function checkVideo(videoFlag) {
        return new Promise(resolve => {
            setTimeout(() => {
                // 自动播放开始
                videoFlag.muted = true
                videoFlag.preload = "auto"
                // videoFlag.controls = true
                videoFlag.autoplay = true
                videoFlag.playbackRate = 2
                videoFlag.play()
                console.log('自动播放视频')
                // 视频播放结束监听
                videoFlag.addEventListener('ended', function () {
                    console.log('视频播放结束')
                    resolve()
                });
                // 视频出错监听
                videoFlag.addEventListener('error', function () {
                    console.log('视频播放错误')
                    resolve()
                })
            }, 3000)
        })
    }

    /**
         * 执行本章下一个任务
         * @return {Promise<unknown>}
         * @param workList
         */
    function nextWorks(workList) {
        return new Promise(resolve => {
            let i = 1
            run()

            function run() {
                if (workList.length >= i) {
                    const currentBtn = workList[i]
                    currentBtn.click()
                    setTimeout(async () => {
                        await rushWorks()
                        i++
                        run()
                    }, 1000 * 5)
                } else {
                    resolve()
                }
            }
        })
    }

    // 开始查找
    setTimeout(() => findUndoneList(), 5 * 1000)

})();