关轮询
This commit is contained in:
parent
e28793ac5b
commit
cb6c5459e1
@ -19,29 +19,40 @@
|
||||
id: ""
|
||||
};
|
||||
|
||||
async function poll() {
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(`${HOST}/polling?env=course`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
return poll();
|
||||
class Poll {
|
||||
polling = true;
|
||||
|
||||
constructor() {
|
||||
this.poll();
|
||||
}
|
||||
poll();
|
||||
try {
|
||||
const data = await res.json();
|
||||
if (data.status !== "nothing") {
|
||||
const { questionID, answer } = data.data;
|
||||
console.log("回答", questionID, "\n", answer);
|
||||
answerQuestion(questionID, answer);
|
||||
|
||||
async poll() {
|
||||
if (!this.polling) {
|
||||
return;
|
||||
}
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(`${HOST}/polling?env=course`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
return poll();
|
||||
}
|
||||
poll();
|
||||
try {
|
||||
const data = await res.json();
|
||||
if (data.status !== "nothing") {
|
||||
const { questionID, answer } = data.data;
|
||||
console.log("回答", questionID, "\n", answer);
|
||||
answerQuestion(questionID, answer);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -71,7 +82,7 @@
|
||||
if (id !== question.id) {
|
||||
return;
|
||||
}
|
||||
aiButton.value = "?"
|
||||
aiButton.value = "?";
|
||||
const answers = answer.split("\n");
|
||||
const children = getAnswerElement().children[0].children;
|
||||
for (const child of children) {
|
||||
@ -108,6 +119,7 @@
|
||||
};
|
||||
|
||||
let aiButton;
|
||||
let poll;
|
||||
|
||||
function createButton(addAiButton = false) {
|
||||
const base = document.getElementById("mainFrame");
|
||||
@ -140,9 +152,19 @@
|
||||
|
||||
function initAiBridge() {
|
||||
createButton(true);
|
||||
poll();
|
||||
if (poll) {
|
||||
poll.polling = false;
|
||||
}
|
||||
poll = new Poll();
|
||||
}
|
||||
|
||||
function stopPolling() {
|
||||
if (poll) {
|
||||
poll.polling = false;
|
||||
}
|
||||
}
|
||||
|
||||
GM_registerMenuCommand("添加复制按钮", createButton);
|
||||
GM_registerMenuCommand("初始化ai桥", initAiBridge);
|
||||
GM_registerMenuCommand("停止轮询", stopPolling);
|
||||
})();
|
||||
|
Loading…
x
Reference in New Issue
Block a user