const SERVER_URL = "https://youtube.marha.de/profiles"; const container = document.getElementById("profiles"); const statusEl = document.getElementById("cookieStatus"); const syncBtn = document.getElementById("syncBtn"); async function loadProfiles() { try { const res = await fetch(SERVER_URL); const profiles = await res.json(); const stored = await browser.storage.local.get("profileId"); const selectedId = stored.profileId || null; for (const profile of profiles) { const label = document.createElement("label"); const radio = document.createElement("input"); radio.type = "radio"; radio.name = "profile"; radio.value = profile.id; radio.checked = profile.id === selectedId; radio.addEventListener("change", () => { browser.storage.local.set({ profileId: profile.id }); }); label.appendChild(radio); label.appendChild(document.createTextNode(" " + profile.name)); container.appendChild(label); } } catch { container.innerHTML = 'Server nicht erreichbar'; } } function formatAgo(iso) { const diff = Date.now() - new Date(iso).getTime(); const mins = Math.floor(diff / 60000); if (mins < 1) return "gerade eben"; if (mins < 60) return `vor ${mins} min`; const hrs = Math.floor(mins / 60); if (hrs < 24) return `vor ${hrs} h`; const days = Math.floor(hrs / 24); return `vor ${days} Tagen`; } async function refreshStatus() { const { lastCookieSync } = await browser.storage.local.get("lastCookieSync"); if (!lastCookieSync) { statusEl.className = "status"; statusEl.textContent = "noch nicht synchronisiert"; return; } if (lastCookieSync.ok) { statusEl.className = "status ok"; statusEl.textContent = `OK (${lastCookieSync.count} Cookies, ${formatAgo(lastCookieSync.when)})`; } else { statusEl.className = "status fail"; statusEl.textContent = `Fehler: ${lastCookieSync.error}`; } } syncBtn.addEventListener("click", async () => { syncBtn.disabled = true; syncBtn.textContent = "Syncing..."; await browser.runtime.sendMessage({ type: "sync-cookies" }); await refreshStatus(); syncBtn.disabled = false; syncBtn.textContent = "Jetzt synchronisieren"; }); loadProfiles(); refreshStatus();