-
Notifications
You must be signed in to change notification settings - Fork 6
Fix Problem Switcher Not Update #930
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -533,6 +533,29 @@ let RequestAPI = (Action, Data, CallBack) => { | |||||
| } | ||||||
| }; | ||||||
|
|
||||||
| unsafeWindow.GetContestProblemList = async function(RefreshList) { | ||||||
| try { | ||||||
| const contestReq = await fetch("https://www.xmoj.tech/contest.php?cid=" + SearchParams.get("cid")); | ||||||
| const res = await contestReq.text(); | ||||||
| if (contestReq.status === 200 && res.indexOf("比赛尚未开始或私有,不能查看题目。") === -1) { | ||||||
| const parser = new DOMParser(); | ||||||
| const dom = parser.parseFromString(res, "text/html"); | ||||||
| const rows = (dom.querySelector("#problemset > tbody")).rows; | ||||||
| let problemList = []; | ||||||
| for (let i = 0; i < rows.length; i++) { | ||||||
| problemList.push({ | ||||||
| "title": rows[i].children[2].innerText, | ||||||
| "url": rows[i].children[2].children[0].href | ||||||
| }); | ||||||
| } | ||||||
| localStorage.setItem("UserScript-Contest-" + SearchParams.get("cid") + "-ProblemList", JSON.stringify(problemList)); | ||||||
| if (RefreshList) location.reload(); | ||||||
| } | ||||||
| } catch (e) { | ||||||
| console.error(e); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // WebSocket Notification System | ||||||
| let NotificationSocket = null; | ||||||
| let NotificationSocketReconnectAttempts = 0; | ||||||
|
|
@@ -2270,22 +2293,8 @@ async function main() { | |||||
| } | ||||||
| let ContestProblemList = localStorage.getItem("UserScript-Contest-" + SearchParams.get("cid") + "-ProblemList"); | ||||||
| if (ContestProblemList == null) { | ||||||
| const contestReq = await fetch("https://www.xmoj.tech/contest.php?cid=" + SearchParams.get("cid")); | ||||||
| const res = await contestReq.text(); | ||||||
| if (contestReq.status === 200 && res.indexOf("比赛尚未开始或私有,不能查看题目。") === -1) { | ||||||
| const parser = new DOMParser(); | ||||||
| const dom = parser.parseFromString(res, "text/html"); | ||||||
| const rows = (dom.querySelector("#problemset > tbody")).rows; | ||||||
| let problemList = []; | ||||||
| for (let i = 0; i < rows.length; i++) { | ||||||
| problemList.push({ | ||||||
| "title": rows[i].children[2].innerText, | ||||||
| "url": rows[i].children[2].children[0].href | ||||||
| }); | ||||||
| } | ||||||
| localStorage.setItem("UserScript-Contest-" + SearchParams.get("cid") + "-ProblemList", JSON.stringify(problemList)); | ||||||
| ContestProblemList = JSON.stringify(problemList); | ||||||
| } | ||||||
| unsafeWindow.GetContestProblemList(false); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Await the async cache refresh before reading Prompt for AI agents
Suggested change
|
||||||
| ContestProblemList = localStorage.getItem("UserScript-Contest-" + SearchParams.get("cid") + "-ProblemList"); | ||||||
| } | ||||||
|
|
||||||
| let problemSwitcher = document.createElement("div"); | ||||||
|
Comment on lines
2294
to
2300
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (bug_risk): On failure of |
||||||
|
|
@@ -2308,6 +2317,7 @@ async function main() { | |||||
| problemSwitcher.style.flexDirection = "column"; | ||||||
|
|
||||||
| let problemList = JSON.parse(ContestProblemList); | ||||||
| problemSwitcher.innerHTML += `<a href="javascript:void(0)" onclick="GetContestProblemList(true)" title="刷新列表" class="mb-2" style="text-align: center;" active>刷新</a>`; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-document-method): User controlled data in methods like Source: opengrep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (javascript.browser.security.insecure-innerhtml): User controlled data in a Source: opengrep |
||||||
| for (let i = 0; i < problemList.length; i++) { | ||||||
| let buttonText = ""; | ||||||
| if (i < 26) { | ||||||
|
|
||||||
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.
issue (bug_risk): The async
GetContestProblemListcall is not awaited before reading fromlocalStorage, which can lead to a race condition.Previously the fetch and
localStorage.setItemcompleted inline beforeContestProblemListwas used. NowGetContestProblemListisasync, butunsafeWindow.GetContestProblemList(false);is called withoutawaitandlocalStorageis read immediately afterward, soContestProblemListmay still benull, causingJSON.parse(ContestProblemList)to throw. Please eitherawait unsafeWindow.GetContestProblemList(false);(marking the callerasyncif needed) before reading fromlocalStorage, or refactorGetContestProblemListto return the list directly instead of going throughlocalStorage.