fix: 修复异常页面的 TypeError,并兼容新版 /web/contest 比赛页面

Merged
BO
2mo ago
+1679-941

What does this PR aim to accomplish?:

Closes #1009
Closes #1029

  1. #1009: pages whose DOM differs from what the script expects (403/404 pages, multi-subtask test point pages, etc.) throw TypeErrors, which DebugMode shows as an "internal error" popup.
  2. #1029: the new client-rendered contest pages under /web/contest/ (a Vue app mounted on #app) break the script's navigation and layout. The site no longer links to the old contest pages.

How does this PR accomplish the above?:

Error fixes (#1009)

  • Every dereferenced querySelector result is checked for null. Page branches that rebuild their own container skip the page when it is missing.
  • Paths that send requests stop instead of sending empty values: profile save, AutoCheat, ImproveACRate, ApplyData and GetUserInfo.
  • The multi-subtask test point page (reinfo.php) goes through every result group and skips elements without a test point's structure. This fixes the "Cannot read properties of undefined" error reported in the issue.
  • The old contest 403/404 pages now redirect to /web, which handles them itself.

New contest pages (#1029)

  • /web gets the same Bootstrap 5 and skin setup as the legacy pages; the site's Bootstrap 3 CSS is removed there too.
    • The app's Bootstrap 3 markup is mapped to Bootstrap 5 classes on every render: navbar, dropdown, buttons, pagination and badges.
    • The legacy navbar behavior is shared rather than copied: the floating top bar (NavbarStyler), the ResetType user menu, Translate and RemoveUseless.
  • InitializeContestWebApp re-applies the script's enhancements after async renders and in-app route changes. Vue's own elements are never replaced.
    • Countdowns.
    • Problem IDs and discussion links.
    • Copy buttons.
    • Open all / unsolved problems.
    • AutoCheat.
    • Read-only code editors.
    • Rank header translation.
    • The left-edge problem switcher, built from the contest API response.
  • Contest problem IDs and I/O file names come from the new /api/contest/... JSON API. The submit page's fallback to the original problem after a contest ends no longer reads legacy contest HTML.

Old contest pages redirect to /web, and their handlers are removed

The site no longer links to these pages. The script redirects them at document-start:

Old pageNew page
contest.php, contest.php?cid=X/web/contest, /web/contest/X
problem.php?cid=X&pid=N/web/contest/X/<letter>
problem_std.php, problem_solution.php…/std, …/solution
contestrank-correct.php?cid=X/web/contest/X/rank-correct

The handlers for those pages are removed (about 450 lines). problem.php?id=…, contestrank-oi.php, submitpage.php and status.php are still in use and unchanged. On ordinary problem pages the problem ID now comes from id. It was previously read from the contest cache, which gave null, so the I/O file name and the discussion count used the wrong problem.

Loading: fewer flashes and layout shifts

  • While loading, pages hide the body instead of making the root transparent. A transparent root showed the browser's white canvas in dark mode.
  • The canvas uses the active skin's background and color-scheme.
  • A page is shown only once its navbar is converted and the top bar applied. There is a safety fallback: 1.5 s after main() starts, 4 s after DOMContentLoaded at the latest.
  • The top bar is applied immediately instead of up to a second later.
  • Measured in dark mode (headless Chromium, every 10 ms during each navigation): pages are first shown fully styled, with no navbar or content movement afterwards, where dev had 2–4 jumps per load.
  • Server-rendered .php pages can still paint before a userscript manager injects the script.

Other fixes

  • Monochrome skin: links in table headers were the same color as the header background (1:1 contrast), for example the problem letters in rank tables. They now reach 13.8:1 in dark mode and 21:1 in light.

Tests

  • npm test runs tests/contest-web.test.cjs (9 tests), and a new Test workflow runs it on pull requests and on pushes to dev and master. The tests cover:
    • Route parsing.
    • The old-page redirect mapping.
    • Letter-to-index problem mapping.
    • API error handling.
    • Theme and canvas selection.
  • tests/userscript-harness.cjs runs the script on the live site in Chromium the way a userscript manager does. It checks each /web page, the redirects, legacy pages and both user menus, and saves screenshots. Credentials come only from environment variables. The script's backend and all submit/modify/login/logout POSTs are blocked. See the header of the file for usage.

Verification

  • The harness passes on the live site (logged in) for the /web contest pages, all six redirects, and the legacy problem and problem-set pages, in both the default and monochrome skins.
  • Not verified in a real Tampermonkey install: it installs under automation, but Chrome did not run any user script there. A maintainer should still try the new and the classic UI by hand before ticking the box below.

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

🤖 Generated with Claude Code

https://claude.ai/code/session_0135Ggqqp6xktQZytLRJQ9nc