|
| 1 | +# ChatGPTBox - Browser Extension |
| 2 | + |
| 3 | +ChatGPTBox is a cross-platform browser extension that deeply integrates ChatGPT and other AI models into web browsing. The extension provides chat dialogs, selection tools, site-specific adapters, and AI-powered features across the web. |
| 4 | + |
| 5 | +Always reference these instructions first and fall back to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Bootstrap and Build |
| 10 | + |
| 11 | +- Install dependencies: `npm ci` -- npm audit warnings may appear; for development-only dependencies they generally do not affect the shipped extension. Review and address runtime-impacting advisories separately. |
| 12 | +- Development build: `npm run dev` -- runs webpack in watch mode. Do not kill mid-compilation, but stop gracefully when switching branches or after dependency/config changes, then restart to avoid stale watchers and inconsistent state. |
| 13 | +- Production build: `npm run build` -- Avoid force-killing mid-bundle; stop, fix, then rebuild. |
| 14 | + See "Time Expectations" and "Build Issues" for the hung-build policy and recovery steps. |
| 15 | +- Analyze bundle: `npm run analyze` -- Inspects the size of webpack output files. |
| 16 | +- Format code: `npm run pretty` -- uses Prettier to format all JS/JSX/CSS files. Run this before linting. |
| 17 | +- Lint code: `npm run lint` -- uses ESLint. |
| 18 | +- Safari build: `npm run build:safari` (see Platform-Specific Instructions for details) |
| 19 | + |
| 20 | +### Build Output Structure |
| 21 | + |
| 22 | +Production build creates multiple variants in `build/` directory: |
| 23 | + |
| 24 | +- `chromium/` - Chromium-based browsers (Chrome, Edge) with full features |
| 25 | +- `firefox/` - Firefox with manifest v2 |
| 26 | +- `chromium-without-katex-and-tiktoken/` - Minimal build without math rendering and token encoding |
| 27 | +- `firefox-without-katex-and-tiktoken/` - Minimal Firefox build without math rendering and token encoding |
| 28 | +- Distribution artifacts: |
| 29 | + - Chromium: `build/chromium.zip` |
| 30 | + - Firefox: `build/firefox.zip` |
| 31 | + - Safari: `Fission - ChatBox.app` and `safari.dmg` (see Safari Build section for details) |
| 32 | + |
| 33 | +## Architecture Overview |
| 34 | + |
| 35 | +The project uses Preact (for React-like components), SCSS (for styling), and Webpack 5 (for bundling). |
| 36 | + |
| 37 | +### Key Components |
| 38 | + |
| 39 | +- **Content Script** (`src/content-script/index.jsx`) - Injected into all web pages, provides main chat functionality |
| 40 | +- **Background Script** (`src/background/index.mjs`) - Handles browser APIs and cross-page communication |
| 41 | +- **Popup** (`src/popup/`) - Extension popup interface accessible via browser toolbar |
| 42 | +- **Independent Panel** (`src/pages/IndependentPanel/`) - Standalone chat page and side panel |
| 43 | +- **Site Adapters** (`src/content-script/site-adapters/`) - Custom integrations for specific websites (Reddit, GitHub, YouTube, etc.) |
| 44 | +- **Selection Tools** (`src/content-script/selection-tools/`) - Text selection features (translate, summarize, explain, etc.) |
| 45 | + |
| 46 | +### Manifests |
| 47 | + |
| 48 | +- `src/manifest.json` - Manifest v3 for Chromium browsers (Chrome, Edge, Opera, etc.) |
| 49 | +- `src/manifest.v2.json` - Manifest v2 for Firefox (current status; future MV3 migration may change this) |
| 50 | + - Background runs as service worker (MV3) vs background page (MV2) |
| 51 | + - Different permission models between manifest versions |
| 52 | + |
| 53 | +## Testing and Validation |
| 54 | + |
| 55 | +### Manual Browser Extension Testing (CRITICAL) |
| 56 | + |
| 57 | +This browser extension has no automated tests, so manual testing is essential: |
| 58 | + |
| 59 | +1. **Load Extension in Browser:** |
| 60 | + - Chrome: Go to `chrome://extensions/`, enable Developer Mode, click "Load unpacked", then select the folder `build/chromium/` (the folder must contain `manifest.json`). |
| 61 | + - Firefox: Go to `about:debugging#/runtime/this-firefox`, click "Load Temporary Add-on", then select the `manifest.json` file inside `build/firefox/` (do not select the folder directly). Note: Temporary (unsigned) add-ons are removed on browser restart; reload them via the same "This Firefox" page after every restart, and some environments with enterprise policies may block loading from file. |
| 62 | + - **Important**: Extension files cannot be tested by serving them via HTTP server - they must be loaded as a proper browser extension. |
| 63 | + |
| 64 | +2. **Core Functionality Tests:** |
| 65 | + - Press `Ctrl+B` (Windows/Linux) or `⌘+B` (macOS) to open the chat dialog on any webpage |
| 66 | + - Select text on a page, verify selection tools appear |
| 67 | + - Right-click and verify "Ask ChatGPT" context menu appears |
| 68 | + - Click extension icon to open popup |
| 69 | + - Press `Ctrl+Shift+H` (Windows/Linux) or `⌘+Shift+H` (macOS) to open the independent conversation page |
| 70 | + |
| 71 | +3. **Site Integration Tests:** |
| 72 | + - Visit YouTube.com, verify video summary features work |
| 73 | + - Visit Reddit.com, verify ChatGPT integration appears in sidebar |
| 74 | + - Visit github.com, verify code analysis features work |
| 75 | + - Visit Google.com search results, verify ChatGPT responses appear |
| 76 | + |
| 77 | +4. **Configuration Tests:** |
| 78 | + - Open extension popup, navigate through tabs (General, Feature Pages, Modules > Selection Tools, Modules > Sites, Advanced) |
| 79 | + - Test API mode switching (Web API vs OpenAI API) under Modules > API Modes |
| 80 | + - If using Web APIs, ensure you are signed in to the provider in the same browser profile; if using API Keys, configure valid keys in settings |
| 81 | + - Verify language settings work |
| 82 | + |
| 83 | +Debugging tips: |
| 84 | + |
| 85 | +- Inspect background Service Worker, page DevTools for content scripts, and use "Inspect popup" for the popup UI |
| 86 | +- After rebuilds, reload the extension and refresh the page to re‑inject content scripts |
| 87 | + |
| 88 | +### Build Validation |
| 89 | + |
| 90 | +Ensure these files exist in `build/chromium/` after successful build: |
| 91 | + |
| 92 | +- `manifest.json` (contains proper extension metadata) |
| 93 | +- `background.js` (service worker bundle) |
| 94 | +- `content-script.js` (main functionality) |
| 95 | +- `content-script.css` (styling) |
| 96 | +- `popup.html` and `popup.js` (popup interface) |
| 97 | +- `IndependentPanel.html` and `IndependentPanel.js` (standalone chat page) |
| 98 | +- `shared.js` (shared vendor/runtime; size varies by environment and dependencies) |
| 99 | +- `logo.png` (extension icon) |
| 100 | +- `rules.json` (declarative net request rules) |
| 101 | + |
| 102 | +Bundle sizes are approximate and not validation criteria. |
| 103 | + |
| 104 | +### Verify Script Limitations |
| 105 | + |
| 106 | +- `npm run verify` tests search engine configurations by attempting to fetch search results from external search engines (Bing, Yahoo, Baidu, Naver) to validate that the site adapters can parse and handle real responses. |
| 107 | +- **Successful validation**: For each search engine, the script expects to receive a valid HTTP response (status 200) and to successfully extract and parse search results using the corresponding site adapter. If the adapter can parse the expected data structure from the response, the test is considered a pass. |
| 108 | +- **Expected failure modes**: In sandboxed or CI environments, the script may fail due to network restrictions (e.g., DNS errors, timeouts, connection refused), HTTP errors (e.g., 403, 429, 503), or changes in the search engine's response format. These failures are expected and do **not** indicate build problems. |
| 109 | +- If you see network or HTTP errors during `npm run verify`, you can safely ignore them unless you are specifically testing or updating site adapter logic. |
| 110 | + |
| 111 | +Usage notes: |
| 112 | + |
| 113 | +- Default checks target: `https://www.bing.com/search?q=hello`, `https://search.yahoo.co.jp/search?p=hello`, `https://www.baidu.com/s?wd=hello`, `https://search.naver.com/search.naver?query=hello` |
| 114 | +- Optional engines (may be blocked by region or anti-bot measures): Google, DuckDuckGo, Brave, Searx. |
| 115 | +- Troubleshooting: If a site fails, try adjusting `Accept-Language`/`User-Agent` headers in the script, update the site's selector arrays with ordered fallbacks, or temporarily reduce the test to a single URL while iterating. |
| 116 | + |
| 117 | +## Development Workflow |
| 118 | + |
| 119 | +### Code Style, Quality, and File Organization |
| 120 | + |
| 121 | +- ALWAYS run `npm run lint` before committing - CI will fail otherwise |
| 122 | +- ALWAYS run `npm run pretty` to format code consistently |
| 123 | +- ESLint configuration in `.eslintrc.json` enforces React/JSX standards |
| 124 | +- Prettier configuration in `.prettierrc` handles formatting |
| 125 | + |
| 126 | +- Naming conventions: component directories use PascalCase; feature folders use kebab-case; entry files are typically `index.jsx` or `index.mjs` |
| 127 | +- Avoid heavy dependencies; if necessary, justify and keep bundle size under control |
| 128 | + |
| 129 | +**Pre-commit hooks automatically:** |
| 130 | + |
| 131 | +1. Run prettier formatting |
| 132 | +2. Stage formatted files |
| 133 | +3. Run lint checks |
| 134 | + |
| 135 | +**Key file locations:** |
| 136 | + |
| 137 | +- Configuration: `src/config/index.mjs` |
| 138 | +- API integrations: `src/services/apis/` |
| 139 | +- Localization: `src/_locales/` |
| 140 | +- UI components: `src/components/` |
| 141 | +- Utilities: `src/utils/` |
| 142 | + |
| 143 | +### Commits & PRs |
| 144 | + |
| 145 | +- Keep changes minimal and focused. Avoid unrelated refactors in the same PR. |
| 146 | +- Commit subject: imperative, capitalize first word; separate subject/body with a blank line; wrap at ~72 characters; explain what and why. |
| 147 | +- PRs: link related issues, summarize scope/behavior changes; include screenshots for UI changes. |
| 148 | +- Note i18n updates in PR description when `src/_locales/` changes. |
| 149 | + |
| 150 | +### Directory Structure |
| 151 | + |
| 152 | +```text |
| 153 | +src/ |
| 154 | +├── background/ # Background script/service worker |
| 155 | +├── components/ # Reusable UI components |
| 156 | +├── config/ # Configuration management |
| 157 | +├── content-script/ # Main content script and features |
| 158 | +│ ├── site-adapters/ # Website-specific integrations |
| 159 | +│ ├── selection-tools/ # Text selection features |
| 160 | +│ └── menu-tools/ # Context menu features |
| 161 | +├── pages/IndependentPanel/ # Standalone chat page |
| 162 | +├── popup/ # Extension popup |
| 163 | +├── services/ # API clients and wrappers |
| 164 | +└── utils/ # Helper functions |
| 165 | +``` |
| 166 | + |
| 167 | +## Platform-Specific Instructions |
| 168 | + |
| 169 | +### Safari Build (macOS Only) |
| 170 | + |
| 171 | +- Run `npm run build:safari` (requires macOS with Xcode installed) |
| 172 | +- Creates `Fission - ChatBox.app` bundle and `safari.dmg` installer |
| 173 | +- Uses `safari/build.sh` script with platform-specific patches |
| 174 | + |
| 175 | +### Cross-Browser Compatibility |
| 176 | + |
| 177 | +- Uses `webextension-polyfill` for API compatibility |
| 178 | + |
| 179 | +## Security & Privacy |
| 180 | + |
| 181 | +- Do not commit secrets, API keys, or user data |
| 182 | +- Keep manifest permissions minimal and justify any additions |
| 183 | +- Centralize network/API logic under `src/services/apis/` and keep endpoints auditable |
| 184 | + |
| 185 | +## Localization |
| 186 | + |
| 187 | +- Source of truth: `src/_locales/en/main.json`; do not change keys |
| 188 | +- Add new strings to `en/main.json` first, then propagate to other locales |
| 189 | +- Register new locales in `src/_locales/resources.mjs` |
| 190 | +- Preserve placeholders and product names; keep punctuation/quotes intact |
| 191 | +- For Traditional Chinese (Taiwan), use `src/_locales/zh-hant/main.json` and avoid zh‑CN terms |
| 192 | + |
| 193 | +## AI Model Support |
| 194 | + |
| 195 | +The extension supports multiple AI providers: |
| 196 | + |
| 197 | +- **Web (cookie-based)**: ChatGPT (Web), Claude (Web), Kimi.Moonshot (Web), Bing (Web), Bard (Web), Poe (Web) |
| 198 | +- **APIs (key-based)**: OpenAI (API), Azure OpenAI (API), Anthropic (Claude API), OpenRouter (API), AI/ML (API), DeepSeek (API), Ollama (local), ChatGLM (API), Waylaidwanderer (API), Kimi.Moonshot (API) |
| 199 | +- **Custom/self-hosted**: Alternative endpoints and self-hosted backends |
| 200 | + |
| 201 | +## Troubleshooting |
| 202 | + |
| 203 | +### Build Issues |
| 204 | + |
| 205 | +- Build failures: Check Node.js version (requires Node 20+), clear caches and rebuild. |
| 206 | + - macOS/Linux: `rm -rf node_modules && npm ci && rm -rf node_modules/.cache build/ dist/` |
| 207 | + - Windows (PowerShell): `Remove-Item -Recurse -Force node_modules, build, dist; if (Test-Path node_modules\.cache) { Remove-Item -Recurse -Force node_modules\.cache }; npm ci` |
| 208 | +- "Module not found" errors: Usually indicate missing `npm ci` |
| 209 | + |
| 210 | +### Runtime Issues |
| 211 | + |
| 212 | +- Extension not loading: Check console for manifest errors |
| 213 | +- API not working: Verify browser has required permissions and cookies |
| 214 | +- Selection tools not appearing: Check if content script loaded correctly |
| 215 | + |
| 216 | +### Common Development Tasks |
| 217 | + |
| 218 | +- Adding new site adapter: Create new file in `src/content-script/site-adapters/`, register it in `src/content-script/site-adapters/index.mjs`, keep selectors minimal with feature detection, and verify on Chromium/Firefox |
| 219 | +- Adding new selection tool: Modify `src/content-script/selection-tools/`, keep UI and logic separate, and reuse helpers in `src/utils/` |
| 220 | +- Updating API integration: Modify files in `src/services/apis/` |
| 221 | +- Adding new UI component: Create in `src/components/` |
| 222 | + |
| 223 | +## Time Expectations |
| 224 | + |
| 225 | +- Do not interrupt builds or long-running commands unless they appear hung or unresponsive. |
| 226 | +- `npm ci`: ~30 seconds |
| 227 | +- `npm run build`: ~35 seconds (measured). Set timeout to 5-10 minutes for system variations. |
| 228 | +- `npm run dev`: ~15 seconds initial build, then watches for changes; use Ctrl+C to stop when switching branches or after config/dependency changes. |
| 229 | +- `npm run lint`: ~5 seconds |
| 230 | +- Manual extension testing: 5-10 minutes for thorough validation |
| 231 | +- Safari build: 2-5 minutes (macOS only) |
| 232 | + |
| 233 | +## Critical Validation Steps |
| 234 | + |
| 235 | +1. ALWAYS run `npm run build` after any code changes |
| 236 | +2. ALWAYS manually load and test the built extension in a browser (no automated testing available) |
| 237 | +3. ALWAYS verify the build creates expected file structure (non-empty bundles, no missing files) |
| 238 | +4. ALWAYS test core extension functionality (popup, content script injection, keyboard shortcuts) |
| 239 | + |
| 240 | +Always build and manually test the extension in a browser before considering any change complete. Simply running the build is NOT sufficient - you must load the extension and test actual functionality. |
| 241 | + |
| 242 | +--- |
| 243 | + |
| 244 | +Most of this document was generated by AI and reviewed under human supervision. If you find any clear errors while using it, please submit corrections with supporting evidence where possible. |
0 commit comments