Skip to content

Fix page flashes in dark mode#802

Merged
boomzero merged 7 commits intodevfrom
boomzero/dark-mode-improvements
Jun 2, 2025
Merged

Fix page flashes in dark mode#802
boomzero merged 7 commits intodevfrom
boomzero/dark-mode-improvements

Conversation

@boomzero
Copy link
Member

@boomzero boomzero commented Jun 1, 2025

Emm 等一会写这个

What does this PR aim to accomplish?:

Fix page flashes in dark mode

How does this PR accomplish the above?:

Somehow


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 and I have tested my changes.
  3. I am willing to help maintain this change if there are issues with it later.
  4. It is compatible with the GNU General Public License v3.0
  5. I have squashed any insignificant commits. (git rebase)
  6. I have checked that another pull request for this purpose does not exist.
  7. I have considered and confirmed that this submission will be valuable to others.
  8. I accept that this submission may not be used, and the pull request can be closed at the will of the maintainer.
  9. I give this submission freely and claim no ownership to its content.

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

boomzero added 2 commits June 1, 2025 19:29
- Applies dark theme immediately at `document-start` using `data-bs-theme`
  and critical preloader CSS to prevent flash of unstyled/light content.
- Leverages localStorage via `UtilityEnabled` for reliable early theme detection.
- Enhances user experience with smoother theme transition on page load.
Added a `color-scheme` meta tag to improve browser UI hints for dark/light modes and updated the dark mode preloader to temporarily hide the body, preventing visual flashes during Bootstrap transitions. Also streamlined style cleanup on DOMContentLoaded for better consistency.
@hendragon-bot hendragon-bot bot added the user-script This issue or pull request is related to the main user script label Jun 1, 2025
@boomzero boomzero linked an issue Jun 1, 2025 that may be closed by this pull request
2 tasks
@boomzero boomzero changed the title Add color-scheme meta tag and refine dark mode preloader logic Fix page flashes in dark mode Jun 1, 2025
@github-actions github-actions bot force-pushed the boomzero/dark-mode-improvements branch from 8fff998 to 3cad8b4 Compare June 1, 2025 12:44
@boomzero
Copy link
Member Author

boomzero commented Jun 1, 2025

@langningchen this

@boomzero boomzero requested review from Copilot and langningchen June 1, 2025 14:14
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes page flashes in dark mode by applying a minimal dark-theme preload at document-start, then removing it once the page loads.

  • Bumps version to 1.6.1 and registers the release in Update.json.
  • Adds @run-at document-start, UtilityEnabled, and applyInitialTheme in XMOJ.user.js to inject and later remove dark-mode preload styles.
  • Adjusts error logging in RequestAPI from console.error to console.log.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
package.json Updated version to 1.6.1
XMOJ.user.js Added @run-at document-start; implemented UtilityEnabled and applyInitialTheme; removed stale utility; adjusted logging and style removal on load.
Update.json Added entry for version 1.6.1 with update metadata
Comments suppressed due to low confidence (3)

XMOJ.user.js:42

  • [nitpick] The function name UtilityEnabled is ambiguous and doesn’t follow common JS boolean naming conventions. Consider renaming it to isUtilityEnabled or hasUtilityEnabled to clarify that it returns a boolean.
let UtilityEnabled = (Name) => {

XMOJ.user.js:54

  • There’s no automated coverage for applyInitialTheme’s logic (injection/removal of preload styles). Consider adding unit tests or integration tests to verify both dark and light paths.
const applyInitialTheme = () => {

XMOJ.user.js:523

  • [nitpick] Swapping console.error for console.log downplays errors. It may be clearer to use console.warn (or leave console.error) so that parsing failures remain visible as errors.
console.log(Response.responseText);

Comment on lines +42 to +48
let UtilityEnabled = (Name) => {
try {
Input = new Date(Input);
let Now = new Date().getTime();
let Delta = Now - Input.getTime();
let RelativeName = "";
if (Delta < 0) {
RelativeName = "未来";
} else if (Delta <= 1000 * 60) {
RelativeName = "刚刚";
} else if (Delta <= 1000 * 60 * 60) {
RelativeName = Math.floor((Now - Input) / 1000 / 60) + "分钟前";
} else if (Delta <= 1000 * 60 * 60 * 24) {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60) + "小时前";
} else if (Delta <= 1000 * 60 * 60 * 24 * 31) {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60 / 24) + "天前";
} else if (Delta <= 1000 * 60 * 60 * 24 * 365) {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60 / 24 / 31) + "个月前";
} else {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60 / 24 / 365) + "年前";
if (localStorage.getItem("UserScript-Setting-" + Name) == null) {
const defaultOffItems = ["DebugMode", "SuperDebug", "ReplaceXM"];
localStorage.setItem("UserScript-Setting-" + Name, defaultOffItems.includes(Name) ? "false" : "true");
}
return "<span title=\"" + Input.toLocaleString() + "\">" + RelativeName + "</span>";
return localStorage.getItem("UserScript-Setting-" + Name) == "true";
Copy link

Copilot AI Jun 1, 2025

Choose a reason for hiding this comment

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

The string "UserScript-Setting-" is repeated in multiple places. Extract it into a constant (e.g. STORAGE_KEY_PREFIX) to avoid duplication and typos.

Suggested change
let UtilityEnabled = (Name) => {
try {
Input = new Date(Input);
let Now = new Date().getTime();
let Delta = Now - Input.getTime();
let RelativeName = "";
if (Delta < 0) {
RelativeName = "未来";
} else if (Delta <= 1000 * 60) {
RelativeName = "刚刚";
} else if (Delta <= 1000 * 60 * 60) {
RelativeName = Math.floor((Now - Input) / 1000 / 60) + "分钟前";
} else if (Delta <= 1000 * 60 * 60 * 24) {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60) + "小时前";
} else if (Delta <= 1000 * 60 * 60 * 24 * 31) {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60 / 24) + "天前";
} else if (Delta <= 1000 * 60 * 60 * 24 * 365) {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60 / 24 / 31) + "个月前";
} else {
RelativeName = Math.floor((Now - Input) / 1000 / 60 / 60 / 24 / 365) + "年前";
if (localStorage.getItem("UserScript-Setting-" + Name) == null) {
const defaultOffItems = ["DebugMode", "SuperDebug", "ReplaceXM"];
localStorage.setItem("UserScript-Setting-" + Name, defaultOffItems.includes(Name) ? "false" : "true");
}
return "<span title=\"" + Input.toLocaleString() + "\">" + RelativeName + "</span>";
return localStorage.getItem("UserScript-Setting-" + Name) == "true";
const STORAGE_KEY_PREFIX = "UserScript-Setting-";
let UtilityEnabled = (Name) => {
try {
if (localStorage.getItem(STORAGE_KEY_PREFIX + Name) == null) {
const defaultOffItems = ["DebugMode", "SuperDebug", "ReplaceXM"];
localStorage.setItem(STORAGE_KEY_PREFIX + Name, defaultOffItems.includes(Name) ? "false" : "true");
}
return localStorage.getItem(STORAGE_KEY_PREFIX + Name) == "true";

Copilot uses AI. Check for mistakes.
} catch (e) {
console.error(e);

const applyInitialTheme = () => {
Copy link

Copilot AI Jun 1, 2025

Choose a reason for hiding this comment

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

add a brief JSDoc or inline comment describing applyInitialTheme’s behavior, side effects, and when it should run, to improve readability and maintainability.

Copilot uses AI. Check for mistakes.

const applyInitialTheme = () => {
const isDarkMode = UtilityEnabled("DarkMode");
console.log("Initial DarkMode check (document-start):", isDarkMode);
Copy link

Copilot AI Jun 1, 2025

Choose a reason for hiding this comment

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

[nitpick] This log runs on every page load and could clutter the console in production. Consider gate-ing it behind a debug flag or removing it once verified.

Suggested change
console.log("Initial DarkMode check (document-start):", isDarkMode);
if (UtilityEnabled("DebugMode")) {
console.log("Initial DarkMode check (document-start):", isDarkMode);
}

Copilot uses AI. Check for mistakes.
Copy link
Member

@langningchen langningchen left a comment

Choose a reason for hiding this comment

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

还是会有闪烁问题,我似乎并没有发现变好 😮‍💨

@boomzero
Copy link
Member Author

boomzero commented Jun 1, 2025

hhh this is hard

@boomzero
Copy link
Member Author

boomzero commented Jun 1, 2025

The only thing we can control is when we remove these temporary styles

@langningchen
Copy link
Member

langningchen commented Jun 2, 2025 via email

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

是的…

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

How about first forcing dark mode in bootstrap 3?

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

The browser has to remember this

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

So when we open other pages, even the initial render would be dark

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

Turns out bootstrap 3 doesn't even support dark mode...

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

先 merge 了…

@boomzero boomzero merged commit 4139bbd into dev Jun 2, 2025
2 of 3 checks passed
@boomzero boomzero deleted the boomzero/dark-mode-improvements branch June 2, 2025 00:59
@langningchen
Copy link
Member

……

@boomzero
Copy link
Member Author

boomzero commented Jun 2, 2025

……

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

Labels

size/XXL 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.

[Bug] if dark mode is enabled, the page flashes when loading

3 participants