"use strict"; const CSRF_HEADER = "X-CSRF-Token"; const CLASS_OK = "ok"; const CLASS_ERROR = "error"; const MSG_TESTING = "Teste …"; const MSG_NETWORK_ERROR = "Netzwerkfehler: "; const MSG_BAD_RESPONSE = "Unerwartete Antwort (HTTP "; function csrfToken() { const meta = document.querySelector('meta[name="csrf"]'); return meta ? meta.content : ""; } function showResult(target, kind, text) { target.classList.remove(CLASS_OK, CLASS_ERROR); if (kind) { target.classList.add(kind); } target.textContent = text; } async function runTest(button) { const form = button.closest("form"); const target = form.querySelector(".test-result"); button.disabled = true; showResult(target, null, MSG_TESTING); try { const response = await fetch(button.dataset.testUrl, { method: "POST", headers: { [CSRF_HEADER]: csrfToken() }, body: new FormData(form), credentials: "same-origin", }); let data = null; try { data = await response.json(); } catch (_) { data = null; } if (data && typeof data.ok === "boolean") { showResult(target, data.ok ? CLASS_OK : CLASS_ERROR, String(data.detail ?? "")); } else { showResult(target, CLASS_ERROR, MSG_BAD_RESPONSE + response.status + ")"); } } catch (error) { showResult(target, CLASS_ERROR, MSG_NETWORK_ERROR + error.message); } finally { button.disabled = false; } } document.addEventListener("click", (event) => { const button = event.target.closest("button[data-test-url]"); if (button) { event.preventDefault(); runTest(button); } }); document.addEventListener("submit", (event) => { const form = event.target; if (form.matches("form[data-confirm]") && !window.confirm(form.dataset.confirm)) { event.preventDefault(); } });