auto mode

This commit is contained in:
Bluemangoo 2025-05-24 16:15:25 +08:00
parent 4ba1085535
commit e89eb9a970
Signed by: Bluemangoo
GPG Key ID: F2F7E46880A1C4CF

View File

@ -15,8 +15,11 @@
const HOST = "https://local.bluemangoo.net:3443"; const HOST = "https://local.bluemangoo.net:3443";
let question = { let stat = {
id: "" question: {
id: ""
},
autoMode: false
}; };
class Poll { class Poll {
@ -78,11 +81,12 @@
navigator.clipboard.writeText(getContent()); navigator.clipboard.writeText(getContent());
} }
function answerQuestion(id, answer) { async function answerQuestion(id, answer) {
if (id !== question.id) { if (id !== stat.question.id) {
return; return;
} }
aiButton.value = ""; aiButton.value = "";
let answered = false;
const answers = answer.split("\n"); const answers = answer.split("\n");
const children = getAnswerElement().children[0].children; const children = getAnswerElement().children[0].children;
for (const child of children) { for (const child of children) {
@ -91,6 +95,15 @@
} }
if (answers.includes(child.innerText.replaceAll("\t", ""))) { if (answers.includes(child.innerText.replaceAll("\t", ""))) {
child.children[0].children[0].click(); 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(); const data = await req.json();
if (data.data.questionId) { if (data.data.questionId) {
question.id = data.data.questionId; stat.question.id = data.data.questionId;
} }
aiButton.value = "Zzz"; 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 = { const flag = {
copyButton: false, copyButton: false,
aiButton: false aiButton: false
}; };
let aiButton; let aiButton;
let autoButton;
let poll; 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) { function createButton(addAiButton = false) {
const base = document.getElementById("mainFrame"); const base = document.getElementById("mainFrame");
const win = base.contentWindow; const win = base.contentWindow;
@ -130,22 +171,15 @@
win.answerQuestion = answerQuestion; win.answerQuestion = answerQuestion;
win.copyToClipboard = copyToClipboard; win.copyToClipboard = copyToClipboard;
if (!flag.copyButton) { if (!flag.copyButton) {
const button = doc.createElement("input"); const button = newButton(doc, "复制", copyToClipboard);
button.value = "复制";
button.style = "margin-left: 10px;";
button.type = "button";
button.onclick = copyToClipboard;
doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(button); doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(button);
flag.copyButton = true; flag.copyButton = true;
} }
if (addAiButton && !flag.aiButton) { if (addAiButton && !flag.aiButton) {
const button = doc.createElement("input"); aiButton = newButton(doc, "", ask);
aiButton = button; doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(aiButton);
button.value = ""; autoButton = newButton(doc, getAutoText(), toggleAutoMode);
button.style = "margin-left: 10px;"; doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(autoButton);
button.type = "button";
button.onclick = ask;
doc.getElementsByClassName("navigation")[0].childNodes[1].appendChild(button);
flag.aiButton = true; flag.aiButton = true;
} }
} }
@ -161,10 +195,31 @@
function stopPolling() { function stopPolling() {
if (poll) { if (poll) {
poll.polling = false; poll.polling = false;
flag.copyButton = false;
flag.aiButton = false;
} }
} }
GM_registerMenuCommand("添加复制按钮", createButton); GM_registerMenuCommand("添加复制按钮", createButton);
GM_registerMenuCommand("初始化ai桥", initAiBridge); GM_registerMenuCommand("初始化ai桥", initAiBridge);
GM_registerMenuCommand("停止轮询", stopPolling); 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;
})(); })();