]> git.aero2k.de Git - dfde/nopaste-affenscript.git/commitdiff
fix shit
authorThorsten Sperber <thorsten@heimdall.asgard>
Thu, 9 Nov 2017 19:43:53 +0000 (20:43 +0100)
committerThorsten Sperber <thorsten@heimdall.asgard>
Thu, 9 Nov 2017 19:43:53 +0000 (20:43 +0100)
nopaste.user.js

index 32b572d48ccc22931b3502ac6832fba4f4532222..dd18e664bbf14b5f49b76c2016c80d495f8003e8 100644 (file)
-// ==UserScript==\r
-// @name          debianforum.de-nopaste-addition\r
-// @namespace     org.free.for.all\r
-// @require       http://code.jquery.com/jquery-latest.min.js\r
-// @include       /^https?://debianforum\.de/forum/posting\.php.*/\r
-// @author        Thorsten Sperber\r
-// @version       0.7\r
-// ==/UserScript==\r
-\r
-_log = function (x) {\r
-    try {\r
-        console.log(x);\r
-    } catch (e) {\r
-    }\r
-};\r
-\r
-$("#format-buttons").append('<input type="button" class="button2" name="addbbcodeNOPASTE" value="NoPaste" id="nopaster" title="Pasta">');\r
-\r
-function replaceIt(txtarea, newtxt, start, end) {\r
-    $(txtarea).val(\r
-        $(txtarea).val().substring(0, start) +\r
-        newtxt +\r
-        $(txtarea).val().substring(end)\r
-    );\r
-}\r
-\r
-function postSelection(data, textStatus, jqXHR) {\r
-    var message = $("#message");\r
-    var start = message[0].selectionStart;\r
-    var end = message[0].selectionEnd;\r
-\r
-    var pasteID = $(data).find('a[href^="./pastebin.php?mode=view"]');\r
-    pasteID = pasteID.attr("href").substr(pasteID.attr("href").lastIndexOf("=") + 1);\r
-//        _log("pasteID: " + pasteID);\r
-    replaceIt(message[0], '[np]' + pasteID + '[/np]', start, end);\r
-    ready = true;\r
-}\r
-\r
-function checkAll(textarea) {\r
-    var counter = 0;\r
-    var last_found = 0;\r
-    ready = true;\r
-    while (counter++ < 5 && (last_found >= 0)) {\r
-        var message = textarea.value;\r
-\r
-        var start = message.indexOf("[code]", last_found + 7);\r
-        var end = message.indexOf("[/code]", last_found + 7);\r
-\r
-        // without the code tags\r
-        var long_text = message.slice(start + 6, end);\r
-        var line_count = (long_text.match(/\n/g) || []).length;\r
-\r
-        _log("[" + counter + "]", "found:", start, end, "beginning at", last_found);\r
-        var confirm_message = "found: " + (end - start) + " chars long with " + line_count + " lines, delete?";\r
-        _log(confirm_message);\r
-        // cannot confirm due to greasemonkey/firefox bug\r
-        if (start >= 0 && (line_count > 20 || (end - start) > 20000)) {\r
-            textarea.setSelectionRange(start, end + 7);\r
-            sendToNoPaste(long_text, postSelection);\r
-        } else {\r
-            _log("not worth it");\r
-        }\r
-        last_found = end;\r
-        // crude hack for ajax handling\r
-        while (ready === false) {\r
-            sleep(10);\r
-        }\r
-    }\r
-}\r
-\r
-function sendToNoPaste(message, callback) {\r
-    var url = "//debianforum.de/forum/pastebin.php";\r
-    var data =\r
-    {\r
-        snippet_title: $("#subject").val(),\r
-        snippet_desc: window.location.href,\r
-        snippet_highlight: "text",\r
-        fileupload: null,\r
-        snippet_text: message,\r
-        mode: "post",\r
-        submit: "Absenden"\r
-    };\r
-    ready = false;\r
-    $.ajax(\r
-        {\r
-            type: 'POST',\r
-            url: url,\r
-            data: data,\r
-            dataType: "html",\r
-            success: callback\r
-        });\r
-}\r
-\r
-$("#nopaster").click(function () {\r
-    checkAll($("#message")[0]);\r
-});\r
+// ==UserScript==
+// @name          debianforum.de-nopaste-addition
+// @namespace     org.free.for.all
+// @require       http://code.jquery.com/jquery-latest.min.js
+// @include       /^https?://debianforum\.de/forum/posting\.php.*/
+// @author        Thorsten Sperber
+// @version       3.0
+// ==/UserScript==
+
+const MIN_LINES=20;
+const MIN_CHARACTERS=20000;
+
+function sleep(ms) {
+    var start = new Date().getTime(), expire = start + ms;
+    while (new Date().getTime() < expire) { }
+    return;
+}
+
+$("#format-buttons").append('<input type="button" class="button2" name="addbbcodeNOPASTE" value="NoPaste" id="nopaster" title="Pasta">');
+
+function replaceIt(txtarea, newtxt, start, end) {
+    $(txtarea).val(
+        $(txtarea).val().substring(0, start)+
+        newtxt+
+        $(txtarea).val().substring(end)
+    );
+}
+function getMessageText (ta) { return ta.value.substring(ta.selectionStart, ta.selectionEnd); }
+
+function postSelection(data, textStatus, jqXHR)
+{
+    var message = $("#message")[0];
+    var start = message.selectionStart;
+    var end = message.selectionEnd;
+
+    var pasteID = data.REFRESH_DATA.url.match(/[0-9]+$/)[0];
+    console.log("pasteID: " + pasteID);
+    replaceIt(message, '[np]' + pasteID + '[/np]', start, end);
+}
+
+function checkAll(textarea) {
+    var counter = 0;
+    var last_found = 0;
+
+    var message = textarea.value;
+
+    var start = message.indexOf("[code]");
+    var end = message.indexOf("[/code]");
+    if (start == -1) { return; }
+
+    // message excluding the code tags
+    var long_text = message.slice(start + 6, end);
+    var line_count = (long_text.match(/\n/g) || []).length;
+
+    console.log("[" + counter + "]", "found:", start, end, "beginning at", last_found);
+    var confirm_message = "found: " + (end-start) + " chars long with " + line_count + " lines, delete?";
+    console.log(confirm_message);
+    // var do_it = confirm(confirm_message);
+    // cannot confirm due to greasemonkey/firefox bug
+
+    if (line_count > MIN_LINES || (end-start) > MIN_CHARACTERS) {
+        textarea.setSelectionRange(start, end + 7);
+        console.log("send to nopaste");
+        sendToNoPaste(long_text, postSelection, end + 7);
+    } else {
+        console.log("not worth it");
+    }
+}
+
+function sendToNoPaste(message, callback) {
+    var url = "https://debianforum.de/forum/pastebin/";
+
+    // rip some secrets from the page first, otherwise we won't get anywhere
+    $.get(url).done(function(resp) {
+        var ctime = resp.match(/creation_time"\s+value="(.+)"/).pop();
+        var token = resp.match(/form_token"\s+value="(.+)"/).pop();
+
+        // I do not know what I am doing. Request fails (but status 200) if done immediately...
+        // Spam protection maybe?
+        sleep(1000);
+        var data =
+            {
+                snippet_title: $("#subject").val(),
+                // the nopaste service behaves strange if it finds the sid.. maybe due to length?
+                snippet_desc: window.location.href.replace(/&sid=.*/, ''),
+                snippet_highlight: "text",
+                pruning_months: -1,
+                fileupload: null,
+                snippet_text: message,
+                mode: "post",
+                creation_time: ctime,
+                form_token: token,
+                submit: "Absenden"
+            };
+        // console.log(url);
+        // console.log(data);
+        $.ajax(
+            {
+                type: 'POST',
+                url: url,
+                data: data,
+                dataType: "json",
+                success: function(data, textStatus, jqXHR) {
+                    callback(data);
+                    checkAll($("#message")[0]);
+                },
+                error: function(data, textStatus, jqXHR) { console.log("error while sending to nopaste"); console.log(data); console.log(textStatus); }
+            });
+    });
+}
+
+$("#nopaster").click(function() {
+    checkAll($("#message")[0]);
+});