const $ = (id) => document.getElementById(id); async function refresh() { const res = await chrome.runtime.sendMessage({ type: "GET_STATUS" }); const r = res?.result || {}; const el = $("status"); if (r.connected) { el.className = "status on"; el.textContent = `已连接 · ${r.email || r.userId || ""}`; } else { el.className = "status off"; el.textContent = "未连接 — 请先在 Options 登录"; } } $("options").onclick = () => { chrome.runtime.openOptionsPage(); }; $("savePage").onclick = async () => { $("msg").textContent = ""; const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); if (!tab?.id) { $("msg").textContent = "无活动标签"; return; } try { // Prefer pending draft (post-login) via background; falls back to live form. const res = await chrome.runtime.sendMessage({ type: "PROMPT_PENDING", tabId: tab.id, }); if (!res?.ok) { $("msg").textContent = res?.error || "无法打开确认层"; return; } $("msg").textContent = "已在页面打开确认层"; window.close(); } catch (e) { $("msg").textContent = e?.message || "无法注入当前页(受保护页面或未加载)。请在普通 http(s) 页重试。"; } }; $("updateSession").onclick = async () => { $("msg").textContent = "匹配条目…"; const [tab] = await chrome.tabs.query({ active: true, currentWindow: true }); if (!tab?.url) { $("msg").textContent = "无活动标签 URL"; return; } try { const match = await chrome.runtime.sendMessage({ type: "MATCH_URL", pageUrl: tab.url, }); const items = match?.result?.items || []; if (!items.length) { $("msg").textContent = "无同站条目:请先保存账号"; return; } const id = items[0].id; const res = await chrome.runtime.sendMessage({ type: "UPDATE_SESSION", credentialId: id, pageUrl: tab.url, accountId: "primary", }); if (!res?.ok) { $("msg").textContent = res?.error || "更新失败"; return; } $("msg").textContent = `已更新登录态 · ${id}`; } catch (e) { $("msg").textContent = e?.message || String(e); } }; refresh();