-
Notifications
You must be signed in to change notification settings - Fork 6
Add isSubmitting flags to prevent multiple submissions #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
04654f7
35248ff
e1da47c
69dfadf
9373cb5
174a53f
4b9e5b3
5183741
45900b1
f560b8a
60ca02e
b4562c6
c20cd77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| // ==UserScript== | ||
| // @name XMOJ | ||
| // @version 2.7.2 | ||
| // @version 2.7.3 | ||
| // @description XMOJ增强脚本 | ||
| // @author @XMOJ-Script-dev, @langningchen and the community | ||
| // @namespace https://github/langningchen | ||
|
|
@@ -2667,11 +2667,17 @@ async function main() { | |
| ErrorMessage.innerText = "提交失败!请关闭脚本后重试!"; | ||
| Submit.disabled = false; | ||
| Submit.value = "提交"; | ||
| isSubmitting = false; | ||
| } | ||
| }) | ||
| }); | ||
|
|
||
| let isSubmitting = false; | ||
| Submit.addEventListener("click", async () => { | ||
|
Comment on lines
2668
to
2676
|
||
| if (isSubmitting) { | ||
| return; | ||
| } | ||
| isSubmitting = true; | ||
| PassCheck.style.display = "none"; | ||
| ErrorElement.style.display = "none"; | ||
| document.querySelector("#Submit").disabled = true; | ||
|
|
@@ -2721,6 +2727,7 @@ async function main() { | |
| freopenCodeField.setSize("100%", "auto"); | ||
| document.querySelector("#Submit").disabled = false; | ||
| document.querySelector("#Submit").value = "提交"; | ||
| isSubmitting = false; | ||
| return false; | ||
| } else if (RegExp("//.*freopen").test(Source)) { | ||
| PassCheck.style.display = ""; | ||
|
|
@@ -2729,6 +2736,7 @@ async function main() { | |
| ErrorMessage.innerText = "请不要注释freopen语句"; | ||
| document.querySelector("#Submit").disabled = false; | ||
| document.querySelector("#Submit").value = "提交"; | ||
| isSubmitting = false; | ||
| return false; | ||
| } | ||
| } | ||
|
|
@@ -2739,6 +2747,7 @@ async function main() { | |
| ErrorMessage.innerText = "源代码为空"; | ||
| document.querySelector("#Submit").disabled = false; | ||
| document.querySelector("#Submit").value = "提交"; | ||
| isSubmitting = false; | ||
| return false; | ||
| } | ||
| if (UtilityEnabled("CompileError")) { | ||
|
|
@@ -2761,6 +2770,7 @@ async function main() { | |
| ErrorMessage.innerText = "编译错误:\n" + Response.stderr.trim(); | ||
| document.querySelector("#Submit").disabled = false; | ||
| document.querySelector("#Submit").value = "提交"; | ||
| isSubmitting = false; | ||
| return false; | ||
| } else { | ||
| PassCheck.click(); | ||
|
Comment on lines
2770
to
2776
|
||
|
|
@@ -4378,22 +4388,30 @@ int main() | |
| } | ||
| } | ||
| }); | ||
| let isSubmittingPost = false; | ||
| SubmitElement.addEventListener("click", async () => { | ||
| if (isSubmittingPost) { | ||
| return; | ||
| } | ||
| isSubmittingPost = true; | ||
| ErrorElement.style.display = "none"; | ||
|
Comment on lines
+4391
to
4397
|
||
| let Title = TitleElement.value; | ||
| let Content = ContentElement.value; | ||
| let ProblemID = parseInt(SearchParams.get("pid")); | ||
| if (Title === "") { | ||
| TitleElement.classList.add("is-invalid"); | ||
| isSubmittingPost = false; | ||
| return; | ||
| } | ||
| if (Content === "") { | ||
| ContentElement.classList.add("is-invalid"); | ||
| isSubmittingPost = false; | ||
| return; | ||
| } | ||
| if (document.querySelector("#Board input:checked") === null) { | ||
| ErrorElement.innerText = "请选择要发布的板块"; | ||
| ErrorElement.style.display = "block"; | ||
| isSubmittingPost = false; | ||
| return; | ||
| } | ||
| SubmitElement.disabled = true; | ||
|
|
@@ -4407,6 +4425,7 @@ int main() | |
| }, (ResponseData) => { | ||
| SubmitElement.disabled = false; | ||
| SubmitElement.children[0].style.display = "none"; | ||
| isSubmittingPost = false; | ||
| if (ResponseData.Success == true) { | ||
| location.href = "https://www.xmoj.tech/discuss3/thread.php?tid=" + ResponseData.Data.PostID; | ||
| } else { | ||
|
|
@@ -4897,7 +4916,12 @@ int main() | |
| } | ||
| }); | ||
| }); | ||
| let isSubmittingReply = false; | ||
| SubmitElement.addEventListener("click", async () => { | ||
| if (isSubmittingReply) { | ||
| return; | ||
| } | ||
| isSubmittingReply = true; | ||
| ErrorElement.style.display = "none"; | ||
| SubmitElement.disabled = true; | ||
| SubmitElement.children[0].style.display = "inline-block"; | ||
|
Comment on lines
+4919
to
4927
|
||
|
|
@@ -4908,6 +4932,7 @@ int main() | |
| }, async (ResponseData) => { | ||
| SubmitElement.disabled = false; | ||
| SubmitElement.children[0].style.display = "none"; | ||
| isSubmittingReply = false; | ||
| if (ResponseData.Success == true) { | ||
| RefreshReply(); | ||
| ContentElement.value = ""; | ||
|
|
@@ -4944,4 +4969,4 @@ int main() | |
|
|
||
| main().then(r => { | ||
| console.log("XMOJ-Script loaded successfully!"); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isSubmittingis only reset in the non-redirect branch of the PassCheckfetch(...).then(...)chain. If the fetch rejects (network error/timeout) or an exception occurs before reaching this branch,Submitcan remain disabled andisSubmittingstays true, preventing retry. Add explicit error handling (e.g.,.catch(...)ortry/catch/finally) in the PassCheck submission flow to restore UI state and clear the flag.