From: Thorsten Date: Sat, 16 Jan 2016 23:15:04 +0000 (+0100) Subject: some issues resolved X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=50ac41bbda3a50cf64eeca160b11a6de68963bcd;p=dfde%2Fnopaste-affenscript.git some issues resolved --- diff --git a/nopaste.user.js b/nopaste.user.js index ffd7468..32b572d 100644 --- a/nopaste.user.js +++ b/nopaste.user.js @@ -1,55 +1,96 @@ -// ==UserScript== +// ==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.3 +// @version 0.7 // ==/UserScript== +_log = function (x) { + try { + console.log(x); + } catch (e) { + } +}; -_log = console.log; - $("#format-buttons").append(''); -function replaceIt(txtarea, newtxt) { - $(txtarea).val( - $(txtarea).val().substring(0, txtarea.selectionStart)+ - newtxt+ - $(txtarea).val().substring(txtarea.selectionEnd) - ); +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 successPost(data, textStatus, jqXHR) - { - 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]'); + +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"); } - -$("#nopaster").click(function() - { + 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: getMessageText($("#message")[0]), + snippet_text: message, mode: "post", submit: "Absenden" - }; - - // _log(data); - + }; + ready = false; $.ajax( { - type: 'POST', - url: url, - data: data, - dataType: "html", - success: successPost - }) - }); + type: 'POST', + url: url, + data: data, + dataType: "html", + success: callback + }); +} + +$("#nopaster").click(function () { + checkAll($("#message")[0]); +});