// ==UserScript==
// @name           Linkify
// @namespace      http://youngpup.net/userscripts
// @description    Looks for things in the page that look like URLs but aren't hyperlinked, and converts them to clickable links.
// @include        *
// ==/UserScript==
// Modified for Greasemonkey for IE

    urlRegex = /\b(https?:\/\/[^\s+\"\<\>]+)/ig;
	candidates = new Array()
    // tags we will scan looking for un-hyperlinked urls
    var allowedParents = [
        "abbr", "acronym", "address", "applet", "b", "bdo", "big", "blockquote", "body", 
        "caption", "center", "cite", "code", "dd", "del", "div", "dfn", "dt", "em", 
        "fieldset", "font", "form", "h1", "h2", "h3", "h4", "h5", "h6", "i", "iframe",
        "ins", "kdb", "li", "object", "pre", "p", "q", "samp", "small", "span", "strike", 
        "s", "strong", "sub", "sup", "td", "th", "tt", "u", "var"
        ];

		var l=0;
		for(i=0;i<allowedParents.length;i++)
		{
			var aObjects = document.body.getElementsByTagName(allowedParents[i]);
			
			if(aObjects)
			{
				for(j=0;j<aObjects.length;j++)
				{
					if((aObjects[j].innerText.indexOf('HTTP') !=-1) ||
 					   (aObjects[j].innerText.indexOf('http') !=-1))
					   {
							if(aObjects[j].childNodes)
							{
					   			for(k=0;k<aObjects[j].childNodes.length;k++)
								{
									candidates[l++] = aObjects[j].childNodes[k];
								}
							}
					   }
				}
			}

		}


    for (var cand = null, i = 0; i<candidates.length; i++) {
		cand = candidates[i];
		if(cand)
		{
	        if (urlRegex.test(cand.nodeValue)) {
	            var span = document.createElement("span");
	            var source = cand.nodeValue;
	            
	            cand.parentNode.replaceChild(span, cand);
	
	            urlRegex.lastIndex = 0;
	            for (var match = null, lastLastIndex = 0; (match = urlRegex.exec(source)); ) {
	                span.appendChild(document.createTextNode(source.substring(lastLastIndex, match.index)));
	                
	                var a = document.createElement("a");
	                a.setAttribute("href", match[0]);
	                a.appendChild(document.createTextNode(match[0]));
	                span.appendChild(a);
	
	                lastLastIndex = urlRegex.lastIndex;
	            }
	
	            span.appendChild(document.createTextNode(source.substring(lastLastIndex)));
	            span.normalize();
	        }
		}
    }
  
