File Structure
Knowledge Tooltip is a lightweight Chrome extension with no build tools or dependencies. All code is vanilla JavaScript.The entire extension runs in the browser with no backend server. All API calls are made directly to third-party services.
Architecture Overview
The extension uses Chrome’s message passing system to separate concerns and avoid CORS issues:Three-Layer Architecture
- Content Script (
content.js) - Runs on every webpage - Service Worker (
background.js) - Handles API calls - Popup UI (
popup.js) - Manages settings
This architecture is required because content scripts cannot make cross-origin requests due to CORS policies. The service worker acts as a proxy.
Core Files
manifest.json
The extension manifest defines permissions, scripts, and metadata:manifest.json
permissions- Chrome APIs the extension can usehost_permissions- External domains for API callsbackground- Service worker registrationcontent_scripts- Scripts injected into webpages
background.js
The service worker handles all external API calls. It receives messages fromcontent.js and returns data.
Structure:
- Message router that listens for
chrome.runtime.onMessage - Async functions for each API (Wikipedia, Wiktionary, Wikidata, OpenAI)
- Error handling and response formatting
background.js
content.js
The content script runs on every webpage and manages the tooltip UI and user interactions. Responsibilities:- Detect text selection and show floating button
- Build and render the tooltip UI
- Handle tab switching and caching
- Send API requests to
background.js - Render API responses in the appropriate tab
- Handle bilingual UI (English/Arabic)
content.js
content.js
styles.css
Contains all extension styles including:- Floating button styles
- Tooltip container and backdrop
- Tab navigation and content areas
- Loading states and animations
- RTL (right-to-left) support for Arabic
- Responsive design
- CSS variables for consistent theming
- Scoped styles to avoid conflicts with page styles
- Animations for smooth transitions
- RTL layout with
[dir="rtl"]selectors
popup.html / popup.js
The extension popup provides a settings interface: Settings:- Enable/disable toggle
- Language preference (auto-detect, English, Arabic)
- OpenAI API key management (add/remove)
- Preferences stored in
chrome.storage.sync(synced across devices) - API key stored in
chrome.storage.local(never synced)
popup.js
Data Flow
User Selects Text
Selection Detection
User highlights text on a webpage.
content.js detects the selection and shows a floating button.User Clicks Button
User clicks the button.
content.js creates the tooltip UI and automatically loads the Summary tab.Background Fetches Data
background.js receives the message, calls the Wikipedia API, and sends the response back:Tab Switching
When a user switches tabs:- Check if data is cached for this tab
- If cached, render immediately
- If not cached, show loading state
- Send request to
background.jsfor the appropriate API - Render response and cache it
The extension caches up to 30 recent queries to minimize API calls and improve performance.
Extension APIs Used
chrome.runtime
chrome.runtime.sendMessage()- Send messages between scriptschrome.runtime.onMessage- Listen for messages
chrome.storage
chrome.storage.sync- Synced preferences (enabled, language)chrome.storage.local- Local-only secrets (API key)
chrome.tabs
chrome.tabs.query()- Get list of tabschrome.tabs.sendMessage()- Send messages to content scripts
External APIs
| Tab | API | Endpoint | Key Required |
|---|---|---|---|
| Summary | Wikipedia REST API | en.wikipedia.org/api/rest_v1/ | No |
| Summary | Wikipedia Search API | en.wikipedia.org/w/api.php | No |
| Define | Wiktionary REST API | en.wiktionary.org/api/rest_v1/ | No |
| Define | Free Dictionary API | api.dictionaryapi.dev/api/v2/ | No |
| Facts | Wikidata Search | www.wikidata.org/w/api.php | No |
| Facts | Wikidata Entities | www.wikidata.org/wiki/Special:EntityData/ | No |
| AI | OpenAI Chat | api.openai.com/v1/chat/completions | Yes |
| Translate | OpenAI Chat | api.openai.com/v1/chat/completions | Yes |
All API calls are made directly from the browser. There is no backend server.
Next Steps
Development Setup
Set up your local development environment
Contribution Guidelines
Learn how to contribute code