Advertisement
nomcarver

Linkify

May 8th, 2014
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. // This script looks for any thing of the form
  2. // [something not blank][a dot][something not blank],
  3. // then matches that which comes after the dot - and possibly before a slash -
  4. // with a list of know extensions, such as com, us, net etc.
  5. // If it has such an extension, it is turned into a link.
  6. // This way the script attempts to catch links like
  7. // google.com and userscripts.org/tag/link.
  8. // Because the script looks up extensions in a list,
  9. // you might want to alter the list (lovligeEndelser) to fit your needs.
  10. //
  11. // The script is borrows a bit from the “Linkify Plus” script
  12. // (especially the parent not a | head | etc - thing). And therefore the following
  13. //
  14. //- - -
  15. //
  16. // Loosely based on the Linkify script located at:
  17. // http://downloads.mozdev.org/greasemo...inkify.user.js
  18. //
  19. // Originally written by Anthony Lieuallen of http://www.arantius.com/
  20. // Licensed for unlimited modification and redistribution as long as
  21. // this notice is kept intact.
  22. //
  23. // If possible, please contact me regarding new features, bugfixes
  24. // or changes that I could integrate into the existing code instead of
  25. // creating a different script. Thank you
  26. //
  27. //- - -
  28. //
  29. // for my sake you may do what you want with the script,
  30. // but you should keep the notice by Anthony Lieuallen
  31. //
  32. // adam
  33. //
  34. // ==UserScript==
  35. // @name Linkify ting
  36. // @namespace http://ergosum.frac.dk/user/
  37. // @description Turn plain text links into real clikable links. Attempts to catch links like google.com
  38. // @include *
  39. // @exclude http://*.google.*/*
  40. // ==/UserScript==
  41. try{
  42. //dk|com|net|org|se|no|nl|us|uk|de|it|nu|edu|info
  43.  
  44. var regex = /\b(http/\/|shttp/\/){0,1}(\w(\w|-)+\.)+(dk|com|net|org|se|no|nl|us|uk|de|it|nu|edu| info)(\/\w*)*(\.\w{2,4}){0,1}(\?\w*|#\w*|&\w*)*\b/gi;
  45.  
  46. var altText, tekst, muligtLink;
  47.  
  48. var ikkeTilladteTags = ['a', 'head', 'script', 'style', 'textarea', 'title', 'option', 'pre', 'code'];//tags, hvor det der står inden i ikke skal være links
  49. var path = "//text()[not(parent::" + ikkeTilladteTags.join(" or parent::") +")]";
  50.  
  51. altText = document.evaluate(path, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  52.  
  53. for(var i=0;i<altText.snapshotLength;i++){
  54.  
  55. tekst = altText.snapshotItem(i);
  56. muligtLink = tekst.nodeValue;
  57.  
  58. if(regex.test(muligtLink)){
  59.  
  60. //til at holde det nye link, og teksten omkring det
  61. var span = document.createElement('span');
  62. //tekst.parentNode.replaceChild(span, tekst);
  63. //alert("parent:" + span.parentNode);
  64.  
  65. var lastLastIndex = 0;
  66. regex.lastIndex = 0;
  67. for(myArray = null; myArray = regex.exec(muligtLink); ){
  68. //vores match gemmes
  69. var link = myArray[0];
  70.  
  71. //alert("har fundet dette link: " + link);
  72.  
  73. span.appendChild(document.createTextNode(muligtLin k.substring(lastLastIndex, myArray.index))); //indsæt det der kommer før dette hit
  74.  
  75. var href = link;
  76.  
  77. //sætter http:// foran href, hvis der ikke er det
  78. var prefix = '';
  79. if(href.length > 7){
  80. prefix = href.substring(0,7);
  81. }
  82. if(prefix.toLowerCase() != 'http://'){
  83. href = 'http://' + href;
  84. }
  85.  
  86. //skab vores link:
  87. var a = document.createElement('a');
  88. a.setAttribute('href', href); //giv det en href
  89. a.appendChild(document.createTextNode(link)); //linkteksten
  90. span.appendChild(a); //sætter ind i span
  91.  
  92. lastLastIndex = regex.lastIndex;
  93.  
  94. }
  95.  
  96. span.appendChild(document.createTextNode(muligtLin k.substring(lastLastIndex))); //insæt det der kommer efter sidste hit
  97. tekst.parentNode.replaceChild(span, tekst);
  98.  
  99. }
  100.  
  101.  
  102. }
  103. } catch(e){alert(e);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement