Inter-script Communication
Relevant source files
The following files were used as context for generating this wiki page:
Inter-script communication is a fundamental mechanism in the Read Frog browser extension, enabling its various components—such as the background service worker, content scripts, popup, and options page—to interact and coordinate actions. As these components run in isolated environments, a robust messaging system is crucial for the extension's functionality.
The system is built upon the @webext-core/messaging library, providing a standardized and type-safe way to send and receive messages. This ensures that different parts of the extension, like the main content script (host.content) and the side panel (side.content), can reliably trigger events and share data.
Architecture Overview
The extension's architecture is composed of several distinct entry points that need to communicate. The background script acts as a central hub for message routing and coordination.
Sources: CLAUDE.md:83-96
The following diagram illustrates the primary communication paths between the extension's main components.
This diagram shows the flow of messages between different scripts. For example, the host.content script communicates with the background worker to manage page translations, and UI components can send messages to the background worker to perform actions like opening the options page.
Sources: src/entrypoints/host.content/index.tsx:112-115, src/entrypoints/side.content/components/side-content/metadata.tsx:32-34, src/components/api-config-warning.tsx:21-24, CLAUDE.md:83-96
Core Messaging System
The core of the communication system is defined in src/utils/message.ts. It exports two main functions, sendMessage and onMessage, which are wrappers around the underlying messaging library.
Sources: src/utils/message.ts
ProtocolMap
A key element is the ProtocolMap interface. It serves as a central registry, defining all possible message types (keys) and their corresponding data payload and return types. This ensures type safety across the application.
| Message Name | Payload Type | Return Type | Description |
|---|---|---|---|
openOptionsPage | undefined | void | Sent from a UI component to request the background script to open the extension's options page. |
checkAndAskAutoPageTranslation | { url: string, detectedCodeOrUnd: string } | void | Sent by host.content on page load or URL change to the background script to check if auto-translation should run. |
askManagerToTogglePageTranslation | { enabled: boolean } | void | Sent from the background script to host.content to enable or disable the page translation manager. |
readArticle | undefined | void | An internal message within the side.content script to initiate the article reading process. |
Sources: src/utils/message.ts:5-13, src/entrypoints/host.content/index.tsx:112-115, 120-124, src/components/api-config-warning.tsx:21-24, src/entrypoints/side.content/components/side-content/metadata.tsx:32-34
sendMessage and onMessage
sendMessage<T extends keyof ProtocolMap>(type: T, data: ProtocolMap[T]['data']): Promise<ProtocolMap[T]['response']>: A function to send a message of a specific type with a corresponding payload. It returns a promise that resolves with the response.onMessage<T extends keyof ProtocolMap>(type: T, callback: (message: Message<ProtocolMap, T>) => void | Promise<any>): A function to register a listener for a specific message type. The provided callback is executed when a message of that type is received.
Sources: src/utils/message.ts:15-23
Communication Flows
The messaging system facilitates several key workflows within the extension.
Auto Page Translation on Navigation
This flow is initiated when the user navigates to a new page or the URL changes within a single-page application.
- The
host.contentscript detects a URL change. - It sends a
checkAndAskAutoPageTranslationmessage to the background script, including the new URL and the detected language. - The background script (logic implied by architecture) checks the user's configuration to see if the site or language is set for automatic translation.
- If auto-translation is required, the background script sends an
askManagerToTogglePageTranslationmessage back to thehost.contentscript withenabled: true. - The
host.contentscript's listener receives the message and activates thePageTranslationManager.
The sequence diagram below visualizes this interaction.
Initiating Article Reading
The "Read Article" feature is triggered from the side panel UI.
- A message listener for
readArticleis set up in theMetadatacomponent within theside.contentscript. - When this message is received (e.g., from another part of the UI or a hotkey), the listener's callback is executed.
- The callback invokes the
readArticlefunction from theuseReadArticlehook, which begins the process of content extraction, analysis, and explanation.
Opening the Options Page
A simple but common pattern is for a UI component to request an action from the background script. The APIConfigWarning component demonstrates this by allowing the user to open the options page directly from a warning message.
// src/components/api-config-warning.tsx:21-24
<button
type="button"
className="cursor-pointer underline"
onClick={() => sendMessage('openOptionsPage', undefined)}
>
When the button is clicked, it sends an openOptionsPage message. The background script listens for this message and performs the action of opening the options page in a new tab.
Sources: src/components/api-config-warning.tsx:21-24
External API Communication (oRPC)
In addition to the internal browser messaging system, the project uses oRPC (Open-RPC) for communication with an external backend service. This is handled by a separate client defined in src/utils/orpc/client.ts.
This client is configured to communicate with the production backend at https://readfrog.app/api/orpc and uses a typed API contract from @read-frog/api-contract. This mechanism is distinct from the inter-script communication and is used for features that require server-side processing, not for coordination between the extension's local components.