Skip to content

Fix loginpage.php Loop#971

Closed
def-WA2025 wants to merge 6 commits into
devfrom
fix-loginpage-loop
Closed

Fix loginpage.php Loop#971
def-WA2025 wants to merge 6 commits into
devfrom
fix-loginpage-loop

Conversation

@def-WA2025
Copy link
Copy Markdown
Member

@def-WA2025 def-WA2025 commented Apr 12, 2026

What does this PR aim to accomplish?

Fix loginpage.php loop.

How does this PR accomplish the above?

Check Please logout First! and go back.


By submitting this pull request, I confirm the following:

  1. I have read and understood the contributor's guide, as well as this entire template. I understand which branch to base my commits and Pull Requests against.
  2. I have commented on my proposed changes within the code.
  3. I have tested my changes.
  4. I am willing to help maintain this change if there are issues with it later.
  5. It is compatible with the GNU General Public License v3.0
  6. I have squashed any insignificant commits. (git rebase)
  7. I have checked that another pull request for this purpose does not exist.
  8. I have considered and confirmed that this submission will be valuable to others.
  9. I accept that this submission may not be used, and the pull request can be closed at the will of the maintainer.
  10. I give this submission freely and claim no ownership to its content.
  11. I have verified that my changes work correctly in both the new UI and the old/classic UI.

  • I have read the above and my PR is ready for review. Check this box to confirm

Summary by Sourcery

Bug Fixes:

  • Stop repeated redirects to loginpage.php by detecting the "Please logout First!" message and returning the user to their last page before initiating auto-login.

Summary by cubic

Prevents an infinite redirect loop on loginpage.php by detecting the “Please logout First!” message and sending users back to UserScript-LastPage instead of re-triggering AutoLogin. Updates release metadata: adds 3.4.4 to Update.json (with corrected /v1 route text) and bumps versions in XMOJ.user.js and package.json to 3.4.4.

Written for commit 2a8b158. Summary will update on new commits.

@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai Bot commented Apr 12, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds defensive checks around auto-login redirects to avoid an infinite loop on loginpage.php when the server responds with a "Please logout First!" message, by restoring the last page instead of re-triggering login navigation.

Sequence diagram for AutoLogin redirect with Please logout First handling

sequenceDiagram
    actor User
    participant Browser
    participant UserScript
    participant XMOJServer

    User->>Browser: Navigate to protected_page
    Browser->>XMOJServer: GET /protected_page
    XMOJServer-->>Browser: 302 to /loginpage.php
    XMOJServer-->>Browser: Body contains 请登录后继续操作 link

    Browser-->>UserScript: DOM ready on protected_page
    UserScript->>Browser: Check AutoLogin enabled
    UserScript->>Browser: Detect 请登录后继续操作 link
    UserScript->>Browser: Save location to localStorage UserScript-LastPage
    UserScript->>Browser: Redirect to /loginpage.php

    Browser->>XMOJServer: GET /loginpage.php
    XMOJServer-->>Browser: 200 loginpage.php with link text Please logout First!

    Browser-->>UserScript: DOM ready on loginpage.php
    UserScript->>Browser: Check AutoLogin enabled and profile missing
    UserScript->>Browser: Read first anchor text
    alt Anchor text is Please logout First!
        UserScript->>Browser: Read UserScript-LastPage
        UserScript->>Browser: Redirect to stored last page
    else Anchor text is not Please logout First!
        UserScript->>Browser: Save loginpage.php as UserScript-LastPage
        UserScript->>Browser: (Potential further AutoLogin handling)
    end
Loading

Flow diagram for updated AutoLogin redirect logic

flowchart TD
    Start["Check if AutoLogin redirect is needed"]
    AutoLoginEnabled{"UtilityEnabled(AutoLogin)?"}
    HasLoginPrompt{"First body link text is 请登录后继续操作?"}
    OnLoginPageProfileMissing{"On page without profile or profile is 登录?"}
    IsLogoutFirst{"First anchor text is Please logout First!?"}
    RestoreLastPage["Redirect to localStorage UserScript-LastPage"]
    SaveLastPage["Save current location to UserScript-LastPage"]
    GoToLoginPage["Redirect to https://www.xmoj.tech/loginpage.php"]
    End["Page navigation complete"]

    Start --> AutoLoginEnabled
    AutoLoginEnabled -- No --> End
    AutoLoginEnabled -- Yes --> HasLoginPrompt
    HasLoginPrompt -- Yes --> IsLogoutFirst
    HasLoginPrompt -- No --> OnLoginPageProfileMissing

    OnLoginPageProfileMissing -- Yes --> IsLogoutFirst
    OnLoginPageProfileMissing -- No --> End

    IsLogoutFirst -- Yes --> RestoreLastPage --> End
    IsLogoutFirst -- No --> SaveLastPage --> GoToLoginPage --> End
Loading

File-Level Changes

Change Details Files
Prevent infinite redirect loops when auto-login encounters a "Please logout First!" state on login-related pages.
  • Before redirecting unauthenticated users to loginpage.php, detect if the first anchor text is "Please logout First!" and, if so, navigate back to the last stored page instead of redirecting again
  • Apply the same "Please logout First!" detection and back-navigation behavior when #profile is missing and the script tries to force navigation to loginpage.php
  • Extend the main() auto-login condition (when #profile is "登录") with the same guard, ensuring it restores the last page rather than looping on loginpage.php
XMOJ.user.js

Possibly linked issues

  • #[Bug] 已登录状态下跳转到 loginpage.php 死循环: PR adds checks for the “Please logout First!” message to stop the loginpage.php redirect loop described in issue.
  • #[Bug] Opening /loginpage.php when already logged in will cause the page to continously flash: The PR adds logic to detect the logout message and restore the last page, fixing the flashing loop.

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

@hendragon-bot hendragon-bot Bot added the user-script This issue or pull request is related to the main user script label Apr 12, 2026
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 12, 2026

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

Latest commit: 2a8b158
Status: ✅  Deploy successful!
Preview URL: https://20ea2cc3.xmoj-script-dev-channel.pages.dev
Branch Preview URL: https://fix-loginpage-loop.xmoj-script-dev-channel.pages.dev

View logs

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 checks for document.querySelector('a').innerText == "Please logout First!" are repeated in three places and assume the anchor always exists; consider storing the result of document.querySelector('a') in a variable, null‑checking it, and extracting a small helper to keep this logic in one place.
  • When redirecting back with location.href = localStorage.getItem("UserScript-LastPage"), it may be safer to guard against a missing or empty value (e.g., fall back to a sensible default) to avoid navigating to null or an invalid URL.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The checks for `document.querySelector('a').innerText == "Please logout First!"` are repeated in three places and assume the anchor always exists; consider storing the result of `document.querySelector('a')` in a variable, null‑checking it, and extracting a small helper to keep this logic in one place.
- When redirecting back with `location.href = localStorage.getItem("UserScript-LastPage")`, it may be safer to guard against a missing or empty value (e.g., fall back to a sensible default) to avoid navigating to `null` or an invalid URL.

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.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-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.

1 issue found across 1 file

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="XMOJ.user.js">

<violation number="1" location="XMOJ.user.js:863">
P1: The new "Please logout First!" redirect is immediately overridden by a following unconditional redirect, so it does not actually break the login loop.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread XMOJ.user.js

//otherwise CurrentUsername might be undefined
if (UtilityEnabled("AutoLogin") && document.querySelector("body > a:nth-child(1)") != null && document.querySelector("body > a:nth-child(1)").innerText == "请登录后继续操作") {
if (document.querySelector('a').innerText == "Please logout First!") location.href = localStorage.getItem("UserScript-LastPage");
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Apr 12, 2026

Choose a reason for hiding this comment

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

P1: The new "Please logout First!" redirect is immediately overridden by a following unconditional redirect, so it does not actually break the login loop.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At XMOJ.user.js, line 863:

<comment>The new "Please logout First!" redirect is immediately overridden by a following unconditional redirect, so it does not actually break the login loop.</comment>

<file context>
@@ -860,13 +860,15 @@ GM_registerMenuCommand("重置数据", () => {
 
 //otherwise CurrentUsername might be undefined
 if (UtilityEnabled("AutoLogin") && document.querySelector("body > a:nth-child(1)") != null && document.querySelector("body > a:nth-child(1)").innerText == "请登录后继续操作") {
+    if (document.querySelector('a').innerText == "Please logout First!") location.href = localStorage.getItem("UserScript-LastPage");
     localStorage.setItem("UserScript-LastPage", location.pathname + location.search);
     location.href = "https://www.xmoj.tech/loginpage.php";
</file context>
Fix with Cubic

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-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.

1 issue found across 3 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="package.json">

<violation number="1" location="package.json:3">
P2: Do not manually bump `package.json` version; this project’s release workflow manages version updates automatically and manual edits can desynchronize version sources.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread package.json Outdated
Signed-off-by: zsTree <wa2025666@gmail.com>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-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.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="package.json">

<violation number="1" location="package.json:3">
P1: Version was updated only in `package.json`, causing version desynchronization with `XMOJ.user.js` and `Update.json`.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Comment thread package.json
{
"name": "xmoj-script",
"version": "3.3.5",
"version": "3.4.4",

This comment was marked as resolved.

@def-WA2025 def-WA2025 linked an issue Apr 12, 2026 that may be closed by this pull request
2 tasks
@def-WA2025 def-WA2025 closed this Apr 12, 2026
@def-WA2025 def-WA2025 deleted the fix-loginpage-loop branch April 12, 2026 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S user-script This issue or pull request is related to the main user script

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant