Monday, February 7, 2011

Turn plain text URLs into links and turn link color to red. Supports http, https, ftp, email addresses.


// ==UserScript==
// @name        Linkify Plus Red Links
// @version     1.0.0
// @namespace   http://arantius.com/misc/greasemonkey/
// @description Turn plain text URLs into links and turn link color to red.  Supports http, https, ftp, email addresses.
// @include     http*
// @exclude     http://www.google.tld/search*
// ==/UserScript==

/*******************************************************************************

Only modified Linkify Plus to change the link color to red because
in Quake Live the links appeared in white with white background.
Everything else is written by Anthony Liuallen of http://arantius.com/

Link to Linkify Plus
  http://userscripts.org/scripts/show/1352

** Linkify plus comments below
Loosely based on the Linkify script located at:
  http://downloads.mozdev.org/greasemonkey/linkify.user.js

Originally written by Anthony Lieuallen of http://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

Linkify Plus Red Links Version history:
 Version 1.0.2
  - Changed event listener type. Sometimes a node was not inserted, but modified.
 Version 1.0.1
  - Parenthesis fix
 Version 1.0.0
  - Changed link color to red.

Using the Linkify Plus version 2.0.2. as a base.

*******************************************************************************/

var notInTags=[
 'a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea'
];
var textNodeXpath=
 ".//text()[not(ancestor::"+notInTags.join(') and not(ancestor::')+")]";
var urlRE=/((?:https?|ftp):\/\/[^\s'"'<>]+|www\.[^\s'"'<>]+|[-\w.+]+@(?:[-\w]+\.)+[\w]{2,6})/gi;

var queue=[];

/******************************************************************************/

if ('text/xml'!=document.contentType
 && 'application/xml'!=document.contentType
) {
 linkifyContainer(document.body);
 document.body.addEventListener('DOMSubtreeModified', function(event) {
  linkifyContainer(event.target);
 }, false);
}

/******************************************************************************/

function linkifyContainer(container) {
 var xpathResult=document.evaluate(
  textNodeXpath, container, null,
  XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null
 ); 

 var i=0;
 function continuation() {
  var node, limit=0;
  while (node=xpathResult.snapshotItem(i++)) {
   linkifyTextNode(node);

   if (++limit>50) {
    return setTimeout(continuation, 0);
   }
  }
 }
 setTimeout(continuation, 0);
}

function linkifyTextNode(node) {
 var i, l, m;
 var txt=node.textContent;
 var span=null;
 var 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 (l.match(/^www/i)) {
   l='http://'+l;
  } else if (-1==l.indexOf('://')) {
   l='mailto:'+l;
  }
  a.setAttribute('href', l);
  a.style.color= '#FF0000';
  span.appendChild(a);
  //track insertion point
  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
  try {
   node.parentNode.replaceChild(span, node);
  } catch (e) {
   console.error(e);
   console.log(node);
  }
 }
}

0 comments:

Post a Comment