Content Script UIs
Relevant source files
The following files were used as context for generating this wiki page:Content Script UIs are the primary method through which the Read Frog extension interacts with users directly on web pages. These UIs are injected into the page's DOM, typically within a Shadow DOM to ensure style isolation and prevent conflicts with the host page's CSS and JavaScript. The extension utilizes the WXT framework's entrypoint pattern to manage these scripts.
There are several distinct content script UIs, each with a specific responsibility, such as the main article reading panel, a popup for translating selected text, and an onboarding guide. These are implemented as React applications, leveraging a shared set of components, hooks, and state management utilities.
Sources: CLAUDE.md, src/entrypoints/host.content/index.tsx:21, src/entrypoints/selection.content/index.tsx:19
Architecture Overview
The extension defines multiple content script entrypoints, each corresponding to a specific feature that appears on a webpage. The general initialization process for these UIs is consistent across the different entrypoints.
Sources: CLAUDE.md
Initialization Flow
Each UI-based content script follows a similar pattern for setup and mounting, managed by the WXT framework. A shadow root is created to host the React application, ensuring it is sandboxed from the host page.
The diagram below illustrates the typical initialization flow for a content script UI.
This flow ensures that a dedicated, isolated container is prepared before the React application is rendered and attached to the page.
Sources: src/entrypoints/host.content/index.tsx:21-42, src/entrypoints/selection.content/index.tsx:19-41
Content Script Entrypoints
The project is structured with several content script entrypoints, each serving a distinct purpose.
| Entrypoint | Path | Purpose |
|---|---|---|
host.content | src/entrypoints/host.content/ | Main script for orchestrating page/node translation, handling shortcuts, and managing URL changes. |
selection.content | src/entrypoints/selection.content/ | Renders the UI for on-demand translation of user-selected text. |
side.content | src/entrypoints/side.content/ | Manages the main side panel UI for in-depth article reading and analysis. |
guide.content | src/entrypoints/guide.content/ | Injected into the official website to facilitate onboarding and communication between the site and the extension. |
Sources: CLAUDE.md, src/entrypoints/host.content/index.tsx, src/entrypoints/selection.content/index.tsx, src/entrypoints/side.content/components/side-content/index.tsx, src/entrypoints/guide.content/index.ts
Side Content UI (side.content)
This is the most feature-rich UI, providing the core "Read Article" functionality. It appears as a collapsible side panel on the right side of the page.
Sources: src/entrypoints/side.content/components/side-content/index.tsx
Core Components
The side panel is composed of several key React components:
SideContent: The root component that manages the panel's open/closed state, width, and resizing logic. When open, it injects a<style>tag into the document head to shrink the<html>element's width, preventing the panel from overlapping page content.TopBar: Provides user controls for source/target language, language level, AI provider selection, and an option to export the analysis.Metadata: Displays the current page's title and favicon, along with the main "Read" / "Regenerate" button to initiate the analysis.Content: A stateful component that displays the current status of the analysis process (e.g., "Extracting content...", "Analyzing...", "Generating...") and renders the final explanation.Explanation: Renders the structured, detailed analysis of the article, broken down by sentence, with key word definitions and explanations.
Sources: src/entrypoints/side.content/components/side-content/index.tsx:18-85, src/entrypoints/side.content/components/side-content/top-bar.tsx, src/entrypoints/side.content/components/side-content/metadata.tsx, src/entrypoints/side.content/components/side-content/content.tsx, src/entrypoints/side.content/components/side-content/explanation.tsx
Article Reading Flow
The article reading process is a multi-step, asynchronous operation managed by a state machine and several hooks.
Reading State Machine
The readStateAtom (a Jotai atom) tracks the current stage of the analysis. The UI in the Content component updates to reflect the current state.
| State | Description |
|---|---|
extracting | The useReadArticle hook is fetching the main content from the page. |
analyzing | The useAnalyzeContent hook is sending the extracted content to an AI model to get a summary and determine if it's an article. |
continue? | The analysis determined the content may not be an article. The UI prompts the user for confirmation to proceed. |
explaining | The useExplainArticle hook is sending the content in batches to an AI model for detailed sentence-by-sentence explanation. |
undefined | The process is complete, and the final Explanation component is rendered. Also the initial and error state. |
Sources: src/entrypoints/side.content/components/side-content/content.tsx:16-41, src/hooks/read/read.tsx:173-195
The following diagram shows the state transitions during the article reading process.
Data Flow Sequence
The sequence diagram below details the interactions between components and hooks when the user clicks the "Read" button.
Host Content Script (host.content)
This script acts as the central controller for features that interact directly with the page content, such as on-demand translation and shortcuts. It does not render a persistent UI itself but sets up the selection.content UI and manages various event listeners and triggers.
Sources: src/entrypoints/host.content/index.tsx
Key Responsibilities
- UI Mounting: It initializes and mounts the
selection.contentUI, which is used for translating selected text. - Translation Triggers: It registers triggers for both node-level translation (
registerNodeTranslationTriggers) and full-page translation (PageTranslationManager). - Shortcut Binding: It binds keyboard shortcuts for translation actions using
bindTranslationShortcutKey. It also re-binds them if the configuration changes. - URL Monitoring: It listens for URL changes on the page (via a custom
extension:URLChangeevent) to reset translation states and check if auto-translation rules apply to the new page. - Background Communication: It communicates with the background script to request actions (e.g.,
checkAndAskAutoPageTranslation) and listens for commands (e.g.,askManagerToTogglePageTranslation).
Sources: src/entrypoints/host.content/index.tsx:44-99
URL Change Handling
The host.content script has a robust mechanism for handling navigation within single-page applications (SPAs).
This flow ensures that the extension's state is correctly synchronized with the content of the current page, even without a full page reload.
Sources: src/entrypoints/host.content/index.tsx:59-75
Selection Content UI (selection.content)
This UI is a small, transient popup that appears when a user selects text on a page. Its entrypoint is responsible for setting up the Shadow DOM and rendering the main <App /> component, which contains the logic for the translation popup. The setup is wrapped in QueryClientProvider and ThemeProvider to provide necessary context.
Sources: src/entrypoints/selection.content/index.tsx:19-38
Guide Content Script (guide.content)
This specialized content script runs exclusively on the Read Frog official website (OFFICIAL_SITE_URL_PATTERNS). It serves as a communication bridge between the website's JavaScript context and the extension's background scripts. It uses window.postMessage and window.addEventListener('message', ...) to securely exchange information, allowing the website to query the extension's state (like pin state or target language) and request changes, which is crucial for the interactive onboarding guide.