Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses a race condition in the offscreen document messaging layer where concurrent
chrome.runtime.sendMessagecalls could receive mixed-up or lost responses. It introduces a serialized message queue with readiness polling and timeout protection for all offscreen communication. Additionally, Cnblogs login detection is migrated from a manual cookie-header approach in the service worker to an offscreen document fetch, which sends cookies automatically via document context.Related Issue
N/A
Type of Change
Changes Made
apps/extension/src/background.js: Removed the_offscreenCreatedboolean flag; replaced withchrome.offscreen.hasDocument()check on every call. Added_waitForOffscreenReady()ping loop andsendOffscreenMessage()serialization queue. RefactoredwarmUpFetchandoffscreenApiFetchto usesendOffscreenMessage. Added retry logic (up to 2 attempts) fordetectCto51ViaOffscreen. Added newdetectCnblogsViaOffscreenfunction.apps/extension/src/offscreen.js: AddedOFFSCREEN_PINGhandler that immediately returns{ pong: true }. AddedOFFSCREEN_DETECT_CNBLOGShandler callinghandleDetectCnblogs(). Addedcredentials: 'include'to thehandleDetectCto51fetch. Added debug info (status,url,htmlLen,title) to the not-logged-in response ofhandleDetectCto51. Implemented newhandleDetectCnblogs()fetchingaccount.cnblogs.com/user/userinfowithcredentials: 'include'.packages/detection/src/platforms/cnblogs.js: RewrotedetectCnblogsUserto delegate toglobalThis.__coseDetectCnblogs()(offscreen) instead of manually collecting cookies and attaching them as aCookieheader in a service worker fetch.packages/detection/src/platforms/cto51.js: Added pass-through of_debuginfo when 51CTO reports not-logged-in, surfacing response metadata for easier diagnosis.Implementation Details
Key Changes:
sendOffscreenMessage(msg, timeoutMs = 15000)— chains each call onto_offscreenQueueso only one offscreen message is in-flight at a time. APromise.raceagainst a 15 s timeout prevents the queue from getting permanently stuck if the offscreen document is garbage-collected or fails to respond. Errors are swallowed from the queue chain so a single failure does not block future messages._waitForOffscreenReady(timeoutMs = 3000)— aftercreateDocumentresolves, polls by sendingOFFSCREEN_PINGevery 50 ms until the offscreen listener replies{ pong: true }or the timeout elapses, ensuring messages are not sent before the listener is registered.credentials: 'include'in document context, making session cookies available automatically — eliminating the brittle manualCookieheader construction that was previously required in the MV3 service worker.Technical Notes:
chrome.runtime.sendMessageis broadcast-based; when multiple offscreen message types are dispatched concurrently, responses can be routed to the wrong caller. The serialization queue is the minimal fix without restructuring the entire messaging architecture._offscreenCreatedflag was unreliable because it was set totrueeven in thecatchblock, preventing recovery after a failedcreateDocument. The new approach re-checkshasDocument()on every call, which is the correct pattern.handleDetectCto51now explicitly passescredentials: 'include'to ensure consistent cookie behavior across Chrome versions.Testing
Testing Checklist
Manual Testing Steps
devbranch buildOFFSCREEN_PING/ponghandshake appears in the console on first offscreen document creation_debuginfo is logged when not logged inScreenshots/Videos
N/A
Reviewer Checklist
Additional Notes
The
sendOffscreenMessagequeue is a global singleton inbackground.js. If a future refactor introduces multiple background script entry points, the queue state will need to be shared accordingly. The 15 s message timeout and 3 s readiness timeout are conservative defaults and can be tuned based on observed performance.