A sophisticated Chrome extension that enables AI-powered conversations about any webpage with advanced content extraction, intelligent routing, smart caching, and a premium glassmorphic UI. Built with LangChain, Google Gemini, and modern web technologies.
- Smart Content Extraction - Mozilla Readability + DOMPurify + Turndown pipeline for pristine markdown
- Auth-Aware - Works on authenticated pages using browser cookies (Notion, Gmail, paywalled sites)
- Real-Time Streaming - Progressive AI responses with live streaming
- Persistent Conversations - Tab-based chat history with session storage
- Dynamic Content - Handles JavaScript-rendered and SPA pages
- Smart Caching - Instant reload - caches embeddings and summaries per page/URL
- Intelligent Rate Limit Handling - Auto-detects 429 errors and offers model fallback with one click
- 4-Path Adaptive Routing - Automatically selects optimal strategy based on document size
- Token Optimization - 25-30% reduction through advanced text cleaning
- Hybrid Summary+Search - Best-of-both-worlds approach for quality + cost efficiency
- LLM-Driven Auto-Escalation - Seamlessly switches from summary to detailed retrieval when needed
- Cost Optimization - Up to 87% cost reduction vs naive approaches
- Processing Cache - No rebuilding on reopen - instant load from cache
- Markdown-Aware Splitting - Respects document structure (headers, code blocks, lists) for better chunks
- Dynamic Chunk Sizing - Automatically adjusts chunk size (500-2500) with 10-20% overlap based on document length
- Model Fallback Chain - Seamless switching between 4 Gemini models when rate limited
- Glassmorphic Design - Modern frosted-glass aesthetic with backdrop blur
- Dark Mode - Seamless theme switching with system detection
- Toast Notifications - Non-intrusive feedback (success, error, info, warning)
- Processing Indicators - Real-time status during background operations
- Mode Badges - Visual indicators (π¦ Direct, β‘ Hybrid, π RAG)
- Token Counter - Live token count with cost estimation
- Document Stats - Word count and reading time display
- Markdown Rendering - Beautiful formatting with syntax highlighting
- Keyboard Shortcuts - Cmd/Ctrl+K (new chat), Cmd/Ctrl+L (clear), Escape (close)
| Technology | Purpose |
|---|---|
| LangChain | LLM orchestration, chains, prompts, and memory management |
| Google Gemini 2.5 Flash | Lightning-fast AI for chat and summarization |
| Google Embeddings API | text-embedding-004 model for semantic search |
| Mozilla Readability | Article extraction (powers Firefox Reader View) |
| DOMPurify | XSS-proof HTML sanitization |
| Turndown | HTML to Markdown conversion |
| Marked.js | Markdown rendering with GFM support |
| Highlight.js | Syntax highlighting for code blocks |
| Vite | Next-gen build tool with HMR |
| CRXJS | Chrome extension dev with Vite |
| Lucide | Beautiful consistent icons |
- Node.js (v18 or higher)
- Google AI Studio API Key
-
Clone the repository
git clone <repository-url> cd langchain-chrome-extension
-
Install dependencies
npm install
-
Build the extension
npm run build
-
Load in Chrome
- Open
chrome://extensions/ - Enable "Developer mode" (top right toggle)
- Click "Load unpacked"
- Select the
distfolder
- Open
-
Navigate to any webpage you want to chat about
-
Click the extension icon in Chrome's toolbar
-
Enter your Gemini API key (first time only)
- Get a free key from Google AI Studio
- The key is stored locally and never shared
-
Start chatting!
- Ask questions about the page content
- Follow up with related questions
- The AI remembers your conversation
- "What is this article about?"
- "Summarize the main points in 3 bullets"
- "What did the author say about X?"
- "Can you explain that technical concept in simpler terms?"
- "Compare and contrast the arguments presented"
- "What are the key takeaways?"
Visual Indicators:
- π¦ Direct Mode - Small doc, full content sent
- β‘ Hybrid Mode - Medium doc, summary + search
- π RAG Mode - Large doc, retrieval only
- πͺ Token Counter - Live count with cost (e.g., "12.5K tokens β’ $0.009")
- π Document Stats - Word count and estimated reading time
Notifications:
- β Green toast - Success (API key saved, ready to chat)
- β Red toast - Error (rate limit, API issues)
β οΈ Yellow toast - Warning (fallback mode)- βΉοΈ Blue toast - Info (processing status)
Keyboard Shortcuts:
Cmd/Ctrl + K- Clear chat and start new conversationCmd/Ctrl + L- Clear current inputEscape- Close extension popupEnter- Send messageShift + Enter- New line in message
langchain-chrome-extension/
βββ src/
β βββ popup/
β β βββ index.html # Chat UI markup
β β βββ main.js # LangChain integration & chat logic
β β βββ style.css # Modern styling
β βββ content/
β β βββ content.js # Page content extraction
β βββ background/
β βββ background.js # Service worker & history cleanup
βββ public/
β βββ icons/ # Extension icons (16, 48, 128px)
βββ dist/ # Built extension (load this in Chrome)
βββ manifest.json # Extension configuration
βββ vite.config.js # Vite build configuration
βββ package.json
| Command | Description |
|---|---|
npm run dev |
Start development server with hot reload |
npm run build |
Build for production |
npm run preview |
Preview production build |
npm run devThis starts Vite's dev server with hot module replacement. Changes to your code will automatically update the extension.
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Content β β Popup β β Background β
β Script ββββββΆβ (Chat UI) ββββββΆβ Service β
β β β β β Worker β
β Readability + β β Smart Router: β β Clears history β
β DOMPurify + β β Stuffing/Hybrid β β on tab close β
β Turndown + β β Summary/RAG β β β
β Text Cleaning β β + Gemini API β β β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
The extension automatically chooses the best strategy based on document size:
Document Size Detection:
ββ < 10k tokens β Path A: Direct Stuffing
β ββ Strategy: Send full content to LLM
β ββ Speed: Instant β‘
β ββ Cost: ~$0.002/query
β
ββ 10k-20k tokens β Path B: Hybrid Summary+Search β
β ββ Strategy: Compact summary (500-1k tokens) + embeddings
β ββ Auto-escalation: Summary first β retrieval if needed
β ββ Speed: 1-2s (summary) or 3-4s (retrieval)
β ββ Cost: ~$0.0003/query (summary) or ~$0.001 (retrieval)
β
ββ 20k-30k tokens β Path C: Summary-First with Auto-Escalation
β ββ Strategy: Standard summary (1k-2k tokens) + embeddings
β ββ Auto-escalation: Same as Path B
β ββ Speed: 2-3s (summary) or 4-5s (retrieval)
β ββ Cost: ~$0.0004/query average
β
ββ > 30k tokens β Path D: Pure RAG
ββ Strategy: Chunk + embed + retrieve only
ββ Speed: 3-4s per query
ββ Cost: ~$0.001/query
When using Hybrid mode, the system intelligently routes questions:
-
General questions β Answered from summary
- "What's this article about?"
- "What's the author's main argument?"
- "Summarize the key points"
- Result: Fast, cheap (~$0.0003)
-
Specific questions β Auto-escalates to retrieval
- LLM detects need:
π NEEDS_DETAIL: [description] - System retrieves relevant chunks
- Answers with exact details
- Result: Accurate, still efficient (~$0.001)
- LLM detects need:
User sees: "Searching for [detail]..." then detailed answer
1. Readability (Mozilla)
ββ Extracts "Reader View" content
ββ Removes: ads, navigation, sidebars, footers
2. DOMPurify
ββ Sanitizes HTML (XSS protection)
ββ Removes: scripts, dangerous attributes
3. Turndown
ββ Converts to Markdown
ββ Preserves: headings, lists, tables, code blocks, links
4. Advanced Text Cleaning (15 regex patterns)
ββ Removes: copyright footers, "related articles", comments sections
ββ Removes: social buttons, email prompts, excessive whitespace
ββ Result: 25-30% token reduction
Cache Behavior:
First Open (Cold Cache):
ββ Extract content from page
ββ Generate summary (if needed) β±οΈ 5-15s
ββ Create embeddings β±οΈ 10-30s
ββ Save to chrome.storage.session
ββ Total: 15-45 seconds
Reopen Same Page (Warm Cache):
ββ Check cache: matching tabId + URL β
ββ Restore summary (instant)
ββ Restore embeddings (instant)
ββ Total: <1 second β‘
Different Page or URL Changed:
ββ Cache miss (URL mismatch)
ββ Process as new page
ββ Create new cache entry
Cache Storage:
- Location:
chrome.storage.session(temporary, browser session only) - Cleared: When browser closes or tab closes completely
- Size: Embeddings + summary (~100KB - 2MB per page)
- No disk footprint: All in-memory during session
Benefits:
- β‘ 95-99% faster on reopen
- π° Zero API cost for cached pages
- π― Only process once per page visit
- π Privacy-first: Data cleared automatically
- ChatGoogleGenerativeAI - Gemini chat model wrapper
- GoogleGenerativeAIEmbeddings - Gemini embeddings for RAG
- ChatPromptTemplate - Structured prompts with system messages
- MessagesPlaceholder - Injects conversation history into prompts
- RunnableSequence - Chains components together
- StringOutputParser - Parses streaming text output
- RecursiveCharacterTextSplitter - Markdown-aware splitting with dynamic chunk sizing (500-2500 chars, 10-20% overlap)
- HumanMessage / AIMessage - Message types for history
- MemoryVectorStore - In-browser embedding storage (no external DB)
- Cosine Similarity - Custom retrieval using vector similarity
- Auto-Escalation Logic - Detects LLM signals for detail needs
- Parallel Processing - Summary + embeddings generated simultaneously
- Page Load β Readability extracts clean content β DOMPurify sanitizes β Turndown converts to Markdown β Advanced cleaning
- Size Detection β Estimates tokens β Routes to appropriate path (A/B/C/D)
- Preprocessing (if needed):
- Path B/C: Generate summary + embeddings in parallel
- Path D: Generate embeddings only
- User Question β Added to chat history
- Smart Routing:
- Hybrid mode: Try summary β Auto-escalate if needed
- RAG mode: Retrieve relevant chunks
- Direct mode: Use full content
- AI Response β Streamed to UI, added to history, saved to storage
- Tab Close β Background script clears that tab's history
Scenario: 15k token document, 10 queries (8 general + 2 specific)
| Approach | Cost Breakdown | Total |
|---|---|---|
| Naive Stuffing | 10 Γ $0.01 | $0.10 |
| Pure Summary | $0.01 + 9 Γ $0.0003 | $0.013 |
| Hybrid (This Extension) β | Setup: $0.01 + Summary: $0.0024 + Specific: $0.002 | $0.0144 |
| Savings | - | 85-87% |
| Mode | Context Size | Cost/Query |
|---|---|---|
| Direct Stuffing | 10k tokens | ~$0.002 |
| Hybrid (Summary) | 500-1k tokens | ~$0.0003 |
| Hybrid (Retrieval) | 4k tokens | ~$0.001 |
| Pure RAG | 4k tokens | ~$0.001 |
Key Insight: 80% of questions are general β Hybrid mode answers 80% of queries at 1/3 the cost!
Edit src/popup/main.js:
const model = new ChatGoogleGenerativeAI({
model: 'gemini-2.5-flash-lite', // Change model here
apiKey: apiKey,
streaming: true,
});Available fast models:
gemini-2.5-flash-lite(fastest)gemini-2.5-flash(balanced)gemini-2.0-flash(legacy)
Edit the history limit in src/popup/main.js:
// Keep last N exchanges (currently 10 exchanges = 20 messages)
if (chatHistory.length > 20) {
chatHistory = chatHistory.slice(-20);
}Change when different routing modes activate in src/popup/main.js:
async function processPageContent(apiKey) {
const estimatedTokens = markdown.length / 4;
if (estimatedTokens < 10000) { // Path A: Direct
} else if (estimatedTokens < 20000) { // Path B: Hybrid
} else if (estimatedTokens < 30000) { // Path C: Hybrid+
} else { // Path D: RAG
}
}Adjust retrieval settings:
// In retrieveRelevantChunks() function
const relevantChunks = await retrieveRelevantChunks(
question,
4, // k: number of chunks to retrieve (increase for more context)
0.5 // scoreThreshold: minimum similarity (0-1, lower = more results)
);
// In setupRAGPipeline() function
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000, // Size of each chunk
chunkOverlap: 200, // Overlap between chunks
});| Permission | Reason |
|---|---|
activeTab |
Access current tab's content |
storage |
Store API key and chat history |
scripting |
Inject content script |
tabs |
Detect tab closure for history cleanup |
The extension applies aggressive text cleaning to reduce token usage:
Removes:
- Copyright footers and legal text
- "Related Articles" / "You May Also Like" sections
- Comments sections
- Social media share buttons
- Email subscription prompts
- Navigation breadcrumbs
- Standalone URLs and reference links
- Excessive whitespace
Result: 25-30% token reduction while preserving actual content
Converting to Markdown helps LLMs understand document structure:
- Headings (
#,##,###) β Better section understanding - Lists (
-,1.) β Clearer enumeration - Tables β Structured data comprehension
- Code blocks β Syntax preservation
Custom implementation with no external dependencies:
- Storage: Plain JavaScript arrays in memory
- Embeddings: Gemini
text-embedding-004model - Retrieval: Cosine similarity calculation
- Performance: ~100ms for similarity search across 100 chunks
- Privacy: Everything stays in your browser
The system uses a special prompt pattern to detect when details are needed:
LLM Response: "π NEEDS_DETAIL: specific date from section 3"
β
Extension detects signal
β
Retrieves relevant chunks
β
Re-queries with detailed context
β
Returns specific answer
This happens automatically without user intervention.
- Refresh the extension in
chrome://extensions/ - Remove and re-add the extension
- Make sure you loaded the
distfolder, not the root folder
- Refresh the webpage and reopen the extension
- Some pages cannot be accessed:
chrome://,chrome-extension://,edge://,about:URLs - Very dynamic SPAs may need a moment to fully render before extraction
- Check browser console (F12) for detailed error messages
"π« Rate Limit Reached - Switch Model to Continue"
- Extension detects rate limits instantly and offers to switch to next model
- Model fallback chain: gemini-2.5-flash-lite β gemini-2.5-flash β gemini-2.0-flash-lite β gemini-2.0-flash
- Click "Switch Model & Continue" to automatically retry with next model
- If all models exhausted, wait 60 seconds and try again
- Free tier limits: 15 requests/minute per model, 1,500 requests/day
- Check quota: Google AI Studio
"π§ Gemini API is temporarily unavailable (503)"
- Google's servers are overloaded (temporary)
- Wait 10-30 seconds and try again
- Usually resolves within seconds
"β Invalid API key"
- Verify your API key at Google AI Studio
- Generate a new key if needed
- Make sure you copied the entire key
Slow initial processing
- Normal: Large documents (15k+ tokens) take 15-45 seconds to process
- Processing happens once - subsequent reopens are instant (<1s) thanks to caching
- Progress shown: Watch the processing status indicator for updates
- "Creating Summary", "Building Search Index", etc.
Cache not working
- Cache is per-tab + URL - changing URL creates new cache
- Cache cleared when browser closes or tab closes completely
- Check console for: "β Loaded processing cache from [timestamp]"
Theme not persisting
- Theme preference is stored in
localStorage - Clearing browser data will reset to system preference
- Manual toggle always overrides system preference
Toast notifications not showing
- Check if browser notifications are blocked
- Look for toasts in top-right corner of popup
- They auto-dismiss after 3-5 seconds
Keyboard shortcuts not working
- Make sure extension popup is focused
- Shortcuts:
Cmd/Ctrl+K,Cmd/Ctrl+L,Escape - Refresh extension if shortcuts stop responding
Mode badge showing wrong mode
- This updates after processing completes
- Badge reflects the actual strategy being used
- π¦ Direct, β‘ Hybrid, π RAG
Open DevTools (F12) on the popup for detailed logs:
- Processing status: "π No cache found - processing document"
- Cache hits: "π¦ Using cached processing"
- API calls: "Initializing LangChain in [mode] mode"
- Errors: Full stack traces with context
MIT License - feel free to use this project for learning or as a base for your own extensions.
- LangChain for the excellent JS/TS library and RAG implementation
- Google AI for Gemini API (chat and embeddings)
- Mozilla for Readability - the gold standard in content extraction
- DOMPurify for bulletproof HTML sanitization
- Turndown for reliable HTML-to-Markdown conversion
- CRXJS for making Chrome extension development with Vite seamless
This extension demonstrates several advanced RAG (Retrieval-Augmented Generation) techniques:
- Intelligent 4-Path Routing - Automatically chooses between stuffing, hybrid, and RAG based on document size
- Hybrid Summary+Search - Novel approach combining summaries with retrieval for optimal cost/quality
- LLM-Driven Auto-Escalation - Intelligent detection of when to switch from summary to detailed retrieval
- Smart Caching System - Session-based cache for instant reload without API calls (95-99% faster)
- Token Optimization - Multi-stage cleaning pipeline (Readability β DOMPurify β Turndown β 15 regex patterns)
- In-Browser Vector Store - Custom cosine similarity implementation requiring no external database
- Parallel Processing - Summary and embeddings generated simultaneously for 2x faster setup
- Auth-Aware Extraction - Uses browser cookies for authenticated pages (Notion, Gmail, paywalled sites)
- Premium UI/UX - Glassmorphic design, dark mode, toast notifications, processing indicators
- Markdown Rendering - Beautiful formatting with syntax highlighting and copy buttons
- Markdown-Aware Splitting - Respects document structure across all modes (headers, code blocks, lists, tables)
- Dynamic Chunk Sizing - Automatically adjusts chunk size and overlap (10-20%) based on document length
- Smart Rate Limit Detection - 3-guard system with cooldown, console clearing, and instant feedback
- Model Fallback Chain - Automatic model switching with one-click retry when rate limited
Performance: 95-99% faster on reopen thanks to intelligent caching Cost efficiency: Up to 87% reduction compared to naive approaches while maintaining quality User Experience: Professional, polished interface with real-time feedback
Built with β€οΈ for efficient, intelligent web content interaction.