From e89eb9a97073f2e95f606ea1813c2f29b6f57c02 Mon Sep 17 00:00:00 2001 From: Bluemangoo Date: Sat, 24 May 2025 16:15:25 +0800 Subject: [PATCH] auto mode --- ...t-cource-copy.js => buct-cource-helper.js} | 89 +++++++++++++++---- 1 file changed, 72 insertions(+), 17 deletions(-) rename script/{buct-cource-copy.js => buct-cource-helper.js} (65%) diff --git a/script/buct-cource-copy.js b/script/buct-cource-helper.js similarity index 65% rename from script/buct-cource-copy.js rename to script/buct-cource-helper.js index dc87778..d65e200 100644 --- a/script/buct-cource-copy.js +++ b/script/buct-cource-helper.js @@ -15,8 +15,11 @@ const HOST = "https://local.bluemangoo.net:3443"; - let question = { - id: "" + let stat = { + question: { + id: "" + }, + autoMode: false }; class Poll { @@ -78,11 +81,12 @@ navigator.clipboard.writeText(getContent()); } - function answerQuestion(id, answer) { - if (id !== question.id) { + async function answerQuestion(id, answer) { + if (id !== stat.question.id) { return; } aiButton.value = "?"; + let answered = false; const answers = answer.split("\n"); const children = getAnswerElement().children[0].children; for (const child of children) { @@ -91,6 +95,15 @@ } if (answers.includes(child.innerText.replaceAll("\t", ""))) { child.children[0].children[0].click(); + answered = true; + } + } + if (answered && stat.autoMode) { + const submitButton = getAnswerElement().nextElementSibling.children[1]; + submitButton.click(); + await new Promise((resolve) => setTimeout(resolve, 500)); + if (!allDone()) { + aiButton.click(); } } } @@ -108,19 +121,47 @@ }); const data = await req.json(); if (data.data.questionId) { - question.id = data.data.questionId; + stat.question.id = data.data.questionId; } aiButton.value = "Zzz"; } + function getAutoText() { + return stat.autoMode ? "A*" : "A"; + } + + function toggleAutoMode() { + stat.autoMode = !stat.autoMode; + autoButton.value = getAutoText(); + } + + function allDone() { + const element = document + .getElementById("mainFrame") + .contentDocument.getElementsByClassName("infotable")[1]; + return !element.innerText.toString().includes("未作答"); + } + const flag = { copyButton: false, aiButton: false }; let aiButton; + let autoButton; let poll; + function newButton(document, value, onclick) { + const button = document.createElement("input"); + button.value = value; + button.style = "margin-left: 10px;"; + button.type = "button"; + if (onclick) { + button.onclick = onclick; + } + return button; + } + function createButton(addAiButton = false) { const base = document.getElementById("mainFrame"); const win = base.contentWindow; @@ -130,22 +171,15 @@ win.answerQuestion = answerQuestion; win.copyToClipboard = copyToClipboard; if (!flag.copyButton) { - const button = doc.createElement("input"); - button.value = "复制"; - button.style = "margin-left: 10px;"; - button.type = "button"; - button.onclick = copyToClipboard; + const button = newButton(doc, "复制", copyToClipboard); doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(button); flag.copyButton = true; } if (addAiButton && !flag.aiButton) { - const button = doc.createElement("input"); - aiButton = button; - button.value = "?"; - button.style = "margin-left: 10px;"; - button.type = "button"; - button.onclick = ask; - doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(button); + aiButton = newButton(doc, "?", ask); + doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(aiButton); + autoButton = newButton(doc, getAutoText(), toggleAutoMode); + doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(autoButton); flag.aiButton = true; } } @@ -161,10 +195,31 @@ function stopPolling() { if (poll) { poll.polling = false; + flag.copyButton = false; + flag.aiButton = false; } } GM_registerMenuCommand("添加复制按钮", createButton); GM_registerMenuCommand("初始化ai桥", initAiBridge); GM_registerMenuCommand("停止轮询", stopPolling); + + window.stat = stat; + window.getContent = getContent; + window.getAnswerElement = getAnswerElement; + window.answerQuestion = answerQuestion; + window.copyToClipboard = copyToClipboard; + window.ask = ask; + window.toggleAutoMode = toggleAutoMode; + window.allDone = allDone; + window.flag = flag; + window.initAiBridge = initAiBridge; + window.stopPolling = stopPolling; + window.aiButton = aiButton; + window.autoButton = autoButton; + window.newButton = newButton; + window.getAutoText = getAutoText; + window.createButton = createButton; + window.initAiBridge = initAiBridge; + window.stopPolling = stopPolling; })();