c1d3046778
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
57 lines
1.6 KiB
JavaScript
57 lines
1.6 KiB
JavaScript
/**
|
|
* Cookie extraction + Netscape format conversion.
|
|
* Direct port of GS extension/lib/cookies.js.
|
|
*/
|
|
|
|
async function extractCookiesForPlatform(platformKey) {
|
|
const platform = PLATFORMS[platformKey];
|
|
if (!platform) throw new Error(`Unknown platform: ${platformKey}`);
|
|
|
|
const all = [];
|
|
for (const domain of platform.domains) {
|
|
try {
|
|
const cookies = await browser.cookies.getAll({ domain });
|
|
all.push(...cookies);
|
|
} catch (e) {
|
|
console.warn(`cookies.getAll failed for ${domain}:`, e);
|
|
}
|
|
}
|
|
return deduplicateCookies(all);
|
|
}
|
|
|
|
function deduplicateCookies(cookies) {
|
|
const seen = new Map();
|
|
for (const c of cookies) {
|
|
const key = `${c.name}|${c.domain}|${c.path}`;
|
|
if (!seen.has(key)) {
|
|
seen.set(key, c);
|
|
} else {
|
|
const existing = seen.get(key);
|
|
if (c.expirationDate && (!existing.expirationDate || c.expirationDate > existing.expirationDate)) {
|
|
seen.set(key, c);
|
|
}
|
|
}
|
|
}
|
|
return Array.from(seen.values());
|
|
}
|
|
|
|
function toNetscapeFormat(cookies) {
|
|
const lines = ['# Netscape HTTP Cookie File'];
|
|
for (const c of cookies) {
|
|
let domain = c.domain.replace(/^\.?www\./, '.');
|
|
if (!domain.startsWith('.')) domain = '.' + domain;
|
|
const secure = c.secure ? 'TRUE' : 'FALSE';
|
|
const expiration = c.expirationDate ? Math.floor(c.expirationDate) : 0;
|
|
lines.push([domain, 'TRUE', c.path || '/', secure, String(expiration), c.name, c.value].join('\t'));
|
|
}
|
|
return lines.join('\n');
|
|
}
|
|
|
|
async function getCookieCount(platformKey) {
|
|
try {
|
|
return (await extractCookiesForPlatform(platformKey)).length;
|
|
} catch {
|
|
return 0;
|
|
}
|
|
}
|