Skip to content

Instantly share code, notes, and snippets.

@mkropat
Created March 5, 2023 00:57
Show Gist options
  • Save mkropat/4700713333561de748e27cdb9c915864 to your computer and use it in GitHub Desktop.
Save mkropat/4700713333561de748e27cdb9c915864 to your computer and use it in GitHub Desktop.
objs = new Set
nameToObj = new Map
function t(obj) {
if (!obj || objs.has(obj) || typeof obj !== 'object') { return }
if (obj.nodeName === 'IFRAME') { return } // iframe descendants can cause security errors
objs.add(obj)
let proto = Object.getPrototypeOf(obj)
let name = (obj.constructor && obj.constructor.name) || (proto && proto.constructor && proto.constructor.name)
if (!nameToObj.has(name)) { nameToObj.set(name, []) }
nameToObj.get(name).push(obj)
for (let key of Reflect.ownKeys(obj)) {
try {
t(obj[key])
} catch (err) { }
}
t(proto)
if (obj[Symbol.iterator]) {
for (let item of obj) { t(item) }
}
}
t(window)
objs = [...objs]
nameToObj = Object.fromEntries(nameToObj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment