-
-
Save ITJoePC/86a6d7edbb5f592079ba41dc6677881b to your computer and use it in GitHub Desktop.
Waldo JS -- Find Strings in JS DOM
This file contains hidden or 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
/* | |
Author: Angus Croll | |
Found at: https://javascriptweblog.wordpress.com/2011/07/11/waldo-search-the-javascript-runtime-in-under-1-kb/ | |
MINIFIED AS A BOOKMARKLET: | |
javascript:(function(){var c=function(d,e,f){var f=f||{};var i=f.obj||window;var h=f.path||((i==window)?"window":"");var g=Object.keys(i);g.forEach(function(j){if((b[d]||d)(e,i,j)){console.log([h,".",j].join(""),"->",["(",typeof i[j],")"].join(""),i[j])}if(Object.prototype.toString.call(i[j])=="[object Object]"&&(i[j]!=i)&&h.split(".").indexOf(j)==-1){c(d,e,{obj:i[j],path:[h,j].join(".")})}})};var a=function(d,g,e,f){(!g||typeof e==g)?c(d,e,f):console.error([e,"must be",g].join(" "))};var b={name:function(d,e,f){return d==f},nameContains:function(d,e,f){return f.indexOf(d)>-1},type:function(d,e,f){return e[f] instanceof d},value:function(d,e,f){return e[f]===d},valueCoerced:function(d,e,f){return e[f]==d}};window.find={byName:function(d,e){a("name","string",d,e)},byNameContains:function(d,e){a("nameContains","string",d,e)},byType:function(d,e){a("type","function",d,e)},byValue:function(d,e){a("value",null,d,e)},byValueCoerced:function(d,e){a("valueCoerced",null,d,e)},custom:function(e,d){c(e,null,d)}}})(); | |
*/ | |
(function(){ | |
var traverse = function(util, searchTerm, options) { | |
var options = options || {}; | |
var obj = options.obj || window; | |
var path = options.path || ((obj==window) ? "window" : ""); | |
var props = Object.keys(obj); | |
props.forEach(function(prop) { | |
if ((tests[util] || util)(searchTerm, obj, prop)){ | |
console.log([path, ".", prop].join(""), "->",["(", typeof obj[prop], ")"].join(""), obj[prop]); | |
} | |
if(Object.prototype.toString.call(obj[prop])=="[object Object]" && | |
(obj[prop] != obj) && path.split(".").indexOf(prop) == -1) { | |
traverse(util, searchTerm, {obj: obj[prop], path: [path,prop].join(".")}); | |
} | |
}); | |
} | |
var dealWithIt = function(util, expected, searchTerm, options) { | |
(!expected || typeof searchTerm == expected) ? | |
traverse(util, searchTerm, options) : | |
console.error([searchTerm, 'must be', expected].join(' ')); | |
} | |
var tests = { | |
'name': function(searchTerm, obj, prop) {return searchTerm == prop}, | |
'nameContains': function(searchTerm, obj, prop) {return prop.indexOf(searchTerm)>-1}, | |
'type': function(searchTerm, obj, prop) {return obj[prop] instanceof searchTerm}, | |
'value': function(searchTerm, obj, prop) {return obj[prop] === searchTerm}, | |
'valueCoerced': function(searchTerm, obj, prop) {return obj[prop] == searchTerm} | |
} | |
window.find={ | |
byName: function(searchTerm, options) {dealWithIt('name', 'string', searchTerm, options);}, | |
byNameContains: function(searchTerm, options) {dealWithIt('nameContains', 'string', searchTerm, options);}, | |
byType: function(searchTerm, options) {dealWithIt('type', 'function', searchTerm, options);}, | |
byValue: function(searchTerm, options) {dealWithIt('value', null, searchTerm, options);}, | |
byValueCoerced: function(searchTerm, options) {dealWithIt('valueCoerced', null, searchTerm, options);}, | |
custom: function(fn, options) {traverse(fn, null, options);} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment