Skip to content

Instantly share code, notes, and snippets.

@iso2022jp
Created July 22, 2017 06:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iso2022jp/6b948cc5cb949d39ac5f910419ea65db to your computer and use it in GitHub Desktop.
Save iso2022jp/6b948cc5cb949d39ac5f910419ea65db to your computer and use it in GitHub Desktop.
何か探索するアレ
function $grep(o, v, p = '') {
$grep.known = []
return $grep.find(o, v, p)
}
$grep.match = function (e, v) {
return v instanceof RegExp ? typeof e !== 'object' && typeof e !== 'function' && v.test(String(e)) : e === v
}
$grep.pandora = function (key) {
// if (key.startsWith('-')) { return true }
if (key.startsWith('gm_')) { return true }
if (key.startsWith('__gm')) { return true }
if (key === 'streetView') { return true }
if (key === 'top') { return true }
if (key === 'window') { return true }
if (key === 'frames') { return true }
if (key === 'self') { return true }
return false
}
$grep.find = function (o, v, p) {
// match
if ($grep.match(o, v)) { return p }
if (o === null || typeof o !== 'object' || o instanceof Node) { return null }
// dive into
$grep.known.push(o)
var names = Object.getOwnPropertyNames(o)
for (k in names) {
var n = names[k]
// avoid chaos
if ($grep.pandora(n)) { continue }
try {
var e = o[n]
// known?
if ($grep.known.lastIndexOf(e) != -1) { continue }
// log scan path
var path = p + (isNaN(+n) ? '.' + n : '[' + n + ']')
if (typeof e === 'number' || typeof e === 'string') {
// console.log(path + ': ' + e)
} else {
// console.log(path)
}
// found?
var found = $grep.find(e, v, path)
if (found !== null) { return found }
} catch (e) {
// boo!
}
}
return null
}
$grep(this, /Hoge/)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment