refactor: adjust code injection pattern #13
Merged
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.
Fix Content Script Communication Error
BLDL-31
Summary
Refactored the background script's message handling architecture to eliminate the "Could not establish connection. Receiving end does not exist" error and improve code clarity.
Problem
The previous implementation had several architectural issues:
Used error handling for normal flow: Every first click on a page would throw and catch an error, treating the expected case (content script not loaded) as an exceptional case.
Insufficient retry logic: After injecting the content script, the code waited only 150ms before attempting to send a message. This was not enough time for the dynamic
import()incontent.jsto complete and set up the message listener, causing a "Receiving end does not exist" error.Misleading error logging: The code logged errors for normal operation (content script injection), making it difficult to distinguish real failures from expected behavior.
No happy path: There was no clear success path - the code always relied on try/catch blocks even for normal operation.
Solution
Refactored the message handling to use explicit checks and retry logic:
Explicit content script detection: Added
isContentScriptLoaded()function that returns a boolean instead of throwing an error.Clear conditional logic: Replaced try/catch-based flow control with an explicit if/else that handles both cases as normal operations:
Exponential backoff retry: Implemented
sendMessageWithRetry()that waits progressively longer (100ms, 200ms, 300ms, etc.) until the content script's message listener is ready, with a maximum of 10 attempts.Better error reporting: Errors are now only logged when all retry attempts are exhausted, indicating a genuine failure.
Changes
background.js
isContentScriptLoaded(tabId): Checks if content script is loaded by attempting to send a ping messagesendMessageWithRetry(tabId, message, maxRetries): Sends message with exponential backoff retrieshandleIconClick(): Uses explicit conditional logic instead of try/catch for flow controlValidation Instructions
Setup
npm run build(or your build command)chrome://extensions/, enable "Developer mode", click "Load unpacked"about:debugging#/runtime/this-firefox, click "Load Temporary Add-on"Test Cases
Test 1: First Click on a Page (Content Script Not Loaded)
https://example.com)Test 2: Second Click on Same Page (Content Script Already Loaded)
Test 3: Toggle Multiple Times
Test 4: Multiple Tabs
Test 5: Background Page Console Check
chrome://extensions/(orabout:debuggingin Firefox)What NOT to See
What TO See