fix: comprehensive extension context invalidated handling
Some checks failed
CI — Test & Build / Test & Build (push) Has been cancelled

This commit is contained in:
Forge
2026-03-06 13:56:30 +08:00
parent 4343abbab3
commit a0b00c6d87
3 changed files with 93 additions and 9 deletions

View File

@@ -151,6 +151,30 @@ const styles = {
} as React.CSSProperties,
};
// ─── Safe chrome storage wrappers ────────────────────────────────────────────
function safeStorageGet(keys: string[], callback: (result: Record<string, string>) => void) {
try {
if (typeof chrome === 'undefined' || !chrome.runtime?.id) return;
chrome.storage.local.get(keys, callback);
} catch (err) {
console.warn('LexAI: Extension context invalidated', err);
}
}
function safeStorageSet(data: Record<string, string>, callback?: () => void) {
try {
if (typeof chrome === 'undefined' || !chrome.runtime?.id) return;
if (callback) {
chrome.storage.local.set(data, callback);
} else {
chrome.storage.local.set(data);
}
} catch (err) {
console.warn('LexAI: Extension context invalidated', err);
}
}
// ─── Component ────────────────────────────────────────────────────────────────
function OptionsPage() {
@@ -162,7 +186,7 @@ function OptionsPage() {
const apiKeyRef = useRef<HTMLInputElement>(null);
useEffect(() => {
chrome.storage.local.get(["provider", "apiKey", "model"], (result: Record<string, string>) => {
safeStorageGet(["provider", "apiKey", "model"], (result) => {
if (result.provider) setProvider(result.provider);
if (result.apiKey) setApiKey(result.apiKey);
if (result.model) setModel(result.model);
@@ -183,7 +207,7 @@ function OptionsPage() {
return;
}
setSaveStatus('saving');
chrome.storage.local.set({ provider, apiKey: apiKey.trim(), model }, () => {
safeStorageSet({ provider, apiKey: apiKey.trim(), model }, () => {
if (chrome.runtime.lastError) {
setSaveStatus('error');
setTimeout(() => setSaveStatus('idle'), 3000);