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";
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;
})();