function nestedReplace(x) { if(x.tagName && x.tagName == 'A') { return; } if(x.childNodes) { for(var i=0; i<x.childNodes.length; i++) { nestedReplace(x.childNodes[i]); } } if(x.nodeName == '#text') { var urlmatch = /http:\/\/\S*[^\s'"()!?.,]/g; var text = x.nodeValue.split(urlmatch); if(text.length > 1) { var urls = x.nodeValue.match(urlmatch); var index = 0, p = x.parentNode; var n = document.createDocumentFragment(); n.appendChild(document.createTextNode(text[index])); while(index < urls.length) { var link = document.createElement('a'); link.href = urls[index]; link.onclick = function() { location = 'javascript:\'<meta http-equiv=refresh content="0;'+this.href+'">Please wait...\''; return false; }; link.appendChild(document.createTextNode(urls[index])); n.appendChild(link, x); index++; n.appendChild(document.createTextNode(text[index]), x); } p.replaceChild(n, x); } } } window.addEventListener('load', function() { nestedReplace(document) }, false); |