From: Thorsten Date: Mon, 29 Aug 2022 17:57:42 +0000 (+0200) Subject: add confirmation, fix [spam][spam], read mod userid from profile link X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=b294bae5a0b4c4ad7553abadde4e85d06b1d5219;p=dfde%2Fquickmods.git add confirmation, fix [spam][spam], read mod userid from profile link --- diff --git a/quickmod.user.js b/quickmod.user.js index 1835fcc..7bcc14d 100644 --- a/quickmod.user.js +++ b/quickmod.user.js @@ -1,15 +1,22 @@ // ==UserScript== // @name debianforum.de-quickmod-additions // @namespace org.free.for.all -// @include https://debianforum.de/forum/viewtopic.php.* +// @include https://debianforum.de/forum/viewtopic.php* +// @match https://debianforum.de/forum/viewtopic.php* // @author Thorsten Sperber -// @version 1.0 +// @author JTH +// @version 1.1 // ==/UserScript== -ARCHIVFORUMID = 35; -boardurl = "/forum/"; +const ARCHIVFORUMID = 35; +const boardurl = "/forum/"; -mod_user_id = "18865"; +let mod_user_id; + +function ellipsify(str, maxlen) { + let 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]; @@ -38,9 +45,14 @@ function param(object) { function remove_post_handler(event) { - var post_id = event.currentTarget.closest('.post').getAttribute('id').slice(1); - var thread_title = document.querySelector('.topic-title a').text; - send_mcp_request_archival(post_id, get_thread_id(), thread_title); + 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}“`)) { + 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) { @@ -58,7 +70,7 @@ function send_mcp_request_confirmation(confirm_key, sess, sid, post_id, thread_i "sess": sess, "sid": sid, "start": "0", - "subject": "[spam] " + thread_title, + "subject": thread_title.toLowerCase().includes("spam") ? thread_title : "[spam] " + thread_title, "t": thread_id, "to_forum_id": ARCHIVFORUMID }; @@ -74,9 +86,8 @@ function send_mcp_request_confirmation(confirm_key, sess, sid, post_id, thread_i function send_mcp_request_archival(post_id, thread_id, thread_title) { var req = new XMLHttpRequest(); + // confirm when page has loaded req.addEventListener("load", function() { - // extract the confirmation ID, proceed - // console.log(this.responseText); 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]; @@ -93,7 +104,7 @@ function send_mcp_request_archival(post_id, thread_id, thread_title) { "sk": "t", "sd": "a", "icon": "1", - "subject": "[spam] " + thread_title, + "subject": thread_title.toLowerCase().includes("spam") ? thread_title : "[spam] " + thread_title, "to_forum_id": ARCHIVFORUMID, "to_topic_id": "0", "st_old": "0", @@ -122,4 +133,14 @@ function add_buttons() { }); } -add_buttons(); \ No newline at end of file +function find_mod_user_id() { + let profile_link = document.querySelector("#username_logged_in a[title^='Profil']"); + return Number.parseInt(new URLSearchParams(profile_link?.search).get("u")); +} + +mod_user_id ??= find_mod_user_id(); +if (Number.isInteger(mod_user_id)) { + add_buttons(); +} else { + console.error("mod_user_id unset and could not find user ID"); +} \ No newline at end of file