/*
Searcher finds all texts on document even those inside interactive elements
as IMPUT, TEXTAREA etc.
Written by Paul Geisler for Gibney Enterprises in 2006
*/
var id="GIBNEY_ORG_SEARCHER_"; //ID für unique element namespace
var searcherOverlayWindow; //our DIV-overlay
var targetElements; //HTML-nodes of matches
var targetText; //match content itself (see below)
var IEgewurschtel=(navigator.appName=="Microsoft Internet Explorer"); //the usual suspects
/*
create a DIV-overlay
*/
function createDiv(id,posX,posY,content){
var menuDiv=document.createElement("div");
menuDiv.id=id;
menuDiv.innerHTML=content;
//setAttribute("style"... is MOZ only with collated string
menuDiv.style.textAlign="left";
menuDiv.style.position="fixed";
menuDiv.style.backgroundColor="white";
menuDiv.style.border="1px solid black";
menuDiv.style.padding="0px";
if (IEgewurschtel){
menuDiv.style.position="absolute"; //IE has no "fixed"
}
menuDiv.style.top =posY+'px';
menuDiv.style.left=posX+'px';
document.getElementsByTagName('body').item(0).appendChild(menuDiv);
return menuDiv;
}
/*
call point for search
Searches needle and put result-linls to element "Presenter"
*/
function search(){
var needle=document.getElementById(id+"Query").value
var result="";
targetElements=new Array();
targetText=new Array();
if (needle.length>2) { //don't look for very short terms as this would return many results and slow things down
//regexp for search term and some contextual content
result=recurseSearch(document,new RegExp("(.{0,20})("+needle+")(.{0,20})","gi"));
document.getElementById(id+"Presenter").innerHTML=result;
}
}
/*
recursive search the DOM below node for regexp reg
returns links using mark() to mark the result on click
*/
function recurseSearch(node, reg){
var result="";
if (node.id==id+"TheSearcher") return "";
if (node.nodeType==3 || ("value" in node) ){
var text;
if ("value" in node) //interaktive elements (INPUT; AREA) have no actual .data
text=node.value;
else
text=node.data;
if ( typeof (text)!="string" ) return ""; //just in case
var hits;
while(hits=reg.exec(text)){
var found='
'+hits[1]+"["+hits[2]+"]"+hits[3]+"
";
var target=null;
if ( "setAttribute" in node) //attachpoint for selection coarse
target=node;
else if ( "setAttribute" in node.parentNode)
target=node.parentNode;
if ("createRange" in document) //attachpoint for selection fine
target=node;
else if ( "selectionStart" in node) //MOZ
target=node;
else if ( "selectionStart" in node.parentNode)
target=node.parentNode;
if (target!=null){
//search term is markable in some way
var pos=hits.index+hits[1].length;
var length=hits[2].length;
var theId=targetElements.length;
targetElements[theId]=target;
targetText[theId]=hits[2];
//now wrap in link calling mark()
found= ''+found+'';
}
result=result+found;
}
} else {
//recursion
var cs=node.childNodes;
for (var i=0; i