How ahmedamine12/extention_easyai_chat works
Project Type and Purpose
• This is a browser extension (Chrome/Chromium-based) named “EasyAI Chat.”
• Purpose: inject an AI-powered floating chat interface into any webpage, manage API keys locally, and route user queries to OpenAI or Google Gemini directly from the client.
File-Structure Analysis
• manifest.json: extension’s entry point (permissions, scripts, UI).
• background.js: event-driven service worker handling long-lived tasks, message routing, API calls.
• content.js & homepage.html: injection of the floating chat bubble and main in-page UI.
• popup/ (popup.html, popup.js, popup.css): the click-to-open toolbar popup.
• settings/ (settings.html, settings.js, settings.css): configuration UI for API keys, preferences.
• providers/ (openai.js, gemini.js): modules encapsulating remote API endpoints and request logic.
• utils/ (storage.js, crypto.js, error.js): local storage abstraction, encryption routines, error handling.
Insights from README
• No backend servers—everything runs in the browser.
• Privacy-first: keys encrypted locally; local storage only.
• Communication pattern: UI ↔ background.js ↔ external AI APIs.
• Browser Host (represents user’s Chrome/Edge/Vivaldi)
• Content Script Module (content.js + homepage.html)
• Popup UI Module (popup/)
• Settings UI Module (settings/)
• Background Service Worker (background.js)
• Storage & Crypto Utils (utils/storage.js & utils/crypto.js)
• Provider Modules (providers/openai.js & providers/gemini.js)
• Local Storage (chrome.storage + IndexedDB if used)
• External AI Services (OpenAI, Google Gemini)
1. User clicks bubble or context-menu → content.js captures input → sends “QUERY” message to background.js
2. Popup UI → sends “QUERY” or “FETCH_HISTORY” to background.js
3. Settings UI → reads/writes encrypted keys via background.js → utils/storage.js → chrome.storage
4. background.js receives “QUERY” → routes to appropriate provider module → provider builds HTTP request → external AI service → returns JSON → background.js processes errors (utils/error.js) → stores history (utils/storage.js) → responds to sender UI
5. background.js dispatches result → content.js or popup.js → render in homepage.html or popup.html
c. Architectural Patterns & Principles
• Event-driven messaging (chrome.runtime.sendMessage/onMessage)
• Separation of concerns: UI layers (content/popup/settings) vs service logic (background) vs data modules (providers/utils)
• Privacy by design: encryption of keys, zero remote logging
d. Key Technologies & Frameworks
• Chrome Extension Manifest v3 APIs (runtime, storage, contextMenus)
• vanilla JavaScript + CSS for UIs
• Fetch API for external HTTP requests
• Web Crypto API (utils/crypto.js)