GCX自动化(仅供学习)

功能说明:1、当前视频播放完毕自动播放下一个视频。2、每章的最后一节播放到可以做章节练习题时会自动跳转到章节练习题。3、练习题时自动填写答案

// ==UserScript==
// @name         GCX自动化(仅供学习)
// @namespace    your-namespace
// @version      2.6
// @description  功能说明:1、当前视频播放完毕自动播放下一个视频。2、每章的最后一节播放到可以做章节练习题时会自动跳转到章节练习题。3、练习题时自动填写答案
// @match        https://chengkao.gdcxxy.net/gdcx/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    const observer = new MutationObserver((mutationsList) => {
        for (let mutation of mutationsList) {
            if (mutation.type === 'childList') {
                for (let node of mutation.addedNodes) {
                    if (node.id === 'layui-layer2' || node.id === 'layui-layer1') {
                        const aTags = node.getElementsByTagName('a');
                        for (let aTag of aTags) {
                            if (aTag.classList.contains('layui-layer-btn0')) {
                                aTag.click();
                                setTimeout(() => {
                                    const currentUrl = window.location.href;
                                    let nextUrl = document.querySelector('#jjwikiid');
                                    let currentJjwikiid = parseInt(nextUrl.value) + 1;
                                    let newUrl = currentUrl;
                                    if (currentUrl.includes('jjwikiid=')) {
                                        newUrl = currentUrl.replace(/jjwikiid=\d+/, `jjwikiid=${currentJjwikiid}`);
                                    } else {
                                        newUrl += `&jjwikiid=${currentJjwikiid}`;
                                    }
                                    window.location.href = newUrl;
                                }, 1000); // 延迟1秒后重定向
                                break;
                            }
                        }
                    }else if (node.id === 'tcaptcha-transform' || node.id === 't_mask') {
                        node.style.display = 'none';
                        setTimeout(() => {
                            const video = document.querySelector('video');
                            if (video) {
                                video.muted = true;
                                video.play()
                            }
                        }, 2000); // 延迟2秒后模拟点击并播放视频
                    }
                }
            }
        }
    });
    observer.observe(document, { childList: true, subtree: true });
    function checkVideoProgress(video,callback){
        function check() {
            if (video.paused) {
                callback(false);
            } else if (video.currentTime + 20 >= video.duration) {
                callback(true);
            } else {
                setTimeout(check, 1000);
            }
        }
        check();
    }
    window.addEventListener('load', () => {
        let flag = false
        // 根据获取路径去判断是在做题还是在刷视频
        const url = window.location.href;
        let timer = null;
        // kcdm就代表在刷视频
        if (url.includes('kcdm')){
            // 判断是不是这一章的最后一集
            const isLast = document.querySelector('#isnot_st').value
            if(isLast === '1'){
                const video = document.querySelector('video');
                timer = setInterval(() => {
                    checkVideoProgress(video,(result) => {
                        flag = result
                        console.log(flag)
                        if(flag){
                            // 获取跳转过去的题目id
                            const typeId = document.querySelector('#TypeId').value
                            setTimeout(() => {
                                const currentUrl = window.location.href;
                                const nextUrl = document.querySelector('#jjwikiid');
                                const currentJjwikiid = nextUrl.value;
                                const newJjwikiid = parseInt(currentJjwikiid);
                                const finalUrl = `https://chengkao.gdcxxy.net/gdcx/wiki_exam_zy.php?TypeId=${typeId}&jjwikiid=${newJjwikiid}`;
                                window.location.href = finalUrl;
                            }, 1000); // 延迟1秒后重定向
                            clearInterval(timer)
                        }
                    })
                }, 4000);
                // return
            }
            // 延迟执行以免视频未加载就去播放
            setTimeout(() => {
                const video = document.querySelector('video');
                if (video) {
                    try{
                        // mainVideo.click();
                        video.muted = true;
                        video.play();
                    }catch(error){
                        setTimeout(()=>{
                            // mainVideo.click();
                            video.muted = true;
                            video.play();
                        },2000)
                    }
                }
            }, 5000); // 延迟5秒后模拟点击并播放视频
        }else if(url.includes('TypeId')){
            // TypeId 代表现在是在做题目
            // 获取题目答案
            const textGreen = document.querySelector('.text-green');
            const pContent = textGreen.querySelector('p').textContent;
            const pContentArray = pContent.split(':');
            const secondPart = pContentArray[1];
            console.log('正确答案为:'+ secondPart)
            // 题目类型
            const single_choice = document.querySelector('.single_choice').textContent
            const type = single_choice.split('【')[1].split('】')[0]
            console.log(type)
            if( type === '单项选择题'|| type === '多项选择题' || type === '判断题'){
                const singleUl = document.querySelector('.single_ul');
                const labels = singleUl.querySelectorAll('li label');
                labels.forEach(label => {
                    const labelContent = label.textContent;
                    const labelContentArray = labelContent.split('、');
                    // 多选题
                    const comparisonPart = labelContentArray[0].trim();
                    if (secondPart.includes(comparisonPart)) {
                        label.click();
                    }
                    // 判断题
                    const panduan = labelContentArray[1].trim()
                    if(panduan === secondPart){
                        label.click()
                    }
                });
            }else if(type === '简答题' || type === '名词解释题'){
                const form_control = document.querySelector('.form-control')
                form_control.value = pContent
                const submit = document.querySelector('.btn-hero')
                const next = document.querySelector('.active')

                // 获取跳转到题目的kcdm
                const tittwo = document.querySelector('.tittwoBox span').textContent
                const kcdm = tittwo.match(/[A-Z0-9]+/)[0]
                const nextUrl = document.querySelector('#jjwikiid');
                const currentJjwikiid = nextUrl.value;
                const newJjwikiid = parseInt(currentJjwikiid) + 1;
                setTimeout(()=>{
                    submit.click()
                    setTimeout(()=>{
                        next.click()
                        const finalUrl = `https://chengkao.gdcxxy.net/gdcx/jp_wiki_study.php?kcdm=${kcdm}&jjwikiid=${newJjwikiid}`;
                        window.location.href = finalUrl;
                    },2000)
                },2000)
                return
            }
            const submit = document.querySelector('.btn-hero')
            const next = document.querySelector('.active')
            setTimeout(()=>{
                submit.click()
                setTimeout(()=>{
                    next.click()
                },2000)
            },2000)
        }

    })
})();