Linkify ting (exclude emails)

By JoeSimmons Last update Jun 2, 2010 — Installed 10,851 times.

There are 19 previous versions of this script.

// ==UserScript==
// @name          Linkify ting (exclude emails)
// @namespace     http://ergosum.frac.dk/user/
// @description   Turn plain text links into real clikable links. Attempts to catch links like google.com
// @include       http://*
// @include       https://*
// @include       file:///*
// @exclude       http://*.google.*/*
// @exclude       https://*.google.*/*
// @copyright     JoeSimmons, Anthony Lieuallen, Adam
// ==/UserScript==

(function(){
var regex = /\b(?![\@\s]+)((https?|nntp|news|telnet|irc|ftp):\/\/)?(([-.A-Za-z0-9]+:)?[\#-.A-Za-z0-9]+@)?((([\w-]+(?!@)\.)?([\w-]+\.)+(ru|am|dk|com|net|org|se|no|nl|us|uk|de|it|nu|edu|info|co|in|to|fr|gov|biz|tv|mil|hu)(\.(nr|in|uk))?)|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(about:\w+))(\/[^\s]*)?\b/gi, space_regex=/ /g, http_regex=/^((https?|nntp|news|telnet|irc|ftp)\:\/\/)|(about:\w+)/i, txt=/\.txt$/i;

var black_tags = ["a", "script", "style", "textarea", "title", "option", "pre"+(txt.test(location.href)?"allowTxt":""), "code"];
var path = ".//text()[not(parent::" + black_tags.join(" or parent::") +")]";

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

for(var i=0,item; (item=textNodes.snapshotItem(i)); i++){
	
	var itemText = item.nodeValue;
	
	if(regex.test(itemText)){
		var span=document.createElement("span");	
		var lastLastIndex = 0;
			regex.lastIndex = 0;
		for(var myArray = null; myArray = regex.exec(itemText); ){
			var link = myArray[0];
			span.appendChild(document.createTextNode(itemText.substring(lastLastIndex, myArray.index)));
			var href = link.replace(space_regex,""),
				text = (link.indexOf(" ")==0)?link.substring(1):link;
			if(!http_regex.test(href)) href="http://"+href;
			var a = document.createElement("a");
			a.setAttribute("href", href.toLowerCase());
			a.setAttribute("target", "newWin"); // open in a new window/tab
			a.appendChild(document.createTextNode(text.substring(0,4).toLowerCase()+text.substring(4)));
			if((link.indexOf(" ")==0)) span.appendChild(document.createTextNode(" "));
			span.appendChild(a);
			lastLastIndex = regex.lastIndex;
		}
		span.appendChild(document.createTextNode(itemText.substring(lastLastIndex)));
		item.parentNode.replaceChild(span, item);
	}
}
})();