How nthan2005/promptoptimizer works
Project Type and Purpose
• Type: Browser extension (Chrome/Edge) built on Manifest V3.
• Purpose: Intercept or accept user-entered prompts, send them to ChatGPT for optimization, return optimized prompts to the user—all while respecting privacy and personalization.
Main Architectural Components
a. Manifest V3 Configuration
• manifest.json (not shown in file tree but implicit to any MV3 extension).
b. UI Layer
• Optional Options Page (for saving user preferences).
c. Content Script
• Injected into web pages to detect prompt fields or add “Optimize” buttons inline.
d. Background Service Worker
• Central event hub in MV3: listens for UI events, content-script messages, alarm/timers.
• Manages API calls to the ChatGPT endpoint.
• Handles chrome.storage (user tokens, preferences, history).
e. External Service
• OpenAI ChatGPT API (HTTPS).
f. Storage
• chrome.storage.local or chrome.storage.sync for personalization data, API key, and usage logs.
Interactions and Data Flow
• Service worker receives prompt, retrieves stored API key/credentials, issues HTTPS request to ChatGPT optimization endpoint.
• ChatGPT responds with an optimized prompt; service worker stores the result (optionally) and forwards it to the popup.
• Popup displays optimized prompt; user can copy or replace the original.
• If a content script is used, the popup or background can send a message to the content script to inject results directly into the page’s input fields.
Architectural Patterns and Principles
• Event-Driven Architecture: Chrome extension APIs use message passing (publish/subscribe).
• Separation of Concerns: UI logic (popup), background orchestration (service worker), and DOM injection (content script) are in separate modules.
• Privacy-First: No data saved on external servers; all data flows directly between client and ChatGPT API; local storage is encrypted/per-user.
Technologies and Frameworks
• Chrome/Edge MV3 Extension APIs (manifest.json, chrome.runtime, chrome.storage, chrome.action).
• JavaScript or TypeScript for popup, background, content scripts.
• Fetch or Axios for HTTP calls to the OpenAI API.
• Optional bundler (Webpack/Rollup) if using ES modules or TS.