-// ==UserScript==\r
+// ==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.3\r
+// @version 0.7\r
// ==/UserScript==\r
\r
+_log = function (x) {\r
+ try {\r
+ console.log(x);\r
+ } catch (e) {\r
+ }\r
+};\r
\r
-_log = console.log;\r
- \r
$("#format-buttons").append('<input type="button" class="button2" name="addbbcodeNOPASTE" value="NoPaste" id="nopaster" title="Pasta">');\r
\r
-function replaceIt(txtarea, newtxt) {\r
- $(txtarea).val(\r
- $(txtarea).val().substring(0, txtarea.selectionStart)+\r
- newtxt+\r
- $(txtarea).val().substring(txtarea.selectionEnd)\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
-function getMessageText (ta) { return ta.value.substring(ta.selectionStart, ta.selectionEnd); }\r
-function successPost(data, textStatus, jqXHR)\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]');\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
- \r
-$("#nopaster").click(function()\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
+ {\r
snippet_title: $("#subject").val(),\r
snippet_desc: window.location.href,\r
snippet_highlight: "text",\r
fileupload: null,\r
- snippet_text: getMessageText($("#message")[0]),\r
+ snippet_text: message,\r
mode: "post",\r
submit: "Absenden"\r
- };\r
- \r
- // _log(data);\r
- \r
+ };\r
+ ready = false;\r
$.ajax(\r
{\r
- type: 'POST',\r
- url: url,\r
- data: data,\r
- dataType: "html",\r
- success: successPost\r
- })\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