]> git.aero2k.de Git - dfde/quickmods.git/blobdiff - quickmod.user.js
major rewrite, use native js, adapt to new layout
[dfde/quickmods.git] / quickmod.user.js
index 120e46ab64927b72a0ebf86a30a87e7f77fc4198..1835fcc9c8b618a45eb22a74a199f7a2756d02a6 100644 (file)
 // ==UserScript==
 // @name          debianforum.de-quickmod-additions
 // @namespace     org.free.for.all
-// @require       http://code.jquery.com/jquery-latest.min.js
 // @include       https://debianforum.de/forum/viewtopic.php.*
 // @author        Thorsten Sperber
-// @version       0.4
+// @version       1.0
 // ==/UserScript==
 
-_log = function(x) {
-    try {
-        console.log(x);
-    } catch(e) {
-    }
-};
 ARCHIVFORUMID = 35;
 boardurl = "/forum/";
-user_id = "18865";
-
 
-function get_post_id(_this) {
-    return $(_this).closest('.post').attr('id').slice(1);
-}
+mod_user_id = "18865";
 
 function get_thread_id() {
-    return window.location.search.match(/t=([0-9]+)/)[1];
+    return document.querySelector('a[href*="t="').getAttribute('href').match(/t=([0-9]+)/)[1];
 }
+
 function get_forum_id() {
-    return window.location.search.match(/f=([0-9]+)/)[1];
-}
-function get_session_id() {
-    return $("a[href*='?sid']:last").attr("href").match(/sid=([a-z0-9]+)/)[1];
+    return document.querySelector('a[href*="f="').getAttribute('href').match(/f=([0-9]+)/)[1];
 }
 
-function removepost(event) {
+// ##################### derp, should do this with less...
 
-    _log(event);
-    var post_id = get_post_id(event.currentTarget);
-    var thread_id = get_thread_id();
-
-    var url = boardurl + "mcp.php?&f=" + get_forum_id() + "&t=" + thread_id + "&i=main&mode=topic_view&action=split&start=0";
-    var data = {
-        "action": "split_all",
-        "icon": "1",
-        "mcp_topic_submit": "Submit",
-        "post_id_list[]": post_id,
-        "posts_per_page": "10",
-        "sd": "a",
-        "sk": "t",
-        "st": "0",
-        "st_old": "0",
-        "subject": "[spam] " + $('.topic-title a').text(),
-        "to_forum_id": ARCHIVFORUMID,
-        "to_topic_id": "0",
-    };
-    $.ajax(
-        {
-        type: 'POST',
-        url: url,
-        data: data,
-        dataType: "html",
-        error: function(a, b, c) {
-            _log(a, b, c);
-        },
-        success: function(data, status) {
-            key = data.match(/confirm_key=([A-Z0-9]+)/)[1];
-            _removepost(post_id, key);
+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;
+}
+
+// ##################### /derp
 
 
+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);
 }
- function _removepost(post_id, confirm_key) {
-    var thread_id = get_thread_id();
-    url = boardurl + "mcp.php?action=split&f=" + get_forum_id() + "&i=main&mode=topic_view&start=0&t=" + thread_id + "&confirm_key=" + confirm_key;
-    data = {
+
+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": user_id,
+        "confirm_uid": mod_user_id,
         "f": "0",
         "i": "main",
         "icon": "1",
         "mode": "topic_view",
         "post_id_list[0]": post_id,
         "redirect": "/",
-        "sess": get_session_id(),
+        "sess": sess,
+        "sid": sid,
         "start": "0",
-        "subject": "[spam] " + $('#page-body').find('h2').text(),
+        "subject": "[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));
+}
 
-    $.ajax(
-        {
-        type: 'POST',
-        url: url,
-        data: data,
-        dataType: "html",
-        success: function() { $('#p' + post_id).fadeToggle(); }
-        }
-    );
+function send_mcp_request_archival(post_id, thread_id, thread_title) {
+    var req = new XMLHttpRequest();
+    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];
+        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 = {
+        "action": "split_all",
+        "mcp_topic_submit": "Absenden",
+        "post_id_list[]": post_id,
+        "posts_per_page": "15",
+        "st": "0",
+        "sk": "t",
+        "sd": "a",
+        "icon": "1",
+        "subject": "[spam] " + thread_title,
+        "to_forum_id": ARCHIVFORUMID,
+        "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));
 }
 
-$(".postbody .profile-icons .droppost").off('click');
-li = $("<li class='droppost'>");
-li.html('<a name="spampost">weg damit</a>');
-$(".postbody .profile-icons").prepend(li);
-$('li.droppost a').on('click', removepost).css({
-  "background": "linear-gradient(to bottom, #f7f7f7, #e3e3e3) repeat scroll 0 0 rgba(0, 0, 0, 0)",
-  "border": "1px solid #666",
-  "border-radius": "3px",
-  "color": "#d70751",
-  "font-weight": "bold",
-  "line-height": "11px",
-  "margin-top": "3px",
-  "padding": "1px 0 1px 2px"
+function add_buttons() {
+    var 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');
+        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>';
+
+        del_post_btn.addEventListener("click", remove_post_handler);
+
+        del_post_btn_outer.append(del_post_btn);
+        el.insertBefore(del_post_btn_outer, el.querySelector('.dropdown-container'));
+    });
 }
-);
+
+add_buttons();
\ No newline at end of file