Last active
December 22, 2015 18:59
-
-
Save JonAbrams/6516999 to your computer and use it in GitHub Desktop.
Find, in memory, a particular string.
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 gFind(pattern) { | |
var path = []; | |
var isObject = function (obj) { | |
return (/^(Object|Array|String)/).test(obj.constructor.name); | |
}; | |
(function gFindRec(obj) { | |
var found; | |
if (typeof obj === "string") { | |
if ( pattern instanceof RegExp && pattern.test(obj) ) { | |
return true; | |
} else if ( typeof pattern === "string" && obj === pattern ) { | |
return true; | |
} | |
} else if (typeof obj === "object"){ | |
for (var key in obj) { | |
(function (key) { | |
if ( obj[key] && obj.hasOwnProperty(key) && isObject(obj[key]) ) { | |
found = gFindRec(obj[key]); | |
if (found) { | |
path.unshift(key); | |
} | |
} | |
})(key); | |
if (found) { | |
return true; | |
} | |
} | |
} | |
})(window); | |
if (path.length === 0) { | |
return "Sorry, but '" + pattern + "' could not be found in memory."; | |
} | |
var pathStr = pattern + " was found in window"; | |
for (var i = 0; i < path.length; i++) { | |
pathStr += " -> " + path[i]; | |
} | |
return pathStr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment