Advertisement
Guest User

Paste a URL into a textarea as a [url=]<selected text>[/url]

a guest
Apr 17th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.11 KB | None | 0 0
  1. <textarea id="txt1" cols="80" rows="5">The quick brown fox jumps over the lazy dog</textarea>
  2.  
  3. <script>
  4. function getTextAreaSelection(textarea) {
  5.     var start = textarea.selectionStart, end = textarea.selectionEnd;
  6.     return {
  7.         start: start,
  8.         end: end,
  9.         length: end - start,
  10.         text: textarea.value.slice(start, end)
  11.     };
  12. }
  13.  
  14. function detectPaste(textarea, callback) {
  15.     textarea.onpaste = function() {
  16.         var sel = getTextAreaSelection(textarea);
  17.         var initialLength = textarea.value.length;
  18.  
  19.         var textBefore = textarea.value.slice(0, sel.start);
  20.         var textAfter = textarea.value.slice(sel.end, textarea.value.length);
  21.  
  22.         window.setTimeout(function() {
  23.             var val = textarea.value;
  24.             var pastedTextLength = val.length - (initialLength - sel.length);
  25.             var end = sel.start + pastedTextLength;
  26.             callback({
  27.                 start: sel.start,
  28.                 end: end,
  29.                 length: pastedTextLength,
  30.                 text: val.slice(sel.start, end),
  31.                 selection: sel,
  32.                 before: textBefore,
  33.                 after: textAfter
  34.             });
  35.         }, 1);
  36.     };
  37. }
  38.  
  39. function validateURL(textval) {
  40.   var urlregex = new RegExp(
  41.       "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
  42.   return urlregex.test(textval);
  43. }
  44.  
  45. var textarea = document.getElementById("txt1");
  46.  
  47. detectPaste(textarea, function(pasteInfo) {
  48.     // pasteInfo also has properties for the start and end character
  49.     // index and length of the pasted text
  50.     if (pasteInfo.text != null && pasteInfo.text.length > 0 && validateURL(pasteInfo.text)) {
  51.      // Handle trailing spaces in selection (happens when the word was selected with double-click)
  52.      while (pasteInfo.selection.text.length > 0 && pasteInfo.selection.text.charAt(pasteInfo.selection.text.length-1) == ' ') {
  53.         pasteInfo.selection.text = pasteInfo.selection.text.substring(0, pasteInfo.selection.text.length-1);
  54.          pasteInfo.after = " " + pasteInfo.after;
  55.       }
  56.       // Replace URL with [link] tag
  57.       textarea.value = pasteInfo.before + "[url=" + pasteInfo.text + "]" + pasteInfo.selection.text + "[/url]" + pasteInfo.after;
  58.       var newPosition = textarea.value.length - pasteInfo.after.length
  59.       // if no text was selected for title, position cursor inside the [url] tag
  60.       if (pasteInfo.selection.text.length == 0) {
  61.          newPosition -= 6;
  62.       }
  63.       textarea.setSelectionRange(newPosition, newPosition);
  64.     }
  65. });
  66. </script>
  67.  
  68. <br>
  69. To test this feature, select any part of the text, and paste (Ctrl-V, or right-click + Paste) a valid URL.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement