diff --git a/static/app.js b/static/app.js index 3923d6c..5f505b5 100644 --- a/static/app.js +++ b/static/app.js @@ -1,11 +1,17 @@ // app.js — ani-web frontend: search -> detail -> play, vanilla JS + hls.js +// BASE = this page's directory, so the app works at / or behind a +// prefixing reverse proxy (e.g. https://host/ani/) +const BASE = (() => { + const p = location.pathname; + return p.endsWith("/") ? p : p.slice(0, p.lastIndexOf("/") + 1); +})(); const main = document.getElementById("main"); let hls = null, detail = null, lang = "eng", seasonId = null; async function api(path) { - const r = await fetch(path); + const r = await fetch(BASE + path.replace(/^\//, "")); if (r.status === 401) { - location.href = "/login?next=" + encodeURIComponent(location.pathname + location.search); + location.href = BASE + "login?next=" + encodeURIComponent(location.pathname + location.search); throw new Error("not authenticated"); } if (!r.ok) { @@ -40,7 +46,7 @@ async function refreshHistory() { try { history = await api("/api/history"); } catch { /* home just shows empty */ } } function postHistory(entry) { - fetch("/api/history", {method: "POST", headers: {"Content-Type": "application/json"}, + fetch(BASE + "api/history", {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify(entry)}).catch(() => {}); } const fmtPos = s => { s = Math.floor(s || 0); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`; }; @@ -222,12 +228,12 @@ function showPlayOverlay(v) { document.getElementById("me").textContent = "hi, " + me.user; const lo = document.getElementById("logout"); lo.style.display = ""; lo.onclick = async () => { - await fetch("/logout", {method: "POST"}); location.href = "/login"; + await fetch(BASE + "logout", {method: "POST"}); location.href = BASE + "login"; }; const da = document.getElementById("delacct"); da.style.display = ""; da.onclick = async () => { if (!confirm("Delete your account and watch history?")) return; - await fetch("/account/delete", {method: "POST"}); location.href = "/login"; + await fetch(BASE + "account/delete", {method: "POST"}); location.href = BASE + "login"; }; } catch { /* not logged in: api() already redirected to /login */ } await refreshHistory(); diff --git a/static/login.html b/static/login.html index 1771c39..d199174 100644 --- a/static/login.html +++ b/static/login.html @@ -27,15 +27,21 @@ button { width:100%; margin-top:.4rem; padding:.55rem; border:0; border-radius:8