From e34f4aba4be9b1dcadefd1c1527361005e4d7284 Mon Sep 17 00:00:00 2001 From: Forge Date: Fri, 6 Mar 2026 13:43:57 +0800 Subject: [PATCH] fix: handle extension context invalidated error gracefully --- entrypoints/content.ts | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/entrypoints/content.ts b/entrypoints/content.ts index ee7bac1..09975a5 100644 --- a/entrypoints/content.ts +++ b/entrypoints/content.ts @@ -200,12 +200,42 @@ export default defineContentScript({ } } + // ─── Extension context guard ────────────────────────────────────────────── + + function isExtensionValid(): boolean { + try { + return !!chrome.runtime?.id; + } catch { + return false; + } + } + + function showErrorToast(msg: string) { + const toast = document.createElement('div'); + toast.style.cssText = ` + position: fixed; bottom: 20px; right: 20px; z-index: 999999; + background: #f38ba8; color: #1e1e2e; padding: 10px 16px; + border-radius: 8px; font-family: system-ui, sans-serif; + font-size: 13px; box-shadow: 0 4px 12px rgba(0,0,0,0.3); + `; + toast.textContent = msg; + document.body.appendChild(toast); + setTimeout(() => toast.remove(), 4000); + } + // ─── LLM call ───────────────────────────────────────────────────────────── async function runAction(action: string) { if (!selectedText) return; const textToProcess = selectedText; + // Guard: extension may have been reloaded/updated + if (!isExtensionValid()) { + hideToolbar(); + showErrorToast('LexAI was updated. Please refresh the page.'); + return; + } + // Show loading in toolbar if (toolbar) { toolbar.innerHTML = ''; @@ -235,7 +265,11 @@ export default defineContentScript({ } } catch (err) { hideToolbar(); - showModal(`❌ Error: ${String(err)}`, null); + if (String(err).includes('Extension context invalidated')) { + showErrorToast('LexAI was updated. Please refresh the page.'); + } else { + showModal(`❌ Error: ${String(err)}`, null); + } } }