pastebin link formatter

Turns URL patterns in pastebin pages into actual links. (see code for examples)

  1. // ==UserScript==
  2. // @id pastebin-linkize
  3. // @name pastebin link formatter
  4. // @version 1.0
  5. // @namespace hax
  6. // @author
  7. // @description Turns URL patterns in pastebin pages into actual links. (see code for examples)
  8. // @include http://pastebin.com/*
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. /*
  13. syntax: <contents> link (on one line)
  14. => <a href="link">contents</a>
  15.  
  16. ommitting [] will make
  17. => <a href="link">link</a>
  18.  
  19. -<contents> link (to leave the text plain)
  20. => <contents> link
  21.  
  22. contents may not include right angle brackets or % by itself.
  23. (is unescaped though so %3e will become > and %25 will become %)
  24. (and %51 will become Q if you don't want to type Q for some reason)
  25.  
  26. examples:
  27.  
  28. ---<reddit>https://reddit.com/
  29. => --<reddit> https://reddit.com/
  30. => -<reddit> https://reddit.com/
  31. => <reddit> https://reddit.com/
  32. -<hell> http://4chan.us/
  33. => <hell> http://4chan.us/
  34. --https://github.com/
  35. => -https://github.com/
  36. -<meetup> http://meetup.com
  37. => <meetup> http://meetup.com
  38. -<no site %3e this one> https://pastebin.com
  39. => <no site %3e this one> https://pastebin.com/
  40.  
  41. */
  42.  
  43. var urlpatt = new RegExp('(-)?(?:<([^>]*)>\\s*)?(https?://[^ ]+)','g');
  44.  
  45. function reformatOne(t) {
  46. var pieces = t.nodeValue.split(urlpatt);
  47. if(pieces.length == 1) {
  48. return 0;
  49. }
  50. var lastTextNode = null;
  51. function addtext(text) {
  52. console.log('add '+text);
  53. if(!text) return;
  54. if(lastTextNode==null) {
  55. lastTextNode = document.createTextNode(text);
  56. t.parentNode.insertBefore(lastTextNode,t);
  57. } else {
  58. lastTextNode.nodeValue += text;
  59. }
  60. }
  61. var islink = false;
  62. var i;
  63. console.log('pieces');
  64. console.log(t.nodeValue);
  65. console.log(pieces);
  66. for(i=0; i < pieces.length; ++i) {
  67. var piece = pieces[i];
  68. if(islink) {
  69. // a ()? capture returns undefined instead of omitting the capture
  70. var skip = piece;
  71. var contents = pieces[++i];
  72. var href = pieces[++i];
  73. if(skip) {
  74. if(contents == undefined)
  75. contents = href;
  76. else
  77. contents = '<' + contents + '> ' + href;
  78. addtext(contents);
  79. islink = false;
  80. continue;
  81. }
  82. if(contents == undefined) {
  83. contents = href;
  84. } else {
  85. contents = unescape(contents);
  86. }
  87. var link = document.createElement('a');
  88. link.setAttribute('href',href);
  89. link.appendChild(document.createTextNode(contents));
  90. t.parentNode.insertBefore(link,t);
  91. lastTextNode = null;
  92. islink = false;
  93. } else {
  94. addtext(piece);
  95. islink = true;
  96. }
  97. }
  98. t.parentNode.removeChild(t);
  99. return i + 1;
  100. }
  101.  
  102. function reformat(e) {
  103. if (e.nodeValue) {
  104. reformatOne(e);
  105. } else if(e.childNodes.length > 0) {
  106. // do NOT iterate and SAVE the next step
  107. // otherwise we descend into the links we created!
  108. var cur = e.childNodes[0];
  109. while(cur) {
  110. var next = cur.nextSibling;
  111. reformat(cur);
  112. cur = next;
  113. }
  114. }
  115. }
  116.  
  117. var div = document.getElementById('selectable');
  118. if(div)
  119. reformat(div);