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.3
// @author      aliha
// @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);
	}

  // 获取进度,自动切换小节
	function getProcess () {
		let jindu = document.querySelector("#a span[du-html=sumschedule]"); // 获取总进度
		if (jindu) {

			if (jindu.innerText === "100.00") {
				console.info("本课程已完成")
			} else {

				let dangqian = document.querySelector(".videoLi.active"); // 获取当前进度
				if (dangqian.innerText.match(/[0-9]+%/)[0] == "100%") {

					console.info("下一节");
					// 切换到下一节

					const ulElement = document.querySelector('.pt5'); //// 获取播放列表
					if (ulElement) {
						const childElements = ulElement.children;

						for (const child of childElements) { // 当前小节播放完毕,播放下一节
							if (dangqian.innerText.match(/[0-9]+%/)[0] != "100%") {
								const pauseBtn = document.querySelector('button[type="button"].pv-playpause.pv-iconfont.pv-icon-btn-play');
								if (pauseBtn) {
									pauseBtn.click();
								}
							}
						}
					}


					// document.querySelector(".pt5 [class=progress-bar]").parentElement.parentElement.click(); // 暂停
				}
			}
		}

	}

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

		for (let i = res_ls.length - 1; i != 0; i--) {
			await delay(6)

			if (flag) {
				console.log("回答正确")
				break;
			}

			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
				}

			}

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

		}
	}


	// 主函数
	async function main () {
		console.info("开始答题")
		await getProcess()

		// 自动答题
		if (getItems()) {
			let inputs = getItems()
			const array = Array.from({
				length: inputs.length
			}, (_, index) => index);
			let num = generateCombinations(array)
			await answer(num)
			console.info("答题脚本执行完毕")
		} else {
			console.info("未检测到答题卡,答题脚本执行完毕")
		}
	}


	setInterval(main, 180000); // 定时3分钟运行一次


})();