How hasan-py/ai-assistant-chrome-extension works
Main Components
• Background Scripts: Found in the “background” directory. Files such as index.js, contextMenuManager.js, and menus.js form a persistent or event-based background process managing global events (e.g., right-click context menus).
• Content Scripts: Located in the “content” directory (content/index.js, content/promptHandler.js, content/promptUi.js) which interact with the current web page. They monitor page content and handle prompts triggered by user actions.
• Manifest and Assets: The manifest.json defines the extension’s behavior and registers scripts. The “asset” directory and any other resources provide static content or styling.
Component Relationships and Data Flow
• The manifest.json is the central configuration file that connects all the components by declaring which scripts run in which contexts.
• The background script also sets up context menus using managers defined in background/contextMenuManager.js. When a user right-clicks and selects the specified menu item, the background script triggers the content script.
• The content script (with logic split between promptHandler.js and promptUi.js) then interacts directly with the web page. It processes selected text, shows prompts, or displays results based on actions initiated from either the popup or background.
• The extension may then use the provided API credentials to send requests to external services (like GPT or Gemini) and receive processed text, which is then rendered in the UI.
Architectural Patterns & Key Design Principles
• Separation of Concerns: Each major function (UI, background processing, content manipulation) is separated into different directories and scripts.
• Event-Driven Communication: The components communicate via Chrome’s messaging system (e.g., events from the background or content scripts to the popup).
• Modularity: The clear division into popup, background, content, and static assets reflects a modular design that is easy to extend or modify.
Specific Technologies & Frameworks
• Browser APIs: Integration with Chrome’s extension APIs (e.g., context menus, messaging, extension events).
• JavaScript: All logic is implemented in JavaScript.
• HTML/CSS: Used in the popup for UI rendering.
• Content components interacting with web pages, receiving triggers (or events) from the Background scripts.
• Optionally, include an “External API Service” component, which the content scripts or background might contact using the user-provided API key/endpoint.
• Finally, incorporate the manifest.json as a central node that connects to all components, emphasizing its role in configuration.