From 70ba43d168adcd4537450f585f534393a5f9f9f8 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Fri, 1 Mar 2024 21:55:46 +0100 Subject: [PATCH] some improvements sitting here for months --- nopaste.user.js | 64 +++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 23 deletions(-) diff --git a/nopaste.user.js b/nopaste.user.js index dd18e66..7d35fac 100644 --- a/nopaste.user.js +++ b/nopaste.user.js @@ -4,21 +4,30 @@ // @require http://code.jquery.com/jquery-latest.min.js // @include /^https?://debianforum\.de/forum/posting\.php.*/ // @author Thorsten Sperber -// @version 3.0 +// @version 3.1 // ==/UserScript== +const MAX_CODE_BLOCKS = 10; const MIN_LINES=20; const MIN_CHARACTERS=20000; +let counter = 0; +let last_found = 0; + function sleep(ms) { - var start = new Date().getTime(), expire = start + ms; + const start = new Date().getTime(), expire = start + ms; while (new Date().getTime() < expire) { } - return; } +// TODO replace jquery $("#format-buttons").append(''); +function markText(txtarea, start, end) { + txtarea.setSelectionRange(start, end); +} + function replaceIt(txtarea, newtxt, start, end) { + // TODO replace jquery $(txtarea).val( $(txtarea).val().substring(0, start)+ newtxt+ @@ -29,56 +38,63 @@ function getMessageText (ta) { return ta.value.substring(ta.selectionStart, ta.s function postSelection(data, textStatus, jqXHR) { - var message = $("#message")[0]; - var start = message.selectionStart; - var end = message.selectionEnd; + const message = $("#message")[0]; + const start = message.selectionStart; + const end = message.selectionEnd; - var pasteID = data.REFRESH_DATA.url.match(/[0-9]+$/)[0]; + const 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; + const message = textarea.value; - var start = message.indexOf("[code]"); - var end = message.indexOf("[/code]"); - if (start == -1) { return; } + const start = message.indexOf("[code]", last_found); + const end = message.indexOf("[/code]", last_found); + markText(textarea, start, end); + 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; + const long_text = message.slice(start + 6, end); + const 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?"; + const 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 + counter++; if (line_count > MIN_LINES || (end-start) > MIN_CHARACTERS) { textarea.setSelectionRange(start, end + 7); + last_found = start; console.log("send to nopaste"); - sendToNoPaste(long_text, postSelection, end + 7); + sendToNoPaste(long_text, function(data, textStatus, jqXHR) { + postSelection(data, textStatus, jqXHR); + checkAll(textarea); + }, end + 7); } else { console.log("not worth it"); + last_found = end+7; + if (counter > MAX_CODE_BLOCKS) { console.log("limit reached"); return; } + checkAll(textarea); } } function sendToNoPaste(message, callback) { - var url = "https://debianforum.de/forum/pastebin/"; + const url = "https://debianforum.de/forum/pastebin/"; // rip some secrets from the page first, otherwise we won't get anywhere + // TODO replace jquery $.get(url).done(function(resp) { - var ctime = resp.match(/creation_time"\s+value="(.+)"/).pop(); - var token = resp.match(/form_token"\s+value="(.+)"/).pop(); + const ctime = resp.match(/creation_time"\s+value="(.+)"/).pop(); + const 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 = + const data = { snippet_title: $("#subject").val(), // the nopaste service behaves strange if it finds the sid.. maybe due to length? @@ -102,13 +118,15 @@ function sendToNoPaste(message, callback) { 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); } }); }); } +// TODO replace jquery $("#nopaster").click(function() { + counter = 0; + last_found = 0; checkAll($("#message")[0]); }); -- 2.39.2