Created
February 7, 2014 13:54
-
-
Save Yogu/8862953 to your computer and use it in GitHub Desktop.
Recursively find string in DOM
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 find(object, search, cache, path) { | |
if (!cache) | |
cache = []; | |
if (!path) | |
path = ''; | |
if (cache.indexOf(object) >= 0) | |
return; | |
cache.push(object); | |
for (var key in object) { | |
if (object.hasOwnProperty && !object.hasOwnProperty(key)) | |
continue; | |
var found = key.indexOf(search) >= 0; | |
var value; | |
try { | |
value = object[key]; | |
} catch (e) {} | |
found |= value && value.indexOf && value.indexOf(search) >= 0; | |
if (found) { | |
console.log(path); | |
console.log(object); | |
} | |
if (value instanceof Object) | |
find(value, search, cache, path + '.' + key); | |
} | |
} | |
find(window, 'string to search in the entire DOM') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment