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