From d41b0b7a437aaebd9dcf244d455dbf7b71b4b5d7 Mon Sep 17 00:00:00 2001 From: JTH Date: Fri, 30 Sep 2022 11:24:03 +0200 Subject: [PATCH] Optionally handle confirmation directly in postForm() --- quickmod.user.js | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/quickmod.user.js b/quickmod.user.js index 16891e2..32e3984 100644 --- a/quickmod.user.js +++ b/quickmod.user.js @@ -25,15 +25,10 @@ async function banUser(username, reason) { formData.set("ban", username); formData.set("banreason", reason); //formData.set("bangivereason", reason); - const resp = await postForm(form, formData, "bansubmit"); - if (!resp.ok) { - throw "Konnte Sperrung des Benutzers nicht anfragen."; - } - try { - await confirmAction(resp); + await postForm(form, formData, "bansubmit", true); } catch (err) { - throw `Konnte Sperrung des Benutzers nicht bestätigen: ${err}`; + throw `Konnte Benutzer nicht sperren: ${err}`; } } @@ -48,26 +43,16 @@ async function closeReport(post) { throw `Konnte Formular zum Schließen der Meldung nicht öffnen: ${err}`; } - const resp = await postForm(form, formData, "action[close]"); - if (!resp.ok) { - throw "Konnte Schließen der Meldung nicht anfragen."; - } - try { - await confirmAction(resp); + await postForm(form, formData, "action[close]", true); } catch (err) { - throw `Konnte Schließen der Meldung nicht bestätigen: ${err}`; + throw `Konnte Meldung nicht schließen: ${err}`; } } async function confirmAction(response) { const [form, formData] = await openForm(response, "form#confirm"); - - const resp = await postForm(form, formData, "confirm"); - if (!resp.ok) { - throw `${resp.url}: ${resp.status}`; - } - return resp.text(); + await postForm(form, formData, "confirm"); } function ellipsify(str, maxlen) { @@ -93,7 +78,7 @@ async function openForm(urlOrResponse, selector) { return [form, new FormData(form)]; } -function postForm(form, formData, submitName) { +async function postForm(form, formData, submitName, requiresConfirmation = false) { /* "Press" the right submit button. */ const submitBtn = form.elements[submitName]; formData.set(submitBtn.name, submitBtn.value); @@ -101,8 +86,15 @@ function postForm(form, formData, submitName) { /* Have to use explicit getAttribute() below since there is an input with * name="action" which would be accessed with `form.action` :-/ */ - return fetch(toAbsoluteURL(form.getAttribute("action")), + const resp = await fetch(toAbsoluteURL(form.getAttribute("action")), { body: new URLSearchParams(formData), method: "POST" }); + if (!resp.ok) { + throw `${resp.url}: ${resp.status}`; + } + + if (requiresConfirmation) { + await confirmAction(resp); + } } async function remove_post_handler(event) { @@ -194,15 +186,10 @@ async function send_mcp_request_archival(post, reason) { formData.set("subject", thread_title); formData.set("to_forum_id", ARCHIVFORUMID); - const resp = await postForm(form, formData, "mcp_topic_submit"); - if (!resp.ok) { - throw "Konnte Aufteilen des Themas nicht anfragen."; - } - try { - await confirmAction(resp); + await postForm(form, formData, "mcp_topic_submit", true); } catch (err) { - throw `Konnte Aufteilen des Themas nicht bestätigen: ${err}`; + throw `Konnte Thema nicht aufteilen: ${err}`; } } -- 2.39.2