Agentic Browser

AI agent for web automation and scraping, designed to plan, execute, and verify actions across any website

Browser extension architecture

Introduction#

WXT + React extension architecture: side panel, background, content scripts, messaging, WebSocket to backend, auth, packaging.

Project structure#

Entrypoints cover background, content, and side panel, plus shared utilities and hooks. WXT manages build, manifest generation, and browser-specific targets.

Core components#

  • Side Panel (React): Hosted inside a shadow root UI, renders the main app, manages authentication, WebSocket connectivity, tab management, and the agent executor.
  • Background Script: Handles cross-tab messaging, executes agent tools, performs browser-level actions, and manages Gemini requests.
  • Content Script: Injects UI overlays and performs page-level actions when instructed by the background script.
  • Messaging System: Uses browser runtime messaging for background ↔ side panel and background ↔ content script communication.
  • WebSocket Client: Provides a minimal client for real-time agent execution updates and statistics retrieval.
  • Utilities: Command parsing, agent execution, and browser action execution.

Architecture overview#

Extension pieces:

  • UI Layer: Side panel React app with hooks for auth, WebSocket, and tab management.
  • Control Layer: Side panel orchestrates agent execution and displays progress.
  • Communication Layer: WebSocket client for real-time updates; browser messaging for background ↔ side panel and background ↔ content script.
  • Execution Layer: Background script handles browser APIs and dispatches actions; content script executes DOM-level actions.

Detailed component analysis#

Side panel React application#

The side panel is a React app mounted inside a shadow root UI. It initializes the app, sets up authentication, WebSocket connectivity, and tab management. It renders the agent executor and unified settings menu.

Authentication flow#

The authentication hook integrates with browser identity and a backend service to exchange OAuth codes for tokens, persist user data, and manage token refresh. It supports both Google OAuth and a demo GitHub flow.

WebSocket integration and real-time updates#

The WebSocket client encapsulates connection management, event emission, and agent execution. The side panel hook subscribes to connection status and progress updates, displaying real-time feedback.

Agent executor pattern and action execution#

The agent executor parses slash commands, executes agents either via WebSocket or HTTP, and triggers browser actions. It maintains chat sessions and displays formatted responses.

Background script functionality and messaging#

The background script listens for messages from the side panel and content script, executes agent tools, manages tabs, and performs browser-level actions. It injects content scripts and coordinates cross-tab communication.

Content script integration#

The content script runs on all URLs and can be extended to perform page-level actions. It listens for messages from the background script and executes DOM operations.

Dependency analysis#

The extension relies on WXT for build and manifest generation, React for UI, and Socket.IO for real-time communication. Dependencies are declared in package.json and TypeScript configuration extends WXT’s generated tsconfig.

Performance considerations#

  • Debounce or throttle frequent UI updates (e.g., tab list refresh) to reduce re-renders.
  • Batch browser API calls (tabs.query, scripting.executeScript) and cache results where appropriate.
  • Use lazy loading for heavy components and defer non-critical computations.
  • Limit DOM extraction sizes (e.g., HTML capture) and apply timeouts to prevent long-running operations.
  • Prefer polling fallbacks for stats retrieval when WebSocket is unavailable.

Troubleshooting guide#

Common issues and resolutions:

  • WebSocket not connecting: Verify VITE_API_URL and server availability; check connection events and fallback to HTTP stats.
  • Authentication failures: Ensure backend is running and identity API is available; confirm OAuth redirect URI and scopes.
  • Content script injection errors: Confirm permissions and that the content script path is correct; verify target tab exists.
  • Tab management inconsistencies: Ensure listeners are registered/unregistered on mount/unmount; use query results before acting.
  • Cross-origin limitations: Use host_permissions and appropriate permissions; avoid unsafe inline styles in injected UI.

Conclusion#

Host permissions are broad by design for automation. OAuth uses browser.identity. Point the client at the API base URL through env, not hardcoded strings.

Appendices#

WXT configuration and permissions#

  • Manifest permissions include activeTab, tabs, storage, scripting, identity, sidePanel, webNavigation, webRequest, cookies, bookmarks, history, clipboard, notifications, contextMenus, downloads.
  • Host permissions grant access to all URLs.
  • Scripts support development and build targets for Chrome and Firefox.

Browser compatibility and packaging#

  • Use WXT scripts to build and package for Chrome and Firefox.
  • Shadow DOM UI ensures isolation and compatibility across pages.
  • Feature detection for browser APIs (e.g., speech recognition) prevents runtime errors.

Cross-Origin communication#

  • Use host_permissions for broad access.
  • For OAuth, rely on browser.identity and secure redirects.
  • For backend communication, configure CORS and environment variables for API base URL.