Skip to content

Instantly share code, notes, and snippets.

@jeffreytgilbert
Created September 27, 2019 19:29
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 jeffreytgilbert/f5cf15c93c00f4888035283489b7f9b6 to your computer and use it in GitHub Desktop.
Save jeffreytgilbert/f5cf15c93c00f4888035283489b7f9b6 to your computer and use it in GitHub Desktop.
Find a needle in the global haystack of nested junk
const findNeedleInHaystack = (needle, haystack) => {
let reference = [];
let promises = [];
const finder = (obj, chain) => {
return new Promise((resolve, reject)=>{
if (reference.includes(obj)) {
return resolve(chain.join('.')); // already checked this object
}
// add object as "checked" to reference registry
reference.push(obj);
let nextChain;
for (let prop in obj) {
nextChain = chain.map(val => val);
nextChain.push(prop);
try {
if (typeof obj[prop] === 'object' && obj[prop] && obj[prop].constructor && (obj[prop].constructor.name === 'Window' || obj[prop] instanceof Window)) { continue; }
if (typeof obj[prop] === 'object') {
promises.push(finder(obj[prop], nextChain));
} else if(typeof obj[prop] === 'string') {
if(obj[prop].indexOf(needle) !== -1) {
console.warn('FOUND', nextChain.join('.'), obj.constructor.name);
}
}
} catch (ex) {
console.info(nextChain.join('.'), ex.message);
}
}
resolve(chain.join('.'));
});
};
promises.push(finder(haystack, [haystack.constructor.name]));
Promise.all(promises).then((results)=>{
console.log(reference.length);
// console.log(results);
});
}
window.tier1 = {
tier2: [
{},
{},
{
tier3: {
thing: 'Chrome'
}
}
]
};
findNeedleInHaystack('Chrome', window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment