fix: handle extension context invalidated error gracefully
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user