How bcastelino/prompt-polish-local-extension works
Project type and purpose
• This is a browser-extension project (Chrome Extension, Manifest V3) whose sole purpose is to enhance user-supplied AI prompts locally via the OpenRouter API and manage a small prompt library in browser local storage.
• There is no external backend you control—only the OpenRouter API as a third-party service.
File structure
Top-level files and folders:
• manifest.json – extension metadata, permissions, entry points
• background.js – background service worker, context‐menu handler, message router, API invocations
• content.js/css – injected content script for “right-click → enhance” within arbitrary pages
• popup.html/js/css – the UI for manual prompt entry, enhancement, saving, copying
• openrouter_secrets.js – local file holding the API key (gitignored)
Key patterns:
• MV* pattern inside the popup (popup.html is the View, popup.js is the Controller/Model logic).
• Message passing via chrome.runtime.sendMessage / onMessage between popup ↔ background ↔ content.
• Local state persistence via chrome.storage.local.
• External HTTP calls to OpenRouter from the background script only (to keep the secret key out of content/popup).
README for additional insights
• All HTTP calls go directly from the extension (background.js) to OpenRouter’s chat/completions endpoint.
• Data flow is strictly: User → popup.js or content.js → background.js → OpenRouter API → background.js → popup.js or content.js → UI → chrome.storage.local.
a. Main components
1. Chrome Browser (host)
2. Extension:
• Popup UI (popup.html/js/css)
• Background Service Worker (background.js)
• Content Script (content.js/css)
• Local Storage (chrome.storage.local)
3. External Service: OpenRouter LLM API
b. Relationships & interactions
2. Popup UI sends “enhance” message to Background
3. Background performs fetch() to OpenRouter using API key
4. Background returns enhanced prompt to Popup UI
5. Popup UI reads/writes saved prompts via chrome.storage.local
6. User right-clicks on any text input → Context menu provided by Background → Content Script runs → forwards request to Background → Background calls API → Content Script displays result inline
c. Architectural patterns & design principles
• Single-page-style UI in popup; separation of concerns between UI (popup.js) and data flow/side effects (background.js).
• Messaging broker pattern via chrome.runtime.
• Local state store using chrome.storage.local (No external database).
• Secure secret handling: Only the background script holds the API key.
• Stateless enhancement: each API call is independent.
d. Key technologies & frameworks
• Chrome Extension APIs (manifest V3, service workers, storage, contextMenus)
• Vanilla JavaScript, HTML, CSS
• OpenRouter LLM REST API
• Optional: postman/cURL for local testing
Final advice
• Two user entry points: popup click flow and context-menu flow.
• Every message channel explicitly—this is the crux of extension architecture.