Skip to content

sync: dev to extern-contrib#987

Merged
PythonSmall-Q merged 11 commits into
extern-contribfrom
dev
May 30, 2026
Merged

sync: dev to extern-contrib#987
PythonSmall-Q merged 11 commits into
extern-contribfrom
dev

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot commented May 30, 2026

sync-branches: New code has just landed in dev, so let's bring extern-contrib up to speed!

Summary by Sourcery

Sync extern-contrib userscript with latest dev changes and improve initialization flow.

New Features:

  • Allow early-returning from auto-login and profile checks to avoid running script logic when not logged in or redirected.

Bug Fixes:

  • Prevent CurrentUsername, SearchParams, and initTheme from being undefined by declaring them before asynchronous initialization.
  • Ensure script initialization works under Violentmonkey by wrapping async startup logic in an async IIFE instead of relying on top-level await.
  • Avoid accessing #profile when it is absent by checking and returning early, preventing errors on pages without a profile element.

Enhancements:

  • Improve script startup logging and error handling by awaiting main and capturing initialization errors in a catch block.
  • Bump script and package versions from 3.4.6 to 3.5.1 to reflect the latest changes.

Summary by cubic

Sync extern-contrib with dev to release 3.5.1. Fix Violentmonkey top-level await error, harden init/login flow, and update version and release metadata (3.5.0/3.5.1).

  • Bug Fixes
    • Wrapped async init in an IIFE to make await valid in Violentmonkey.
    • Predeclared CurrentUsername, initTheme, and SearchParams; added early returns for login/profile checks to avoid undefined access and redirect loops.
    • Awaited main() and added init error logging.
    • Bumped to 3.5.1 and added 3.5.0/3.5.1 entries in Update.json.

Written for commit 2993f85. Summary will update on new commits.

Review in cubic

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented May 30, 2026

Reviewer's Guide

Synchronizes extern-contrib with dev by wrapping the main userscript initialization in an async IIFE for Violentmonkey compatibility, fixing global variable scoping for username/theme/search params, hardening login/profile handling, adjusting main() invocation and error logging, and bumping the script/package versions to 3.5.1.

Sequence diagram for async IIFE initialization, login handling, and main invocation

sequenceDiagram
    actor User
    participant Browser
    participant Userscript
    participant XMOJServer

    User->>Browser: Open xmoj.tech page
    Browser->>Userscript: Execute XMOJ.user.js
    Userscript->>Userscript: (async IIFE) start
    Userscript->>XMOJServer: fetch(loginpage.php)
    XMOJServer-->>Userscript: loginStatus

    alt UtilityEnabled AutoLogin and login prompt
        Userscript->>Browser: location.href = loginpage.php
        Userscript-->>Userscript: return
    end

    Userscript->>Userscript: SearchParams = new URLSearchParams(location.search)
    Userscript->>Browser: profileElement = document.querySelector(#profile)

    alt profileElement is null and not logined
        Userscript->>Browser: location.href = loginpage.php
        Userscript-->>Userscript: return
    else profileElement exists
        Userscript->>Userscript: CurrentUsername = profileElement.innerText
    end

    Userscript->>Userscript: await main()

    alt main succeeds
        Userscript->>Browser: console.log(XMOJ-Script loaded successfully!)
    else main throws
        Userscript->>Browser: console.error([XMOJ-Script] Initialization error:, e)
    end
Loading

File-Level Changes

Change Details Files
Wrap userscript initialization in an async IIFE to allow top-level await and add robust initialization error handling.
  • Enclose the login/profile/theme initialization and main() call in an immediately-invoked async function expression
  • Replace main().then(...) with await main() followed by a success log
  • Add a top-level catch handler that logs initialization errors with a distinctive prefix
XMOJ.user.js
Fix global variable scoping so values set in the async init block are visible to previously defined closures.
  • Pre-declare CurrentUsername, initTheme, and SearchParams as mutable globals before any closures that may capture them
  • Convert local declarations of SearchParams and initTheme inside the init block into assignments to the pre-declared globals
  • Adjust CurrentUsername initialization to assign to the pre-declared global instead of redeclaring
XMOJ.user.js
Improve login/profile flow to avoid undefined usernames and unnecessary redirects.
  • Compute loginStatus and logined as before but ensure early returns when triggering login redirects
  • Store the last page before redirecting to login and return immediately to prevent further script execution
  • Introduce a cached profileElement variable, redirect to login only when profile is missing and user is not logged in, and early-return when profile is absent
  • Derive CurrentUsername from profileElement and sanitize it as before
XMOJ.user.js
Version bump to 3.5.1 to reflect new behavior.
  • Update the @Version metadata field in the userscript header to 3.5.1
  • Update package.json version from 3.4.6 to 3.5.1
XMOJ.user.js
package.json
Update.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying xmoj-script-dev-channel with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2993f85
Status: ✅  Deploy successful!
Preview URL: https://a1cb1e1b.xmoj-script-dev-channel.pages.dev

View logs

@PythonSmall-Q PythonSmall-Q merged commit a2f5278 into extern-contrib May 30, 2026
7 checks passed
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The new async initialization block assumes the fetch for loginStatus always succeeds; consider wrapping it (and the await main() call) in a more granular try/catch so that network or runtime failures don’t silently prevent the rest of the script from running.
  • The early return when profileElement is null will permanently skip script initialization on pages where #profile is not yet in the DOM (e.g., if the script runs before it’s rendered); consider delaying this check until DOMContentLoaded or otherwise ensuring the element is expected to exist before returning.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new async initialization block assumes the `fetch` for `loginStatus` always succeeds; consider wrapping it (and the `await main()` call) in a more granular try/catch so that network or runtime failures don’t silently prevent the rest of the script from running.
- The early `return` when `profileElement` is `null` will permanently skip script initialization on pages where `#profile` is not yet in the DOM (e.g., if the script runs before it’s rendered); consider delaying this check until DOMContentLoaded or otherwise ensuring the element is expected to exist before returning.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants