From 12135e08359ca97c27aadad7a2df4ef4cef93426 Mon Sep 17 00:00:00 2001 From: JTH Date: Mon, 3 Oct 2022 18:34:26 +0200 Subject: [PATCH] Use instead of alert(), confirm(), prompt() Enables some actual concurrency of user interaction and execution of the mod actions. --- quickmod.user.js | 50 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/quickmod.user.js b/quickmod.user.js index 5f8c885..0111826 100644 --- a/quickmod.user.js +++ b/quickmod.user.js @@ -143,15 +143,17 @@ async function archiveThreadQuickmod() { } async function asyncAlert(message) { - window.alert(message); + return showDialog(message); } async function asyncConfirm(message) { - return window.confirm(message); + return showDialog(message, (dialog) => dialog.returnValue === "OK", true); } async function asyncPrompt(message, defaultValue) { - return window.prompt(message, defaultValue); + return showDialog(message, (dialog) => + dialog.returnValue === "OK" ? dialog.firstChild.elements["value"].value : null, + true, defaultValue); } async function banUser(username, reason) { @@ -365,6 +367,44 @@ async function send_mcp_request_archival(post, reason) { } } +async function showDialog(message, returnFunc = null, abortable = false, defaultValue = null) { + const dialog = document.body.appendChild(document.createElement("dialog")); + dialog.className = "quickmod-dialog"; + dialog.style.borderColor = "#D31141"; + dialog.style.maxWidth = "60em"; + + const form = dialog.appendChild(document.createElement("form")); + form.method = "dialog"; + + const p = form.appendChild(document.createElement("p")); + p.style.whiteSpace = "pre-line"; + p.textContent = message; + + if (defaultValue !== null) { + const inputP = form.appendChild(document.createElement("p")); + inputP.innerHTML = ``; + } + + const submitButtons = form.appendChild(document.createElement("fieldset")); + submitButtons.className = "submit-buttons"; + submitButtons.innerHTML = ' '; + if (abortable) { + const abortBtn = submitButtons.appendChild(document.createElement("input")); + abortBtn.className = "button2"; + abortBtn.type = "submit"; + abortBtn.value = "Abbrechen"; + } + + dialog.showModal(); + + return new Promise((resolve) => { + dialog.addEventListener("close", (event) => { + event.currentTarget.remove(); + resolve(returnFunc?.(event.currentTarget)); + }); + }); +} + function toAbsoluteURL(relativeOrAbsoluteURL) { return new URL(relativeOrAbsoluteURL, window.location); } @@ -399,6 +439,10 @@ function add_buttons() { archiveThreadLink.addEventListener("click", archiveThreadQuickmod); archiveThreadLink.innerText = "Thema als Spam archivieren"; archiveThreadLink.style.cursor = "pointer"; + + const stylesheet = document.head.appendChild(document.createElement("style")).sheet; + /* The pseudo element ::backdrop can only be styled through a rule. */ + stylesheet.insertRule(".quickmod-dialog::backdrop { background: #333C }"); } add_buttons(); -- 2.39.2