Javascript - dumping all global variables

后端 未结 8 1885
迷失自我
迷失自我 2020-11-28 05:39

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

相关标签:
8条回答
  • 2020-11-28 06:21

    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

    0 讨论(0)
  • 2020-11-28 06:22

    To get "globals" object you can use this function:

    function globals() { return this; }
    

    Here is the test: http://jsfiddle.net/EERuf/

    0 讨论(0)
提交回复
热议问题