Skip to content

Instantly share code, notes, and snippets.

@jroakes
Created February 16, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jroakes/cdeb9d6afb9559573111e74ffb315c4b to your computer and use it in GitHub Desktop.
Save jroakes/cdeb9d6afb9559573111e74ffb315c4b to your computer and use it in GitHub Desktop.
var items = []
console.log('\n\n Starting:')
function globalSearch(obj, value, maxrec, rec = 0 ) {
var excl = ['document','window','top']
if (rec <= maxrec ){
for(var p in obj){
var result = false
if (excl.indexOf(p) == -1 && obj[p] ){
if( p.toLowerCase().indexOf(value) > -1 ){
result = p;
}else if( typeof obj[p] === "string" && obj[p].toLowerCase().indexOf(value) > -1 ){
result = p;
}else if(typeof obj[p] === "object" && obj[p] != obj){
var recl = rec+1
var te = globalSearch(obj[p], value, maxrec, recl);
if (te)
result = p + "." + te
}
}
if (result && rec == 0){
items.push(result)
}else if (result && rec > 0){
return result;
}
}
}
return false;
}
globalSearch(window, "<string to search>", 7)
@dwsmart
Copy link

dwsmart commented Feb 18, 2019

function globalSearch(theNodes, search){
var path = '';
var results = [];
nodeLoop(theNodes);
function nodeLoop(node){
	var len = node.childNodes.length
	var attrf = '';
	if(len === 1 && node.textContent.indexOf(search) > -1) {
		if(node.attributes.id){
					attrf = attrf + '#'+node.attributes.id.nodeValue
				}
				if(node.attributes.class){
					attrf = attrf + '.'+node.attributes.class.nodeValue.replace(/ /gi, ".")
				}	
		results.push(path + " > " + node.nodeName + attrf)
		path = ''
		}
    var nodes = node.childNodes;
    for (var i = 0; i <nodes.length; i++){
        if(!nodes[i]){
            continue;
        }
        if(nodes[i].childNodes.length > 0) {
			var attr = ''
			if(node.attributes.length > 0) {
				if(node.attributes.id){
					attr = attr + '#'+node.attributes.id.nodeValue
				}
				if(node.attributes.class){
					attr = attr + '.'+node.attributes.class.nodeValue.replace(/ /gi, ".")
				}
				
			}
			path = path + " > " + node.nodeName + attr
            nodeLoop(nodes[i]);
        } 
    }
}
	return(results)
}
console.log(globalSearch(document.body, 'goose'));

@dwsmart
Copy link

dwsmart commented Feb 18, 2019

Window Search Version:

function globalSearch(obj, search, maxrec, rec = 0 ) {
var result = [];
var excl = ['document','window','top']
if (rec <= maxrec ){
Object.keys(obj).forEach( function(key) {
	if(excl.indexOf(key) == -1 ) {
  var type =  typeof obj[key];
	if(type === "string") {
		if (obj[key]) {
			if( obj[key].toLowerCase().indexOf(search) > -1 ){
				result.push({key: key, value: obj[key]});
			}
		}
	}

if(type === "object" && obj[key] != obj) {
		if (obj[key]) {
			var recl = rec+1
			 var te = globalSearch(obj[key], search, maxrec, recl)
			 
			 if (te) {
				 te.forEach(function(rs) {
					 result.push({key: key + '.' + rs.key, value: rs.value});
				 })
						
			}
		}
	}
	}
	
});
	return result
}
}
console.log(globalSearch(window, 'SearchString', 50));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment