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 ─────────────────────────────────────────────────────────────
|
// ─── LLM call ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
async function runAction(action: string) {
|
async function runAction(action: string) {
|
||||||
if (!selectedText) return;
|
if (!selectedText) return;
|
||||||
const textToProcess = selectedText;
|
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
|
// Show loading in toolbar
|
||||||
if (toolbar) {
|
if (toolbar) {
|
||||||
toolbar.innerHTML = '';
|
toolbar.innerHTML = '';
|
||||||
@@ -235,9 +265,13 @@ export default defineContentScript({
|
|||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
hideToolbar();
|
hideToolbar();
|
||||||
|
if (String(err).includes('Extension context invalidated')) {
|
||||||
|
showErrorToast('LexAI was updated. Please refresh the page.');
|
||||||
|
} else {
|
||||||
showModal(`❌ Error: ${String(err)}`, null);
|
showModal(`❌ Error: ${String(err)}`, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ─── Result Modal ─────────────────────────────────────────────────────────
|
// ─── Result Modal ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user