// ==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]); });