// @match https://debianforum.de/forum/viewtopic.php*
// @author Thorsten Sperber
// @author JTH
-// @version 1.1
+// @version 1.2
// ==/UserScript==
const ARCHIVFORUMID = 35;
let mod_user_id;
function ellipsify(str, maxlen) {
- let ell = str.length > maxlen ? " […]" : "";
+ const ell = str.length > maxlen ? " […]" : "";
return str.substring(0, maxlen - ell.length) + ell;
}
function get_thread_id() {
- return document.querySelector('a[href*="t="').getAttribute('href').match(/t=([0-9]+)/)[1];
+ return new URL(document.head.querySelector("link[rel=canonical]").href).searchParams.get("t");
}
function get_forum_id() {
- return document.querySelector('a[href*="f="').getAttribute('href').match(/f=([0-9]+)/)[1];
+ return new URLSearchParams(document.querySelector('a[href*="f="]').search).get("f");
}
-// ##################### derp, should do this with less...
-
-function param(object) {
- var encodedString = '';
- for (var prop in object) {
- if (object.hasOwnProperty(prop)) {
- if (encodedString.length > 0) {
- encodedString += '&';
- }
- encodedString += encodeURI(prop + '=' + object[prop]);
- }
- }
- return encodedString;
+function redirect_to_new_topic(redirDoc) {
+ const links = redirDoc.querySelectorAll("#message a");
+ window.location = links[links.length-1].href;
}
-// ##################### /derp
-
-
function remove_post_handler(event) {
- let post = event.currentTarget.closest('.post');
- let post_id = post.getAttribute('id').slice(1);
- let username = post.querySelector(".author .username,.author .username-coloured").text;
- let thread_title = document.querySelector('.topic-title a').text;
- let content = ellipsify(post.querySelector(".content").innerText, 250);
- if (localStorage.getItem("deleteWithoutConfirmation") || window.confirm(`Folgenden Beitrag von „${username}“ im Thema „${ellipsify(thread_title, 100)}“ wirklich als Spam archivieren?\n\n„${content}“`)) {
+ const post = event.currentTarget.closest('.post');
+ const post_id = post.id.slice(1);
+ const username = post.querySelector(".author .username,.author .username-coloured").text;
+ const thread_title = document.querySelector('.topic-title a').text;
+ const content = ellipsify(post.querySelector(".content").innerText, 250);
+ if (localStorage.getItem("deleteWithoutConfirmation") ?? window.confirm(`Folgenden Beitrag von „${username}“ im Thema „${ellipsify(thread_title, 100)}“ wirklich als Spam archivieren?\n\n„${content}“`)) {
send_mcp_request_archival(post_id, get_thread_id(), thread_title);
}
}
-function send_mcp_request_confirmation(confirm_key, sess, sid, post_id, thread_id, thread_title) {
- var url = boardurl + "mcp.php?f=" + get_forum_id() + "&t=" + thread_id + "&i=main&mode=topic_view&action=split&start=0&confirm_key=" + confirm_key;
- var data = {
- "action": "split_all",
- "confirm": "Ja",
- "confirm_uid": mod_user_id,
- "f": "0",
- "i": "main",
- "icon": "1",
- "mode": "topic_view",
- "post_id_list[0]": post_id,
- "redirect": "/",
- "sess": sess,
- "sid": sid,
- "start": "0",
- "subject": thread_title.toLowerCase().includes("spam") ? thread_title : "[spam] " + thread_title,
- "t": thread_id,
- "to_forum_id": ARCHIVFORUMID
- };
- var req = new XMLHttpRequest();
- req.addEventListener("load", function() {
- // hide it
- document.querySelector('.post#p' + post_id).style.display = "none";
- });
- req.open('POST', url);
- req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- req.send(param(data));
+async function send_mcp_request_confirmation(post_id, confirmDoc) {
+ const form = confirmDoc.querySelector("form#confirm");
+ const data = new FormData(form);
+ const confirmInput = form.elements["confirm"];
+ data.set(confirmInput.name, confirmInput.value); // Press "Yes"
+ /* Have to use explicit getAttribute() below since there is an input with
+ * name="action" which would be accessed with `form.action` :-/
+ */
+ const resp = await fetch(form.getAttribute("action"),
+ { body: new URLSearchParams(data), method: "POST" });
+ if (resp.ok) {
+ if (localStorage.getItem("redirectToNewTopic") ?? true) {
+ const respTxt = await resp.text();
+ const respDoc = new DOMParser().parseFromString(respTxt, "text/html");
+ if (!respDoc.querySelector("parsererror")) {
+ redirect_to_new_topic(respDoc);
+ }
+ } else {
+ // hide the freshly archived post
+ document.querySelector('.post#p' + post_id).style.display = "none";
+ }
+ }
}
-function send_mcp_request_archival(post_id, thread_id, thread_title) {
- var req = new XMLHttpRequest();
- // confirm when page has loaded
- req.addEventListener("load", function() {
- var confirm_key = this.responseText.match(/confirm_key=([A-Z0-9]+)/)[1];
- var sess = this.responseText.match(/name="sess" value="([a-z0-9]+)"/)[1];
- var sid = this.responseText.match(/name="sid" value="([a-z0-9]+)"/)[1];
- send_mcp_request_confirmation(confirm_key, sess, sid, post_id, thread_id, thread_title);
- });
-
- var url = boardurl + "mcp.php?f=" + get_forum_id() + "&t=" + thread_id + "&i=main&mode=topic_view&action=split&start=0";
- var data = {
+async function send_mcp_request_archival(post_id, thread_id, thread_title) {
+ const url = boardurl + "mcp.php?f=" + get_forum_id() + "&t=" + thread_id + "&i=main&mode=topic_view&action=split&start=0";
+ const data = new URLSearchParams({
"action": "split_all",
"mcp_topic_submit": "Absenden",
"post_id_list[]": post_id,
"to_topic_id": "0",
"st_old": "0",
"post_ids[0]": post_id
- };
- req.open('POST', url);
- req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- req.send(param(data));
+ });
+ const resp = await fetch(url, { body: data, method: "POST" });
+ if (resp.ok) {
+ const respTxt = await resp.text();
+ const respDoc = new DOMParser().parseFromString(respTxt, "text/html");
+ if (!respDoc.querySelector("parsererror")) {
+ send_mcp_request_confirmation(post_id, respDoc);
+ }
+ }
}
function add_buttons() {
- var postbuttons = document.querySelectorAll(".postbody .post-buttons");
+ const postbuttons = document.querySelectorAll(".postbody .post-buttons");
postbuttons.forEach(function(el) {
// well...
el.style.maxWidth = '41%';
- var del_post_btn_outer = document.createElement('li');
- var del_post_btn = document.createElement('a');
+ const del_post_btn_outer = document.createElement('li');
+ const del_post_btn = document.createElement('a');
del_post_btn.className = 'button button-icon-only';
del_post_btn.innerHTML = '<i class="icon fa-fire-extinguisher fa-fw" aria-hidden="true"></i><span class="sr-only">Abfall</span>';
}
function find_mod_user_id() {
- let profile_link = document.querySelector("#username_logged_in a[title^='Profil']");
+ const profile_link = document.querySelector("#username_logged_in a[title^='Profil']");
return Number.parseInt(new URLSearchParams(profile_link?.search).get("u"));
}