Greasy Fork

济南专业技师人员继续教育

继续教育公需科目专业科目辅助|自动答题|

目前为 2023-09-06 提交的版本。查看 最新版本

// ==UserScript==
// @name        济南专业技师人员继续教育
// @namespace   Violentmonkey Scripts
// @match       *://*.ghlearning.com/*
// @match       http://221.214.69.254:9091/*
// @grant       none
// @version     0.1.2
// @author      aliha
// @homepageURL  https://greasyfork.org/zh-CN/scripts/474645-%E6%B5%8E%E5%8D%97%E4%B8%93%E4%B8%9A%E6%8A%80%E5%B8%88%E4%BA%BA%E5%91%98%E7%BB%A7%E7%BB%AD%E6%95%99%E8%82%B2
// @description 继续教育公需科目专业科目辅助|自动答题|
// @run-at       document-end
// ==/UserScript==
(function () {
    // 延时s秒
    function delay(s) {
        return new Promise(resolve => setTimeout(resolve, s * 1000));
    }


    // 检测答题元素,获取选项
    function getItems() {
        if (document.querySelector(".pv-ask-modal")) {
            let qusCard = document.querySelector(".pv-ask-modal")
            let inputs = qusCard.querySelectorAll("input")
            return inputs
        }

        return null
    }

    // 生成数组所有的穷举组合并剔除空数组
    function generateCombinations(arr) {
        const combinations = [[]];

        // 遍历数组元素
        for (let i = 0; i < arr.length; i++) {
            const currentLength = combinations.length;

            // 遍历当前已生成的组合
            for (let j = 0; j < currentLength; j++) {
                const currentCombination = combinations[j];

                // 生成新的组合,包含当前数组元素
                const newCombination = currentCombination.concat(arr[i]);

                // 如果组合不为空,则将新组合添加到二维数组中
                if (newCombination.length > 0) {
                    combinations.push(newCombination);
                }
            }
        }

        // 剔除空数组
        return combinations.filter(combination => combination.length > 1);
    }


    // 挨个尝试,检测到回答错误继续,检测到回答正确跳出,同时清空答案
    async function answer(res_ls) {
        let flag = false;

        for (let i = res_ls.length - 1; i != 0; i--) {
            await delay(6) // 延时6秒防止浏览器卡死

            let inputs = getItems() // 因为每次答错,会重载选项,必须每次重新获取

            console.log(`尝试第${res_ls.length - i}次作答`)
            console.log(res_ls[i])

            for (let j = 0; j < res_ls[i].length; j++) {
              try{
                inputs[res_ls[i][j]].checked = true // 按答案组合勾选选项
              }catch(err){
                flag = true;    // 如果出错可能是答对了,跳出两层循环
                break
              }

            }

          if(flag){
            break; // 跳出循环
          }

            // 提交答案,正确跳出循环,错误继续尝试
            let button = document.querySelector('button.pv-ask-submit[data-type="pvSubmit"]');
            await delay(1)
            button.click()
        }
    }


    // 主函数
    async function main() {
        console.log("开始答题")
        if (getItems()) {
            let inputs = getItems()
            const array = Array.from({ length: inputs.length }, (_, index) => index);
            let num = generateCombinations(array)
            await answer(num)
            console.log("答题脚本执行完毕")
        } else {
            console.log("未检测到答题卡,答题脚本执行完毕")
        }
    }


    setInterval(main, 300000); // 5分钟运行一次


})();