NoPaste

nopaste button userscript (v3.0)

von TRex
SNIPPET_DESC:
https://wiki.debianforum.de/Userscripts
SNIPPET_CREATION_TIME:
27.03.2023 17:06:24
SNIPPET_PRUNE_TIME:
Unendlich

SNIPPET_TEXT:
  1. // ==UserScript==
  2. // @name          debianforum.de-nopaste-addition
  3. // @namespace     org.free.for.all
  4. // @require       http://code.jquery.com/jquery-latest.min.js
  5. // @include       /^https?://debianforum\.de/forum/posting\.php.*/
  6. // @author        Thorsten Sperber
  7. // @version       3.0
  8. // ==/UserScript==
  9.  
  10. const MIN_LINES=20;
  11. const MIN_CHARACTERS=20000;
  12.  
  13. function sleep(ms) {
  14.     var start = new Date().getTime(), expire = start + ms;
  15.     while (new Date().getTime() < expire) { }
  16.     return;
  17. }
  18.  
  19. $("#format-buttons").append('<input type="button" class="button2" name="addbbcodeNOPASTE" value="NoPaste" id="nopaster" title="Pasta">');
  20.  
  21. function replaceIt(txtarea, newtxt, start, end) {
  22.     $(txtarea).val(
  23.         $(txtarea).val().substring(0, start)+
  24.         newtxt+
  25.         $(txtarea).val().substring(end)
  26.     );
  27. }
  28. function getMessageText (ta) { return ta.value.substring(ta.selectionStart, ta.selectionEnd); }
  29.  
  30. function postSelection(data, textStatus, jqXHR)
  31. {
  32.     var message = $("#message")[0];
  33.     var start = message.selectionStart;
  34.     var end = message.selectionEnd;
  35.  
  36.     var pasteID = data.REFRESH_DATA.url.match(/[0-9]+$/)[0];
  37.     console.log("pasteID: " + pasteID);
  38.     replaceIt(message, '[np]' + pasteID + '[/np]', start, end);
  39. }
  40.  
  41. function checkAll(textarea) {
  42.     var counter = 0;
  43.     var last_found = 0;
  44.  
  45.     var message = textarea.value;
  46.  
  47.     var start = message.indexOf("[code]");
  48.     var end = message.indexOf("[/code]");
  49.     if (start == -1) { return; }
  50.  
  51.     // message excluding the code tags
  52.     var long_text = message.slice(start + 6, end);
  53.     var line_count = (long_text.match(/\n/g) || []).length;
  54.  
  55.     console.log("[" + counter + "]", "found:", start, end, "beginning at", last_found);
  56.     var confirm_message = "found: " + (end-start) + " chars long with " + line_count + " lines, delete?";
  57.     console.log(confirm_message);
  58.     // var do_it = confirm(confirm_message);
  59.     // cannot confirm due to greasemonkey/firefox bug
  60.  
  61.     if (line_count > MIN_LINES || (end-start) > MIN_CHARACTERS) {
  62.         textarea.setSelectionRange(start, end + 7);
  63.         console.log("send to nopaste");
  64.         sendToNoPaste(long_text, postSelection, end + 7);
  65.     } else {
  66.         console.log("not worth it");
  67.     }
  68. }
  69.  
  70. function sendToNoPaste(message, callback) {
  71.     var url = "https://debianforum.de/forum/pastebin/";
  72.  
  73.     // rip some secrets from the page first, otherwise we won't get anywhere
  74.     $.get(url).done(function(resp) {
  75.         var ctime = resp.match(/creation_time"\s+value="(.+)"/).pop();
  76.         var token = resp.match(/form_token"\s+value="(.+)"/).pop();
  77.  
  78.         // I do not know what I am doing. Request fails (but status 200) if done immediately...
  79.         // Spam protection maybe?
  80.         sleep(1000);
  81.         var data =
  82.             {
  83.                 snippet_title: $("#subject").val(),
  84.                 // the nopaste service behaves strange if it finds the sid.. maybe due to length?
  85.                 snippet_desc: window.location.href.replace(/&sid=.*/, ''),
  86.                 snippet_highlight: "text",
  87.                 pruning_months: -1,
  88.                 fileupload: null,
  89.                 snippet_text: message,
  90.                 mode: "post",
  91.                 creation_time: ctime,
  92.                 form_token: token,
  93.                 submit: "Absenden"
  94.             };
  95.         // console.log(url);
  96.         // console.log(data);
  97.         $.ajax(
  98.             {
  99.                 type: 'POST',
  100.                 url: url,
  101.                 data: data,
  102.                 dataType: "json",
  103.                 success: function(data, textStatus, jqXHR) {
  104.                     callback(data);
  105.                     checkAll($("#message")[0]);
  106.                 },
  107.                 error: function(data, textStatus, jqXHR) { console.log("error while sending to nopaste"); console.log(data); console.log(textStatus); }
  108.             });
  109.     });
  110. }
  111.  
  112. $("#nopaster").click(function() {
  113.     checkAll($("#message")[0]);
  114. });

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. PASTEBIN_DOWNLOAD_SNIPPET_EXPLAIN