Is there a way in Javascript to get a list or dump the contents of all global variables declared by Javascript/jQuery script on a page? I am particularly interested in array
Greasymonkey script to get leaked globals
// ==UserScript==
// @name SCI
// @namespace ns
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
console.log('SCI loaded');
var SCI = window.SCI = {
defprops: [],
collect: function(){
var wprops = [];
for(var prop in window){
wprops.push(prop);
}
return wprops;
},
collectDef: function(){
this.defprops = this.collect();
},
diff: function(){
var def = this.defprops,
cur = this.collect();
var dif = [];
for(var i = 0; i < cur.length; i++){
var p = cur[i];
if(def.indexOf(p) === -1){
dif.push(p);
}
}
return dif;
},
diffObj: function(){
var diff = this.diff();
var dobj = {};
for (var i = 0; i < diff.length; i++){
var p = diff[i];
dobj[p]=window[p];
}
return dobj;
}
};
SCI.collectDef();
To use run in console SCI.diff()
to get list of names or SCI.diffObj()
to get object with variables
To get "globals" object you can use this function:
function globals() { return this; }
Here is the test: http://jsfiddle.net/EERuf/