Fix Violentmonkey SyntaxError: wrap async init block in IIFE#986
Conversation
Deploying xmoj-script-dev-channel with
|
| Latest commit: |
e7f94d8
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://a80c1d56.xmoj-script-dev-channel.pages.dev |
| Branch Preview URL: | https://copilot-fix-syntax-error-in.xmoj-script-dev-channel.pages.dev |
Reviewer's guide (collapsed on small PRs)Reviewer's GuideWrap the login/initialization sequence and main() call in an async IIFE to avoid top-level await SyntaxError in Violentmonkey, while preserving access to shared state via pre-declared variables and adding explicit error logging for initialization failures. Sequence diagram for async IIFE-based initialization and error handlingsequenceDiagram
actor Violentmonkey
participant Userscript
participant AsyncInitIIFE
participant XmojServer
participant Console
Violentmonkey->>Userscript: Load XMOJ.user.js
Userscript->>AsyncInitIIFE: (async () => { ... })()
activate AsyncInitIIFE
AsyncInitIIFE->>XmojServer: fetch(loginpage.php)
XmojServer-->>AsyncInitIIFE: Response
AsyncInitIIFE->>AsyncInitIIFE: await fetch(...).then(...)
AsyncInitIIFE->>Userscript: Set SearchParams, CurrentUsername, initTheme
AsyncInitIIFE->>Userscript: main()
Userscript-->>AsyncInitIIFE: Promise resolved
deactivate AsyncInitIIFE
rect rgb(255,240,240)
AsyncInitIIFE--xConsole: console.error("[XMOJ-Script] Initialization error:", e) (on .catch)
end
File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="XMOJ.user.js" line_range="6355-6358" />
<code_context>
main().then(r => {
console.log("XMOJ-Script loaded successfully!");
});
+})().catch(e => {
+ console.error("[XMOJ-Script] Initialization error:", e);
+});
</code_context>
<issue_to_address>
**issue (bug_risk):** The async IIFE’s `.catch` won’t catch errors from `main()`’s async work.
Since `main()` is not awaited, the IIFE completes once the `.then` handler is registered, so any later rejection from `main()` (or its awaits) bypasses the outer `catch`. To centralize async init error handling, you can instead do something like:
```js
try {
await main();
console.log("XMOJ-Script loaded successfully!");
} catch (e) {
console.error("[XMOJ-Script] Initialization error:", e);
}
```
This preserves the success log while ensuring async failures from `main()` are properly caught and logged.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| main().then(r => { | ||
| console.log("XMOJ-Script loaded successfully!"); | ||
| }); | ||
| })().catch(e => { |
There was a problem hiding this comment.
issue (bug_risk): The async IIFE’s .catch won’t catch errors from main()’s async work.
Since main() is not awaited, the IIFE completes once the .then handler is registered, so any later rejection from main() (or its awaits) bypasses the outer catch. To centralize async init error handling, you can instead do something like:
try {
await main();
console.log("XMOJ-Script loaded successfully!");
} catch (e) {
console.error("[XMOJ-Script] Initialization error:", e);
}This preserves the success log while ensuring async failures from main() are properly caught and logged.
There was a problem hiding this comment.
Pull request overview
This PR fixes a Violentmonkey compatibility issue where the userscript was being executed as a classic script (not an ES module), causing a hard SyntaxError due to top-level await. The initialization sequence is moved into an async IIFE, and a top-level .catch(...) is added to surface init failures.
Changes:
- Wrapped the async initialization flow (login-status fetch through
main()) in an async IIFE to eliminate top-levelawait. - Pre-declared
CurrentUsername,initTheme, andSearchParamsat top-level so earlier-defined closures can still reference them. - Added IIFE-level error logging via
.catch(...)to help diagnose initialization failures.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Tested. It can work!!! |
3e71520 to
3263752
Compare
3263752 to
3be870f
Compare
Signed-off-by: Shan Wenxiao <seanoj_noreply@yeah.net>
|
I believe that you may encounter some issues using violentmonkey |
|
There are a lot of bugs I think |
|
Also you should probably run fmt |
I've downloaded it and tried somehow, it is still workable though. As of fmt, next time in another pr maybe |
Violentmonkey executes userscripts as classic scripts (not ES modules), so the top-level
awaitat line 916 causes a hardSyntaxError, breaking the entire script.Changes
main().then(...)is now inside(async () => { ... })().catch(...), makingawaitvalid without requiring module semanticsCurrentUsername,initTheme, andSearchParamsare declared at the top level before the IIFE so that closures defined earlier (e.g.PeriodicCloudSync,GetContestProblemList) can still reference them.catch()on the IIFE to log initialization failures to the consoleSummary by Sourcery
Wrap the userscript’s asynchronous initialization logic in an async IIFE to avoid top-level await syntax errors in Violentmonkey while preserving access to shared state.
Bug Fixes:
Enhancements: