Linkify ting

By adam 3 Last update May 15, 2007 — Installed 26,926 times. Daily Installs: 25, 31, 30, 32, 15, 41, 24, 25, 28, 33, 29, 33, 21, 37, 120, 68, 60, 24, 21, 32, 29, 39, 24, 18, 19, 25, 28, 25, 30, 33, 27, 18
// This script looks for any thing of the form
// [something not blank][a dot][something not blank], 
// then matches that which comes after the dot - and possibly before a slash - 
// with a list of know extensions, such as com, us, net etc. 
// If it has such an extension, it is turned into a link. 
// This way the script attempts to catch links like
// google.com and userscripts.org/tag/link.
// Because the script looks up extensions in a list, 
// you might want to alter the list (lovligeEndelser) to fit your needs. 
//
// The script is borrows a bit from the “Linkify Plus” script 
// (especially the parent not a | head | etc -  thing). And therefore the following 
//
//- - - 
//
// Loosely based on the Linkify script located at:
//   http://downloads.mozdev.org/greasemonkey/linkify.user.js
//
// Originally written by Anthony Lieuallen of http://www.arantius.com/
// Licensed for unlimited modification and redistribution as long as
// this notice is kept intact.
//
// If possible, please contact me regarding new features, bugfixes
// or changes that I could integrate into the existing code instead of
// creating a different script.  Thank you
//
//- - -
//
// for my sake you may do what you want with the script, 
// but you should keep the notice by Anthony Lieuallen
//
// adam
//
// ==UserScript==
// @name          Linkify ting
// @namespace     http://ergosum.frac.dk/user/
// @description   Turn plain text links into real clikable links. Attempts to catch links like google.com
// @include       *
// @exclude       http://*.google.*/*
// ==/UserScript==
try{
//dk|com|net|org|se|no|nl|us|uk|de|it|nu|edu|info

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;

var altText, tekst, muligtLink;

var ikkeTilladteTags = ['a', 'head', 'script', 'style', 'textarea', 'title', 'option', 'pre', 'code'];//tags, hvor det der står inden i ikke skal være links
var path = "//text()[not(parent::" + ikkeTilladteTags.join(" or parent::") +")]";

altText = document.evaluate(path, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

for(var i=0;i<altText.snapshotLength;i++){
	
	tekst = altText.snapshotItem(i);
	muligtLink = tekst.nodeValue;
	
	if(regex.test(muligtLink)){
		
		//til at holde det nye link, og teksten omkring det
		var span = document.createElement('span');
		//tekst.parentNode.replaceChild(span, tekst);
		//alert("parent:" + span.parentNode);
				
		var lastLastIndex = 0;
		regex.lastIndex = 0;
		for(myArray = null; myArray = regex.exec(muligtLink); ){
			//vores match gemmes
			var link = myArray[0];
			
			//alert("har fundet dette link: " + link);
			
			span.appendChild(document.createTextNode(muligtLink.substring(lastLastIndex, myArray.index))); //indsæt det der kommer før dette hit
						
			var href = link;
			
			//sætter http:// foran href, hvis der ikke er det
			var prefix = '';
			if(href.length > 7){
				prefix = href.substring(0,7);
			}
			if(prefix.toLowerCase() != 'http://'){
				href = 'http://' + href;
			}
			
			//skab vores link:
			var a = document.createElement('a');
			a.setAttribute('href', href); //giv det en href
			a.appendChild(document.createTextNode(link)); //linkteksten
			span.appendChild(a); //sætter ind i span
								
			lastLastIndex = regex.lastIndex;
			
		}
		
		span.appendChild(document.createTextNode(muligtLink.substring(lastLastIndex))); //insæt det der kommer efter sidste hit
		tekst.parentNode.replaceChild(span, tekst);

	}
	
		
}
} catch(e){alert(e);}