From: Thorsten Sperber Date: Thu, 9 Nov 2017 19:43:53 +0000 (+0100) Subject: fix shit X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=2849aa5e8937b6d868162881ecf43f444f4f63b6;p=dfde%2Fnopaste-affenscript.git fix shit --- diff --git a/nopaste.user.js b/nopaste.user.js index 32b572d..dd18e66 100644 --- a/nopaste.user.js +++ b/nopaste.user.js @@ -1,96 +1,114 @@ -// ==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 0.7 -// ==/UserScript== - -_log = function (x) { - try { - console.log(x); - } catch (e) { - } -}; - -$("#format-buttons").append(''); - -function replaceIt(txtarea, newtxt, start, end) { - $(txtarea).val( - $(txtarea).val().substring(0, start) + - newtxt + - $(txtarea).val().substring(end) - ); -} - -function postSelection(data, textStatus, jqXHR) { - var message = $("#message"); - var start = message[0].selectionStart; - var end = message[0].selectionEnd; - - var pasteID = $(data).find('a[href^="./pastebin.php?mode=view"]'); - pasteID = pasteID.attr("href").substr(pasteID.attr("href").lastIndexOf("=") + 1); -// _log("pasteID: " + pasteID); - replaceIt(message[0], '[np]' + pasteID + '[/np]', start, end); - ready = true; -} - -function checkAll(textarea) { - var counter = 0; - var last_found = 0; - ready = true; - while (counter++ < 5 && (last_found >= 0)) { - var message = textarea.value; - - var start = message.indexOf("[code]", last_found + 7); - var end = message.indexOf("[/code]", last_found + 7); - - // without the code tags - var long_text = message.slice(start + 6, end); - var line_count = (long_text.match(/\n/g) || []).length; - - _log("[" + counter + "]", "found:", start, end, "beginning at", last_found); - var confirm_message = "found: " + (end - start) + " chars long with " + line_count + " lines, delete?"; - _log(confirm_message); - // cannot confirm due to greasemonkey/firefox bug - if (start >= 0 && (line_count > 20 || (end - start) > 20000)) { - textarea.setSelectionRange(start, end + 7); - sendToNoPaste(long_text, postSelection); - } else { - _log("not worth it"); - } - last_found = end; - // crude hack for ajax handling - while (ready === false) { - sleep(10); - } - } -} - -function sendToNoPaste(message, callback) { - var url = "//debianforum.de/forum/pastebin.php"; - var data = - { - snippet_title: $("#subject").val(), - snippet_desc: window.location.href, - snippet_highlight: "text", - fileupload: null, - snippet_text: message, - mode: "post", - submit: "Absenden" - }; - ready = false; - $.ajax( - { - type: 'POST', - url: url, - data: data, - dataType: "html", - success: callback - }); -} - -$("#nopaster").click(function () { - checkAll($("#message")[0]); -}); +// ==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(''); + +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]); +});