Recursively find variable in page's javascript from firefox extension
So I'm working on a firefox extension that we'll be using internal (thus not terrible worried about security), and I'd like to be able to interact with the functions and values defined in the javascript of a webpage. I'm having trouble reliably finding variable definitions.
Take for example gmail, it has a VIEW_DATA list that has email subjects, etc to display. It can be accessed through window.conten开发者_JS百科t.frames.wrappedJSObject.VIEW_DATA, but this doesn't seem to always work for me.
Is there a reasonable way to reliably search (possible recursively) the javascript of a page for a given variable from a firefox extension?
Is this what you are looking for?
var inspected = [];//prevent infinite loop; top===window
function inspector(obj) {
inspected.push(obj);
for(var prop in obj) {
console.log(prop);
if(!is_duplicate(obj) && typeof obj[prop] == 'object')
inspector(obj[prop]);
}
}
function is_duplicate(obj) {
for(var i = 0; i < inspected.length; i++) {
if(inspected[i] === obj)
return true;
}
return false;
}
These functions will run through all of the properties of an object, walking all the way down the tree of objects. I just log the properties, but you may wish to do something more useful. Try inspector(window)
and watch your firebug console get filled.
精彩评论