Skip to content

Instantly share code, notes, and snippets.

@b1ghawk
Forked from svrnm/globalSearch.js
Created December 19, 2023 07:06
Show Gist options
  • Save b1ghawk/ce2b519e6ec93649a454dc62d3c804d3 to your computer and use it in GitHub Desktop.
Save b1ghawk/ce2b519e6ec93649a454dc62d3c804d3 to your computer and use it in GitHub Desktop.
Global Search on the window object
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