Source for "Linkify Plus"

By arantius
Has 14 other scripts.


// ==UserScript==
// @name        Linkify Plus
// @homepage    http://www.arantius.com/article/arantius/linkify+plus/
// @version     1.1.2
// @namespace   http://www.arantius.com/misc/greasemonkey/
// @description	Turn plain text URLs into links.  Supports http, https, ftp, email addresses
// @include     *
// ==/UserScript==

//
// 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
//

//
// Version history:
//  Version 1.1.3:
//   - Include "+" in the username of email addresses.
//  Version 1.1.2:
//   - Include "." in the username of email addresses.
//  Version 1.1:
//   - Fixed a big that caused the first link in a piece of text to
//     be skipped (i.e. not linkified).
//

(function(){ try {
var notInTags=['a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea'];
var res = document.evaluate("//text()[not(ancestor::"+notInTags.join(') and not(ancestor::')+")]",
	document, null,	XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); 
var i, el, l, m, p, span, txt, 
	urlRE=/((?:https?|ftp):\/\/[^\s'"'<>()]*|[-\w.+]+@(?:[-\w]+\.)+[\w]{2,6})/gi;

for (i=0; el=res.snapshotItem(i); i++) {
	//grab the text of this element and be sure it has a URL in it
	txt=el.textContent;
	span=null;
	p=0;
	while (m=urlRE.exec(txt)) {
		if (null==span) {
			//create a span to hold the new text with links in it
			span=document.createElement('span');
		}

		//get the link without trailing dots
		l=m[0].replace(/\.*$/, '');
		//put in text up to the link
		span.appendChild(document.createTextNode(txt.substring(p, m.index)));
		//create a link and put it in the span
		a=document.createElement('a');
		a.className='linkifyplus';
		a.appendChild(document.createTextNode(l));
		if (-1==l.indexOf('://')) l='mailto:'+l;
		a.setAttribute('href', l);
		span.appendChild(a);
		p=m.index+m[0].length;
	}
	if (span) {
		//take the text after the last link
		span.appendChild(document.createTextNode(txt.substring(p, txt.length)));
		//replace the original text with the new span
		el.parentNode.replaceChild(span, el);
	}
}
} catch(e) {dump('Linkify Plus Error ('+e.lineNumber+'): '+e+'\n');} })();