You may find it useful to use a general method for processing text-
deepText will pass all the text nodes that are descendants of a specified parent
to a specified function. Something like a recursive forEach method.
wrapTableMail is set up to process every table on the page, looking for valid email text, but you could specify one table,or one row to start from.
It will find common emails(joesmith@website.com), but you'll probably want a better Rx filter.
- function deepText(hoo, fun) {
-
var A= [], next, T, pa, i;
-
if(!hoo) return A;
-
if(hoo.nodeType== 3) A.push(fun(hoo));
-
else if(hoo.hasChildNodes()) {
-
pa= hoo.childNodes, i= 0;
-
while(pa[i]){
-
next= pa[i++];
-
T= next.nodeType;
-
if(T== 3){
-
if(/\w+/.test(next.data)) A.push(fun(next));
-
}
-
else if(T== 1) A= A.concat(arguments.callee(next, fun));
-
}
-
}
-
return A;
-
}
- function wrapTableMail(){
-
var T= document.getElementsByTagName('table');
-
var wrapN= document.getElementsByTagName('a').length;
-
var L= T.length, who;
-
var fun= function(hoo){
-
var Rx= /\b([\w]+(\.[\w-]+)*@([\w-]+\.)[a-z]{2,6})\b/i
-
if(hoo.data){
-
var pa= hoo.parentNode;
-
if(pa.nodeName== 'A') return;
-
var res,el;
-
var str= hoo.data;
-
var X= document.createElement('a');
-
X.className= 'wrapMailCss';
-
while(str && (res= Rx.exec(str)) != null){
-
var tem= res[1];
-
el= X.cloneNode(false);
-
el.href= 'mailto:'+tem;
-
el.appendChild(document.createTextNode(tem));
-
hoo.replaceData(res.index,tem.length,'');
-
hoo= hoo.splitText(res.index);
-
str= hoo.data;
-
if (str) pa.insertBefore(el, hoo);
-
else pa.appendChild(el);
-
Rx.lastIndex= 0;
-
}
-
}
-
}
-
while(L){
-
who= T[--L];
-
deepText(who,fun)
-
}
-
-
// the rest is just success testing code
-
wrapN=document.getElementsByTagName('a').length-wrapN;
-
var s= (wrapN)+ ' mailto link';
-
if(wrapN>1) wrapN+= 's';
-
alert(s+' added');
-
}
//call the function
wrapTableMail();