chat-relay/docs/content-script.md
BinaryBeastMaster 25abcd2974 Initial commit
2025-05-10 15:09:23 -07:00

2.7 KiB

Content Script Architecture (content.js)

This document describes the structure and flow of content.js, which acts as the in-page automation engine for injecting chat messages and monitoring responses in various AI web interfaces.


🧩 Overview

The content script:

  1. Detects which AI platform is active (e.g., ChatGPT, AI Studio).
  2. Loads the appropriate provider.
  3. Sends chat messages programmatically.
  4. Monitors the DOM for typed/streamed responses.
  5. Reports back results to the background script.

⚙️ Initialization

Boot Sequence

sequenceDiagram
    participant ContentScript
    participant Provider
    participant Background

    ContentScript->>Provider: detectProvider()
    ContentScript->>Background: CHAT_RELAY_READY
    ContentScript->>DOM: monitor inputs/buttons
    ContentScript->>DOM: start polling

📌 DOM Utilities

Searches for selectors like:

provider.inputSelector
provider.sendButtonSelector
provider.responseSelector

💬 Message Injection

Method: sendChatMessage(text)


📥 Response Capture

A. DOM Monitoring

B. Completion Detection

Platform-specific heuristics like:


🔁 Provider Coordination

  • Uses shared provider object initialized at runtime
  • Each provider has methods like:
    • sendChatMessage()
    • getStreamingApiPatterns()
    • shouldSkipResponseMonitoring()

🛡️ Fault Recovery

  • Logs missing selectors or failures
  • Retries up to 5x for message dispatch
  • Reports failure back to background

Summary

content.js dynamically adapts to different AI platforms to send and monitor chat activity. It forms the frontend relay bridge between user intent (via server) and provider-specific DOM interfaces.