feat(browser): Cookie 备份导入——配套扩展走 chrome.cookies 官方明文通道

This commit is contained in:
Rick Ye
2026-09-13 01:22:21 +08:00
parent 2efcd8e346
commit 573ac5bcde
9 changed files with 309 additions and 8 deletions
@@ -71,6 +71,24 @@ export function BrowserImportForm(): JSX.Element {
.finally(() => setBusy(false))
}
const runBackupImport = (file: File | null): void => {
if (!file || busy) return
setBusy(true)
setError(null)
setResult(null)
try {
const path = window.api.pathForFile(file)
window.api
.browserImportCookiesFile(path)
.then(setResult)
.catch((err) => setError(err instanceof Error ? err.message : String(err)))
.finally(() => setBusy(false))
} catch (err) {
setBusy(false)
setError(err instanceof Error ? err.message : String(err))
}
}
const imported = state?.imported ?? null
return (
@@ -80,12 +98,51 @@ export function BrowserImportForm(): JSX.Element {
<span className="font-medium text-ink-2">完全退出</span>源浏览器(含后台进程)。
</p>
{/* Chrome 新版应用绑定加密:文件导入拿不到 Cookie,走配套扩展的官方明文通道 */}
<div className="mb-3 rounded border border-line px-2.5 py-2">
<div className="flex items-center justify-between gap-2">
<span className="text-[12px] font-medium text-ink-2">Cookie 备份导入</span>
<div className="flex shrink-0 items-center gap-1.5">
<button
type="button"
disabled={busy}
onClick={() => void window.api.browserOpenExporterFolder()}
className="rounded border border-line-strong px-2 py-0.5 text-[11px] text-ink-2 transition-colors hover:bg-hover hover:text-ink disabled:opacity-40"
>
扩展目录
</button>
<label
className={`cursor-pointer rounded border border-line-strong px-2 py-0.5 text-[11px] text-ink-2 transition-colors hover:bg-hover hover:text-ink ${
busy ? 'pointer-events-none opacity-40' : ''
}`}
>
选择备份文件
<input
type="file"
accept=".json,application/json"
className="hidden"
disabled={busy}
onChange={(e) => {
runBackupImport(e.target.files?.[0] ?? null)
e.currentTarget.value = ''
}}
/>
</label>
</div>
</div>
<div className="mt-1 text-[10.5px] leading-relaxed text-ink-3">
Chrome 新版已加密 Cookie(文件导入拿不到)。用配套扩展导出备份:点「扩展目录」,在 Chrome 的
chrome://extensions 加载该目录后一键导出,再在这里选择备份文件,立即生效。
</div>
</div>
{imported && (
<div className="mb-3 rounded border border-line bg-inset px-2.5 py-2">
<div className="flex items-center gap-2">
<span className="shrink-0 text-[11px] text-ink-3">已导入</span>
<span className="min-w-0 flex-1 truncate text-[12px] text-ink">
{imported.browser} · {imported.profile}
{imported.browser}
{imported.profile && <span> · {imported.profile}</span>}
{imported.profileDir && <span className="ml-1.5 text-[10.5px] text-ink-3">{imported.profileDir}</span>}
</span>
{imported.sourceId && imported.profileDir && (
+3
View File
@@ -116,6 +116,9 @@ declare global {
}) => Promise<void>
browserDetectSources: () => Promise<BrowserSource[]>
browserImportProfile: (sourceId: string, profileDir: string) => Promise<BrowserImportResult>
browserImportCookiesFile: (path: string) => Promise<BrowserImportResult>
browserOpenExporterFolder: () => Promise<string>
pathForFile: (file: File) => string
browserGetProxy: () => Promise<BrowserProxySettings>
browserSetProxy: (input: BrowserProxySettings) => Promise<BrowserProxySettings>
onBrowserEvent: (callback: (event: BrowserUiEvent) => void) => () => void