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

@@ -1,13 +1,24 @@
import React, { useEffect, useState } from 'react';
import { createRoot } from 'react-dom/client';
// ─── Safe chrome storage wrapper ─────────────────────────────────────────────
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 Popup() {
const [configured, setConfigured] = useState(false);
const [provider, setProvider] = useState('');
const [model, setModel] = useState('');
useEffect(() => {
chrome.storage.local.get(["provider", "apiKey", "model"], (result: Record<string, string>) => {
safeStorageGet(["provider", "apiKey", "model"], (result) => {
if (result.apiKey) {
setConfigured(true);
setProvider(result.provider || 'openai');
@@ -16,7 +27,15 @@ function Popup() {
});
}, []);
const openSettings = () => chrome.runtime.openOptionsPage();
const openSettings = () => {
try {
if (typeof chrome !== 'undefined' && chrome.runtime?.id) {
chrome.runtime.openOptionsPage();
}
} catch (err) {
console.warn('LexAI: Extension context invalidated', err);
}
};
return (
<div style={{ padding: 16, fontFamily: 'system-ui, sans-serif', color: '#1e1e2e' }}>