const COOKIE_SYNC_URL = "https://youtube.marha.de/cookies"; function toNetscape(cookies) { const lines = ["# Netscape HTTP Cookie File"]; for (const c of cookies) { const domain = c.httpOnly ? `#HttpOnly_${c.domain}` : c.domain; const includeSubdomain = c.domain.startsWith(".") ? "TRUE" : "FALSE"; const secure = c.secure ? "TRUE" : "FALSE"; const expiration = Math.floor(c.expirationDate || 0); lines.push([domain, includeSubdomain, c.path, secure, expiration, c.name, c.value].join("\t")); } return lines.join("\n") + "\n"; } async function syncCookies() { const when = new Date().toISOString(); try { const cookies = await browser.cookies.getAll({ domain: ".youtube.com" }); if (cookies.length === 0) { await browser.storage.local.set({ lastCookieSync: { when, ok: false, error: "keine YouTube-Cookies gefunden" } }); return; } const body = toNetscape(cookies); const res = await fetch(COOKIE_SYNC_URL, { method: "POST", headers: { "Content-Type": "text/plain" }, body, }); if (!res.ok) { await browser.storage.local.set({ lastCookieSync: { when, ok: false, error: `HTTP ${res.status}` } }); return; } await browser.storage.local.set({ lastCookieSync: { when, ok: true, count: cookies.length } }); } catch (e) { await browser.storage.local.set({ lastCookieSync: { when, ok: false, error: String(e) } }); } }