48 lines
1.8 KiB
JavaScript
48 lines
1.8 KiB
JavaScript
// 导出全部 Cookie 为 JSON 备份(chrome.cookies API 返回明文——Chrome 官方提供给扩展的通道)
|
|
const statusEl = document.getElementById('status')
|
|
const button = document.getElementById('export')
|
|
|
|
function setStatus(text) {
|
|
statusEl.textContent = text
|
|
}
|
|
|
|
button.addEventListener('click', async () => {
|
|
button.disabled = true
|
|
try {
|
|
setStatus('正在读取 Cookie…')
|
|
const cookies = await chrome.cookies.getAll({})
|
|
if (!cookies || cookies.length === 0) {
|
|
setStatus('未读取到任何 Cookie。')
|
|
return
|
|
}
|
|
const payload = {
|
|
format: 'aso-cookie-export',
|
|
version: 1,
|
|
exportedAt: new Date().toISOString(),
|
|
cookies: cookies.map((c) => ({
|
|
name: c.name,
|
|
value: c.value,
|
|
domain: c.domain,
|
|
hostOnly: c.hostOnly === true,
|
|
path: c.path || '/',
|
|
secure: c.secure === true,
|
|
httpOnly: c.httpOnly === true,
|
|
...(typeof c.expirationDate === 'number' ? { expirationDate: c.expirationDate } : {}),
|
|
...(c.sameSite && c.sameSite !== 'unspecified' ? { sameSite: c.sameSite } : {})
|
|
}))
|
|
}
|
|
const blob = new Blob([JSON.stringify(payload)], { type: 'application/json' })
|
|
const url = URL.createObjectURL(blob)
|
|
const d = new Date()
|
|
const pad = (n) => String(n).padStart(2, '0')
|
|
const filename = `aso-cookies-${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}.json`
|
|
await chrome.downloads.download({ url, filename, saveAs: true })
|
|
URL.revokeObjectURL(url)
|
|
setStatus(`已导出 ${payload.cookies.length} 条 Cookie。回到 Inkreach Agent:设置 → 浏览器 → Cookie 备份导入,选择刚保存的文件。`)
|
|
} catch (err) {
|
|
setStatus('导出失败:' + (err && err.message ? err.message : String(err)))
|
|
} finally {
|
|
button.disabled = false
|
|
}
|
|
})
|