Advertisement
Huntereb

Discord Text Formatting Userscript

Apr 21st, 2016
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Discord Text Formatting
  3. // @namespace    http://discordapp.com/
  4. // @version      2
  5. // @author       Huntereb
  6. // @match        *://discordapp.com/*
  7. // @require      http://code.jquery.com/jquery-latest.js
  8. // ==/UserScript==
  9.  
  10. //Initial variables! Can be applied in-app with the insert key!
  11.  
  12. var beforeText = "";
  13. var afterText = "";
  14. var reparseQuotes = 0;
  15. var paused = 0;
  16.  
  17. //Don't edit anything below here
  18.  
  19. function literalAddReturn (string) {
  20.     var string2 = string.split('%r');
  21.     var string3 = "";
  22.     for(i = 0; i < string2.length; i++) {
  23.         if (i !== string2.length - 1) string3 += string2[i] + "\r\n";
  24.         else return string3 += string2[i];
  25.     }
  26. }
  27.  
  28. function applyBinds() {
  29.     jQuery(textboxes).keypress(function (e) {
  30.         if (paused == 1) return;
  31.         if (e.shiftKey) return;
  32.         if (e.which == '13') {
  33.             for(i = 0; i < textboxes.length; i++) {
  34.                 var ourText = textboxes[i].value;
  35.                 var ourQuotes = '';
  36.                 var ptrn = /@.+?#[0-9]+/g;
  37.                 var match;
  38.                 while ( ( match = ptrn.exec(ourText) ) !== null ) {
  39.                     ourQuotes += match + ' ';
  40.                 }
  41.                 if(!/\d/g.test(ourQuotes) || reparseQuotes === 0) {
  42.                     ourQuotes = '';
  43.                 }
  44.                 textboxes[i].value = ourQuotes + beforeText + ourText + afterText;
  45.             }
  46.         }
  47.     });
  48.     jQuery(textboxes).keyup(function (e) {
  49.         if (e.keyCode == 19) {
  50.             if (paused == 1) paused = 0;
  51.             else paused = 1;
  52.         }
  53.         if (e.keyCode == 45) {
  54.             beforeText = literalAddReturn(prompt("Enter formatting before your text (%r for new line)", beforeText));
  55.             afterText = literalAddReturn(prompt("Enter formatting after your text (%r for new line)", afterText));
  56.             if(confirm("Parse quotes? (You don't need to unless you're using code tags)\r\nCancel = No")) reparseQuotes = 1;
  57.             else reparseQuotes = 0;
  58.         }
  59.         if (e.keyCode == 36) {
  60.             alert("Auto-Formatting Help:\r\nPAUSE (Pause key): Pause text formatting. Press it again to continue!\r\nINS (Insert key): Set formatting (resets to default on page refresh)\r\nYour current formatting: " + beforeText + "[Your text]" + afterText + "\r\nMade by Huntereb");
  61.         }
  62.     });
  63. }
  64.  
  65. function start() {
  66.     jQuery(textboxes).unbind();
  67.     textboxes = document.getElementsByTagName('textarea');
  68.     if (paused === 0) for(i = 0; i < textboxes.length; i++) textboxes[i].placeholder = 'Formatting in progress! Press "Home" for help!';
  69.     else for(i = 0; i < textboxes.length; i++) textboxes[i].placeholder = 'Formatting paused! Press \"Pause\" key to continue!';
  70.     applyBinds();
  71.     setTimeout(start, 1000);
  72. }
  73.  
  74. var textboxes;
  75.  
  76. start();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement