/* Shared by the helper's web pages: access key, API calls, small DOM helpers. */ "use strict"; const H = (() => { const qs = new URLSearchParams(location.search); function store(k, v) { try { if (v != null) localStorage.setItem(k, v); return localStorage.getItem(k) || ""; } catch (_) { return v || ""; } } const key = store("helperKey", qs.get("key")); const who = store("helperWho", qs.get("who")); let uid = store("helperUid", qs.get("uid")); if (!uid) uid = store("helperUid", (crypto.randomUUID ? crypto.randomUUID() : Date.now() + Math.random().toString(16).slice(2)).replace(/[^A-Za-z0-9-]/g, "")); if (qs.get("uid")) qs.delete("uid"); if (qs.get("key") || location.search.includes("uid=")) { qs.delete("key"); history.replaceState(null, "", location.pathname + (qs.toString() ? "?" + qs : "") + location.hash); } // Google sign-in mode (set by the server on every page): the browser's sign-in cookie is used, not a key. const google = (document.querySelector('meta[name="wl-auth"]') || {}).content === "google"; function signIn() { location.href = "/auth/login?next=" + encodeURIComponent(location.pathname + location.search); } async function api(path, body, raw) { const headers = google ? { "X-Helper-Request": "1" } : { "X-Helper-Key": key, "X-Helper-User": who, "X-Helper-Uid": uid, "X-Helper-Request": "1" }; const init = { method: body ? "POST" : "GET", headers }; if (body instanceof FormData) init.body = body; else if (body) { headers["Content-Type"] = "application/json"; init.body = JSON.stringify(body); } const r = await fetch(path, init); if (google && r.status === 401) { signIn(); throw new Error("Signing you in…"); } if (raw) return r; let data = {}; try { data = await r.json(); } catch (_) { /* empty */ } if (!r.ok) throw new Error(data.detail || "Error " + r.status); return data; } function el(tag, attrs, ...kids) { const e = document.createElement(tag); for (const [k, v] of Object.entries(attrs || {})) { if (k === "class") e.className = v; else if (k.startsWith("on")) e.addEventListener(k.slice(2), v); else if (v !== false && v != null) e.setAttribute(k, v === true ? "" : v); } for (const k of kids.flat()) if (k != null) e.append(k.nodeType ? k : document.createTextNode(String(k))); return e; } function needKey(container) { if (key || google) return false; container.replaceChildren(el("div", { class: "card" }, el("h2", null, "Access key needed"), el("p", { class: "muted" }, "Paste the key from the admin page (or open this page from the side panel)."), el("div", { class: "row" }, el("input", { id: "k", placeholder: "Access key", style: "flex:1" }), el("button", { class: "btn", onclick: () => { store("helperKey", document.getElementById("k").value.trim()); location.reload(); } }, "Save")))); return true; } function when(ts) { return ts ? new Date(ts * 1000).toLocaleString([], { dateStyle: "medium", timeStyle: "short" }) : ""; } async function signOut() { await fetch("/auth/logout", { method: "POST", headers: { "X-Helper-Request": "1" } }); signIn(); } return { api, el, key, who, needKey, when, google, signOut, setWho: (w) => store("helperWho", w) }; })();