refactor: extract shared types + safe chrome wrappers into src/lib
- src/lib/types.ts: message contract (both ANALYZE_TEXT shapes preserved), config/response types, storage-key constants. - src/lib/messaging.ts: single safeStorageGet/safeStorageSet/safeSendMessage/ isExtensionValid implementation replacing the three divergent copies in Options, Popup, and content. Content script keeps its refresh-toast behavior via an onContextInvalidated callback. - New '@lib' import alias (wxt force-overwrites '~' and '@' to srcDir, so those cannot point at ./src); wired in wxt.config, tsconfig, vitest. - tests/unit/messaging.test.ts: 14 unit tests over the wrappers. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { defineContentScript } from 'wxt/utils/define-content-script';
|
||||
import { isExtensionValid, safeSendMessage } from '@lib/messaging';
|
||||
|
||||
export default defineContentScript({
|
||||
matches: ['<all_urls>'],
|
||||
@@ -362,15 +363,7 @@ export default defineContentScript({
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Extension context guard ──────────────────────────────────────────────
|
||||
|
||||
function isExtensionValid(): boolean {
|
||||
try {
|
||||
return typeof chrome !== 'undefined' && !!chrome.runtime?.id;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Extension context guard: isExtensionValid is imported from ~/lib/messaging.
|
||||
|
||||
function showErrorToast(msg: string) {
|
||||
const toast = document.createElement('div');
|
||||
@@ -528,7 +521,7 @@ export default defineContentScript({
|
||||
grid.style.display = 'none';
|
||||
spinnerSlot.style.display = 'flex';
|
||||
|
||||
const response = await safeSendMessage({
|
||||
const response = await sendToBackground({
|
||||
type: 'COPY_AS',
|
||||
text,
|
||||
format: fmt,
|
||||
@@ -563,22 +556,13 @@ export default defineContentScript({
|
||||
}
|
||||
|
||||
// ─── Safe chrome.runtime.sendMessage wrapper ──────────────────────────────
|
||||
// Shared implementation; on a stale extension context we toast and clean up.
|
||||
|
||||
async function safeSendMessage(payload: Record<string, unknown>): Promise<unknown> {
|
||||
if (!isExtensionValid()) {
|
||||
async function sendToBackground(payload: Record<string, unknown>): Promise<unknown> {
|
||||
return safeSendMessage(payload, () => {
|
||||
showErrorToast('LexAI was updated — please refresh this page.');
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return await chrome.runtime.sendMessage(payload);
|
||||
} catch (err) {
|
||||
if (String(err).includes('Extension context invalidated') ||
|
||||
String(err).includes('message channel closed')) {
|
||||
showErrorToast('LexAI was updated — please refresh this page.');
|
||||
hideToolbar();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
hideToolbar();
|
||||
});
|
||||
}
|
||||
|
||||
// ─── LLM call ─────────────────────────────────────────────────────────────
|
||||
@@ -628,14 +612,14 @@ export default defineContentScript({
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await safeSendMessage({
|
||||
const response = await sendToBackground({
|
||||
type: 'ANALYZE_TEXT',
|
||||
payload: { text: textToProcess, action, style: currentStyle },
|
||||
}) as { error?: string; result?: string } | null;
|
||||
|
||||
hideToolbar();
|
||||
|
||||
if (response === null) return; // safeSendMessage already handled the error
|
||||
if (response === null) return; // sendToBackground already handled the error
|
||||
|
||||
if (response?.error) {
|
||||
showModal(`❌ ${response.error}`, null, snapStart, snapEnd, snapElement, snapRange, action, textToProcess);
|
||||
@@ -832,7 +816,7 @@ export default defineContentScript({
|
||||
regenBtn.disabled = true;
|
||||
styleSelect.disabled = true;
|
||||
|
||||
const response = await safeSendMessage({
|
||||
const response = await sendToBackground({
|
||||
type: 'ANALYZE_TEXT',
|
||||
payload: { text: textForRegenerate, action, style: chosenStyle },
|
||||
}) as { error?: string; result?: string } | null;
|
||||
|
||||
Reference in New Issue
Block a user