Bugfix: How to fix "Extension context invalidated" error in Chrome e...
Documentation for Sessions
🐛 Bugfix: How to fix "Extension context invalidated" error in Chrome e...
Date: 2026-03-03
Time: 12:05:34 UTC
Session: gspace1-166.3
Agent: storage
Confidence: 95%
Status: ✅ Documented
📋 Decision Context
Question:
How to fix "Extension context invalidated" error in Chrome extension inject.js when switching Studio/Observatory tabs?
Task: Fix Extension context invalidated error in Chrome extension
🎯 Decision Made
Chosen:
Added 4 safe wrapper functions (safeStorageGet, safeStorageSet, safeSyncStorageGet, safeSendMessage) with isExtensionContextValid() check using chrome.runtime?.id. All 15 raw chrome.* API calls in inject.js replaced with safe wrappers. Shows clickable banner on first invalidation.
Options Considered:
- No alternatives documented
💭 Reasoning
Content scripts injected into third-party pages survive extension reloads/updates — but their chrome.* API references become invalid. Line 771 (chrome.storage.local.set in toggleFloatingPanelMode) was the immediate crash. The fix uses the standard chrome.runtime.id check (returns undefined when context is invalid), wraps all chrome API calls, and degrades gracefully: tab switching still works (iframe URL changes), just without persistence. The banner UX is clickable to reload the page, which is the only real fix for context invalidation.
📁 Files Involved
apps/vitto-chrome-extension/content/inject.js
🔗 Related
- Decision ID:
dev_1772539532865_tpzgl4x - Session: 166
- Workspace: gspace1
Auto-generated by MCP Smart Consolidation