2 // @name debianforum.de-quickmod-additions
3 // @namespace org.free.for.all
4 // @include https://debianforum.de/forum/viewtopic.php*
5 // @match https://debianforum.de/forum/viewtopic.php*
6 // @author Thorsten Sperber
11 const ARCHIVFORUMID = 35;
13 async function banUser(username, reason) {
14 /* The URL to the ban form does not need any IDs or hidden inputs. We
19 [form, formData] = await openForm(toAbsoluteURL("./mcp.php?i=ban&mode=user"),
22 throw `Konnte Formular zum Sperren von Benutzern nicht öffnen: ${err}`;
25 formData.set("ban", username);
26 formData.set("banreason", reason);
27 //formData.set("bangivereason", reason);
29 await postForm(form, formData, "bansubmit", true);
31 throw `Konnte Benutzer nicht sperren: ${err}`;
35 async function closeReport(post) {
36 const reportLink = post.querySelector(".post-notice.reported a");
40 [form, formData] = await openForm(toAbsoluteURL(reportLink.href),
43 throw `Konnte Formular zum Schließen der Meldung nicht öffnen: ${err}`;
47 await postForm(form, formData, "action[close]", true);
49 throw `Konnte Meldung nicht schließen: ${err}`;
53 async function confirmAction(response) {
54 const [form, formData] = await openForm(response, "form#confirm");
55 await postForm(form, formData, "confirm");
58 function ellipsify(str, maxlen) {
59 const ell = str.length > maxlen ? " […]" : "";
60 return str.substring(0, maxlen - ell.length) + ell;
63 function isPostReported(post) {
64 return post.querySelector(".post-notice.reported a") !== null;
67 async function openForm(urlOrResponse, selector) {
68 const resp = urlOrResponse instanceof Response ? urlOrResponse :
69 await fetch(urlOrResponse);
71 throw `${resp.url}: ${resp.status}`;
74 const parser = new DOMParser();
75 const txt = await resp.text();
76 const doc = parser.parseFromString(txt, "text/html");
77 const form = doc.querySelector(selector);
78 return [form, new FormData(form)];
81 async function postForm(form, formData, submitName, requiresConfirmation = false) {
82 /* "Press" the right submit button. */
83 const submitBtn = form.elements[submitName];
84 formData.set(submitBtn.name, submitBtn.value);
86 /* Have to use explicit getAttribute() below since there is an input with
87 * name="action" which would be accessed with `form.action` :-/
89 const resp = await fetch(toAbsoluteURL(form.getAttribute("action")),
90 { body: new URLSearchParams(formData), method: "POST" });
92 throw `${resp.url}: ${resp.status}`;
95 if (requiresConfirmation) {
96 await confirmAction(resp);
100 async function remove_post_handler(event) {
101 const post = event.currentTarget.closest('.post');
102 const usernameElem = post.querySelector(".author .username,.author .username-coloured");
103 const username = usernameElem.textContent;
104 const thread_title = document.querySelector('.topic-title a').text;
105 const content = ellipsify(post.querySelector(".content").innerText, 250);
107 const splitReason = window.prompt(`Folgenden Beitrag von „${username}“ im Thema „${ellipsify(thread_title, 100)}“ wirklich als Spam archivieren?\n\n„${content}“\n\nGrund:`, "Spam");
108 if (splitReason === null) {
109 /* Don't do any of the other actions if splitting was cancelled. */
113 /* Prompting for a separate ban reason in case there is something more
114 * specific to note here.
116 const userStillExists = usernameElem.nodeName === "A";
117 const banReason = userStillExists &&
118 window.prompt(`Benutzer „${username}“ sperren?\n\nGrund:`, "Spam");
119 const shouldCloseReport = isPostReported(post) && window.confirm("Meldung zum Beitrag schließen?");
121 /* Initially, I wanted to use Promise.allSettled() below to trigger and wait
122 * for all actions in parallel. But that made at least one of them fail in
123 * most cases. Not sure, if there was a mistake in here somewhere (totally
124 * not impossible) or if phpBB just does not cope well with parallel mod
125 * actions (there are some session IDs and confirm_keys involved when a mod
126 * action is executed and confirmed).
128 * So essentially, we do not have any asynchronous execution here,
131 const errors = new Array();
133 await send_mcp_request_archival(post, splitReason);
140 await banUser(username, banReason);
144 } else if (!userStillExists) {
145 window.alert(`Benutzer „${username}“ wurde schon gelöscht und kann nicht mehr gesperrt werden.`);
148 if (shouldCloseReport) {
150 await closeReport(post);
156 for (const error of errors) {
158 window.alert(`ACHTUNG!\n\n${error}`);
161 if (errors.length === 0) {
162 updatePageAfterSplit(post);
166 async function send_mcp_request_archival(post, reason) {
167 const splitLink = document.querySelector("#quickmod .dropdown-contents a[href*='action=split']");
171 [form, formData] = await openForm(toAbsoluteURL(splitLink.href), "form#mcp");
173 throw `Konnte Formular zum Aufteilen des Themas nicht öffnen: ${err}`;
176 const post_id = post.id.slice(1);
177 const thread_title = (() => {
178 const prefix = `[${reason}]`;
179 let title = formData.get("subject");
180 if (reason && !title.toLowerCase().includes(prefix.toLowerCase())) {
181 title = `${prefix} ${title}`;
183 return title.slice(0, form.elements["subject"].maxLength);
185 formData.set("post_id_list[]", post_id);
186 formData.set("subject", thread_title);
187 formData.set("to_forum_id", ARCHIVFORUMID);
190 await postForm(form, formData, "mcp_topic_submit", true);
192 throw `Konnte Thema nicht aufteilen: ${err}`;
196 function toAbsoluteURL(relativeOrAbsoluteURL) {
197 return new URL(relativeOrAbsoluteURL, window.location);
200 function updatePageAfterSplit(post) {
201 if (document.querySelectorAll(".post").length > 1) {
202 post.parentNode.removeChild(post);
204 /* TODO: Make the location configurable, redirect to homepage, "Aktive
205 * Themen", "Neue Beiträge", or other?
207 window.location = "/";
211 function add_buttons() {
212 const del_post_btn_outer = document.createElement('li');
213 const del_post_btn = document.createElement('a');
214 del_post_btn.className = 'button button-icon-only';
215 del_post_btn.innerHTML = '<i class="icon fa-fire-extinguisher fa-fw" aria-hidden="true"></i><span class="sr-only">Abfall</span>';
216 del_post_btn.addEventListener("click", remove_post_handler);
217 del_post_btn.title = "Als Spam archivieren";
218 del_post_btn_outer.append(del_post_btn);
220 for (const postButtons of document.querySelectorAll(".post-buttons")) {
221 const del_post_btn_outer_clone = del_post_btn_outer.cloneNode(true);
222 del_post_btn_outer_clone.addEventListener("click", remove_post_handler);
223 postButtons.appendChild(del_post_btn_outer_clone);