Click to See Complete Forum and Search --> : Get sourcecode through javascript?


geuis
08-12-2003, 06:28 PM
Is it possible to create a javascript that will display the source for a target web page? I want to search for specific text strings in the source code of different webpages.

Vladdy
08-12-2003, 06:46 PM
Slow with tag soup pages but works:

function hilite(text,style,options)
{ nodeIterator = function(node,type,cbf)
{ if(node.nodeType == type) cbf(node);
if(!node.childNodes) return;
for(var i=0; i<node.childNodes.length; i++) nodeIterator(node.childNodes[i],type,cbf);
return;
};
applyStyle = function(node)
{ stRE = /^\s*\.(\w*)\s*$/;
res=stRE.exec(style);
if(res && res[1]) node.className = res[1];
else node.style.backgroundColor = style;
};
checkTextNode = function(tn)
{ if(tn.parentNode.nodeName.toLowerCase() == 'textarea') return;
textRE = new RegExp('(' + text + ')',options=='i'?'i':'');
frags = tn.nodeValue.split(textRE);
if(frags.length > 1)
{ if(frags.length == 3 && frags[0].length == 0 && frags[2].length == 0)
{ applyStyle(tn.parentNode);
return;
}
tn.nodeValue = frags[0];
ns=tn.nextSibling;
for(var i=1; i<frags.length; i++)
{ if(i%2 == 0)
{ tn.parentNode.insertBefore(document.createTextNode(frags[i]),ns);
}
else
{ var span=document.createElement('span');
span.appendChild(document.createTextNode(frags[i]));
applyStyle(span);
tn.parentNode.insertBefore(span,ns);
}
}
}
return;
};
nodeIterator(document.body,3,checkTextNode);
}