ArthaPage employs a message-passing architecture to coordinate between isolated browser extension contexts.
The architecture consists of three primary isolated environments:
-
Popup (User Interface)
- Context: Separate browser process.
- Constraint: Ephemeral; terminated when focus is lost.
- Function: Manages user preferences and triggers content script injection.
-
Content Script (Page Integration)
- Context: Runs within the target webpage.
- Constraint: Access to DOM, but constrained by CSP and CORS policies.
- Function: Injects the Sidebar UI, scrapes content, and relays messages.
- Isolation: Encapsulated within a Shadow DOM to prevent CSS leakage.
-
Background Worker (Service Layer)
- Context: Independent background process.
- Constraint: No access to DOM; purely event-driven.
- Function: Orchestrates API calls (OpenAI, Ollama), manages persistent storage, and handles cross-origin requests.
The extension uses the Chrome Messaging API (chrome.runtime.sendMessage and chrome.tabs.sendMessage) for asynchronous communication.
sequenceDiagram
participant User
participant Popup
participant ContentScript
participant DOM
User->>Popup: Clicks "Open Sidebar"
Popup->>ContentScript: Message: INJECT_SIDEBAR
ContentScript->>DOM: Mounth React Sidebar (Shadow Root)
ContentScript-->>Popup: Response: SIDEBAR_INJECTED
Content Scripts cannot always make direct API calls due to CORS restrictions. Requests are routed through the background worker.
sequenceDiagram
participant Sidebar (React)
participant ContentScript
participant Background
participant LLM_API
Sidebar->>ContentScript: User Prompt
ContentScript->>Background: Message: chat_message
Background->>LLM_API: POST /chat/completions
LLM_API-->>Background: Response (Stream/Text)
Background-->>ContentScript: Response Payload
ContentScript-->>Sidebar: Update Chat UI
To ensure visual consistency across any website:
- Shadow DOM: The entire React application is mounted inside a Shadow Root (
mode: 'open'). - Tailwind CSS: Styles are injected directly into the Shadow Root via constructed stylesheets, bypassing the host page's global CSS cascade.