Last active
December 19, 2023 07:06
-
-
Save svrnm/6a2b20f35c1835ac60b7c54922bd5411 to your computer and use it in GitHub Desktop.
Global Search on the window object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function globalSearch(value, max = 100000) { | |
var stack = (function () { | |
iframe = document.createElement('iframe'); | |
document.body.appendChild(iframe); | |
var results = Object.getOwnPropertyNames(window).filter(p => !iframe.contentWindow.hasOwnProperty(p)).map(p => {return {type: typeof window[p], name: p, value: window[p]}}).reduce((c,p) => { | |
c.push([p.value, 'window.' + p.name]); | |
return c; | |
}, []); | |
document.body.removeChild(iframe); | |
//results.sort().forEach(p => console.log(p, ':', window[p])); | |
return results; | |
}()); | |
var searched = []; | |
var found = false; | |
var isArray = function(test) { | |
return Object.prototype.toString.call( test ) === '[object Array]'; | |
} | |
var isBlacklisted = function(test) { | |
return typeof test === 'function' || test instanceof Element || test instanceof XMLHttpRequestEventTarget || test instanceof Document | |
} | |
var counter = 0; | |
var result = [] | |
while(stack.length > 0 && counter < max) { | |
var fromStack = stack.pop(); | |
var obj = fromStack[0]; | |
var address = fromStack[1]; | |
if( (typeof obj == typeof value && obj == value) || typeof obj === 'string' && obj.includes(value)) { | |
result.push(address) | |
continue; | |
}else if(typeof obj == "object" && searched.indexOf(obj) == -1){ | |
if ( isArray(obj) ) { | |
var prefix = '['; | |
var postfix = ']'; | |
}else { | |
var prefix = '.'; | |
var postfix = ''; | |
} | |
for( i in obj ) { | |
if( !isBlacklisted(obj[i]) ) { | |
stack.push( [ | |
obj[i], | |
address + prefix + i + postfix | |
] ); | |
} | |
} | |
searched.push(obj); | |
} | |
counter++; | |
} | |
return result | |
} | |
/* | |
Original code: https://stackoverflow.com/a/12103127/3507477 | |
Usage: Copy the code above into the console of your developer toolbar and run it with globalSearch(keyword) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment