【问题标题】:List all js global variables used by site (not all defined!)列出站点使用的所有 js 全局变量(未全部定义!)
【发布时间】:2013-06-24 13:03:59
【问题描述】:

列出站点已使用的所有全局变量的方法是什么?任何浏览器 javascript 调试器都可以做到这一点吗?使用我的意思是阅读,而不是更改/添加。检测 iframe 的,也不错。

请注意: 我需要获取站点“触及”的全局变量列表。不是所有的,也不是添加的或编辑的,在网站脚本中的任何地方都使用过。

【问题讨论】:

  • @FelixKling 听起来他想要的是那些使用的,而不是创建的
  • @Alnitak:哦……嗯。然后可能必须在特别考虑全局变量的情况下执行代码覆盖率测试。
  • 不是重复的...;代码分析可能非常困难,而且不是 100% 确定的。
  • @DanDascalescu 我也向你道歉,你是对的。
  • @FlashThunder 你可能想看看this Question

标签: javascript


【解决方案1】:

在 Chrome 中,转到开发工具并打开控制台。 然后输入以下内容:

Object.keys( window );

这将为您提供一个包含所有全局变量的数组。

编辑

在谷歌上搜索了一下,我找到了一种方法。您将需要 firefoxjslinter 插件。

设置完成后,打开 jslinter 并转到左侧列中的选项->检查所有内容除了“容忍未使用的参数”

然后在网页上运行 jslinter 并在结果中向下滚动。您将拥有一个未使用变量的列表(全局变量,然后是每个函数的局部变量)。

现在在控制台中运行Object.keys(window); 并比较两者的结果以确定使用了哪些。

【讨论】:

  • 这不是我的问题的答案。这列出了所有全局变量,我只想列出站点接触的一个。
  • @FlashThunder:是的,你会得到 6 个额外的属性,你可以过滤掉它们:“top”、“window”、“location”、“external”、“chrome”、“document”铬合金。尝试在此页面上的控制台中运行 Object.keys( window );
  • @DanDascalescu 但他想要使用/读取的变量,object.keys(window) 将列出未使用/未读取的变量。
  • 这不起作用; ReferenceError: window is not defined
【解决方案2】:

此单行程序将使您非常接近,并且不需要在页面加载之前安装任何其他内容或运行代码:

Object.keys(window).filter(x => typeof(window[x]) !== 'function' &&
  Object.entries(
    Object.getOwnPropertyDescriptor(window, x)).filter(e =>
      ['value', 'writable', 'enumerable', 'configurable'].includes(e[0]) && e[1]
    ).length === 4)

它根据三个原则过滤 Object.keys(window):

  1. null 或 undefined 的内容通常不值得一看。
  2. 大多数脚本会定义一堆事件处理程序(即函数),但它们通常也没有兴趣转储。
  3. 由浏览器本身设置的窗口属性通常以特殊方式定义,它们的属性描述符反映了这一点。用赋值运算符(即window.foo = 'bar')定义的全局变量有一个外观特定的属性描述符,我们可以利用它。请注意,如果脚本使用具有不同描述符的 Object.defineProperty 定义属性,我们会忽略它们,但在实践中这种情况非常罕见。

【讨论】:

  • 你先生太棒了。我正在寻找这种类型的过滤器。
【解决方案3】:

我所做的是。我找到了一个尽可能少的 JavaScript / 框架的页面,将它们的所有键记录在数组中。 然后迭代新页面上的所有键,并仅记录上一个站点中未列出的键。 你可以试试或者使用我的代码sn-p

var ks = ["postMessage","blur","focus","close","frames","self","window","parent","opener","top","length","closed","location","document","origin","name","history","locationbar","menubar","personalbar","scrollbars","statusbar","toolbar","status","frameElement","navigator","customElements","external","screen","innerWidth","innerHeight","scrollX","pageXOffset","scrollY","pageYOffset","screenX","screenY","outerWidth","outerHeight","devicePixelRatio","clientInformation","screenLeft","screenTop","defaultStatus","defaultstatus","styleMedia","onanimationend","onanimationiteration","onanimationstart","onsearch","ontransitionend","onwebkitanimationend","onwebkitanimationiteration","onwebkitanimationstart","onwebkittransitionend","isSecureContext","onabort","onblur","oncancel","oncanplay","oncanplaythrough","onchange","onclick","onclose","oncontextmenu","oncuechange","ondblclick","ondrag","ondragend","ondragenter","ondragleave","ondragover","ondragstart","ondrop","ondurationchange","onemptied","onended","onerror","onfocus","oninput","oninvalid","onkeydown","onkeypress","onkeyup","onload","onloadeddata","onloadedmetadata","onloadstart","onmousedown","onmouseenter","onmouseleave","onmousemove","onmouseout","onmouseover","onmouseup","onmousewheel","onpause","onplay","onplaying","onprogress","onratechange","onreset","onresize","onscroll","onseeked","onseeking","onselect","onstalled","onsubmit","onsuspend","ontimeupdate","ontoggle","onvolumechange","onwaiting","onwheel","onauxclick","ongotpointercapture","onlostpointercapture","onpointerdown","onpointermove","onpointerup","onpointercancel","onpointerover","onpointerout","onpointerenter","onpointerleave","onafterprint","onbeforeprint","onbeforeunload","onhashchange","onlanguagechange","onmessage","onmessageerror","onoffline","ononline","onpagehide","onpageshow","onpopstate","onrejectionhandled","onstorage","onunhandledrejection","onunload","performance","stop","open","alert","confirm","prompt","print","requestAnimationFrame","cancelAnimationFrame","requestIdleCallback","cancelIdleCallback","captureEvents","releaseEvents","getComputedStyle","matchMedia","moveTo","moveBy","resizeTo","resizeBy","getSelection","find","webkitRequestAnimationFrame","webkitCancelAnimationFrame","fetch","btoa","atob","setTimeout","clearTimeout","setInterval","clearInterval","createImageBitmap","scroll","scrollTo","scrollBy","onappinstalled","onbeforeinstallprompt","crypto","ondevicemotion","ondeviceorientation","ondeviceorientationabsolute","indexedDB","webkitStorageInfo","sessionStorage","localStorage","chrome","visualViewport","speechSynthesis","webkitRequestFileSystem","webkitResolveLocalFileSystemURL","addEventListener", "removeEventListener", "openDatabase", "dispatchEvent"]
var newKs = []
for (key in window) {
    if(ks.indexOf(key) == -1 && key !== "ks" && key !=="newKs") {
        newKs.push(key);
    }
}
console.log(newKs);

【讨论】:

    【解决方案4】:

    您可以尝试为此使用 getter,您可以为所有现有的全局变量创建它。在页面启动之前运行:

    Object.keys(window) // or
    Object.getOwnPropertyNames(window).concat(
      Object.getOwnPropertyNames(Object.getPrototypeOf(window))
    ) // or whatever
    .forEach(function(name) {
        var d = Object.getOwnPropertyDescriptor(window, name),
            def = Object.defineProperty,
            log = console.log.bind(console);
        if (d && !d.configurable)
            return log("cannot detect accessing of "+name);
        def(window, name, {
            configurable: true,
            get: function() {
                log("window."+name+" was used by this page!");
                if (d) {
                    def(window, name, d);
                    return d.get ? d.get() : d.value;
                } else { // it was not an own property
                    delete window[name];
                    return window[name];
                }
            },
            set: function(x) {
                log("Ugh, they're overwriting window."+name+"! Something's gonna crash.");
            }
        });
    });
    

    当然,属性描述符等与旧版浏览器不兼容。请注意,有一些全局变量/window 属性可能无法以编程方式列出(如on* 处理程序),如果您需要它们,则必须在数组中显式列出它们。请参阅相关问题List all properties of window object?Cross Browser Valid JavaScript Names

    但我想运行一个代码覆盖率工具来抱怨未声明的全局变量,就像@stackErro 建议的那样,会更有帮助。

    【讨论】:

      【解决方案5】:

      由于这个问题是谷歌搜索如何列出全局 javascript 变量的方法时的第一个问题,因此我将为此添加自己的答案。有时您需要列出全局变量以查看您的代码是否没有变量泄漏到范围之外(定义时不带 'var')。为此,请在调试控制台中使用它:

      (function ()
      {
         var keys=Object.keys( window );
         for (var i in keys)
         {
            if (typeof window[keys[i]] != 'function')
            console.log(keys[i], window[keys[i]]);
         }
      })();
      

      它将列出标准的全局变量,如窗口、文档、位置等。这些只是少数。所以你可以很容易地在列表中找到你泄露的变量。

      【讨论】:

      • 这仍然不是我问题的答案...我问的是reading event而不是writting event ...我需要列出所有并排使用的变量...例如如果网站读取windows.navigator.userAgent.
      【解决方案6】:

      将以下代码复制并粘贴到您的 javascript 控制台中

      var keys = Object.getOwnPropertyNames( window ),
          value;
      
      for( var i = 0; i < keys.length; ++i ) {
          value = window[ keys[ i ] ];
          console.log( value );
      }
      

      所有归功于 RightSaidFred (Javascript - dumping all global variables)

      希望对你有帮助

      【讨论】:

      • 这不是我的问题的答案。
      • 您的回答很棒,但我添加console.info 以便更好地阅读
      【解决方案7】:

      列出我有时使用的全局变量的简单方法。 在执行任何脚本之前,请尽早放置此代码。

      var WINDOW_PROPS = Object.keys(window);
      

      然后,当您需要发现全局变量时,只需执行以下操作:

      var GLOBALS = Object.keys(window)
          // filter the props which your code did not declare
          .filter(prop => WINDOW_PROPS.indexOf(prop) < 0)
          // prettify output a bit :) It's up to you...
          .map(prop => `${typeof window[prop]} ${prop} ${window[prop]}`)
          // sort by types and names to find easier what you need
          .sort();
      
      console.log(GLOBALS.join("\n"));
      

      我在这里使用了一些 ES6 特性来缩短代码。它仍然不适合生产,但足以用于调试目的,并且应该可以在现代浏览器中使用。

      【讨论】:

        【解决方案8】:

        从 Object.keys(window) 中取出列表,删除浏览器默认生成的全局变量,只剩下你在页面上声明的全局变量。注意:您声明的函数也算作全局变量。

        const listGlobal = ()=> { //for debugging purposes
            //put this function inside your html or javascript. Go to the html page.
            //In chrome console type listGlobal(); to see list of global vars
        
            //Array of global variables that exist in chrome browser by default
            var stdChromeVars = ["parent","opener","top","length","frames","closed","location","self","window","document","name","customElements","history","locationbar","menubar","personalbar","scrollbars","statusbar","toolbar","status","frameElement","navigator","origin","external","screen","innerWidth","innerHeight","scrollX","pageXOffset","scrollY","pageYOffset","visualViewport","screenX","screenY","outerWidth","outerHeight","devicePixelRatio","clientInformation","screenLeft","screenTop","defaultStatus","defaultstatus","styleMedia","onsearch","isSecureContext","onabort","onblur","oncancel","oncanplay","oncanplaythrough","onchange","onclick","onclose","oncontextmenu","oncuechange","ondblclick","ondrag","ondragend","ondragenter","ondragleave","ondragover","ondragstart","ondrop","ondurationchange","onemptied","onended","onerror","onfocus","onformdata","oninput","oninvalid","onkeydown","onkeypress","onkeyup","onload","onloadeddata","onloadedmetadata","onloadstart","onmousedown","onmouseenter","onmouseleave","onmousemove","onmouseout","onmouseover","onmouseup","onmousewheel","onpause","onplay","onplaying","onprogress","onratechange","onreset","onresize","onscroll","onseeked","onseeking","onselect","onstalled","onsubmit","onsuspend","ontimeupdate","ontoggle","onvolumechange","onwaiting","onwebkitanimationend","onwebkitanimationiteration","onwebkitanimationstart","onwebkittransitionend","onwheel","onauxclick","ongotpointercapture","onlostpointercapture","onpointerdown","onpointermove","onpointerup","onpointercancel","onpointerover","onpointerout","onpointerenter","onpointerleave","onselectstart","onselectionchange","onanimationend","onanimationiteration","onanimationstart","ontransitionend","onafterprint","onbeforeprint","onbeforeunload","onhashchange","onlanguagechange","onmessage","onmessageerror","onoffline","ononline","onpagehide","onpageshow","onpopstate","onrejectionhandled","onstorage","onunhandledrejection","onunload","performance","stop","open","alert","confirm","prompt","print","queueMicrotask","requestAnimationFrame","cancelAnimationFrame","captureEvents","releaseEvents","requestIdleCallback","cancelIdleCallback","getComputedStyle","matchMedia","moveTo","moveBy","resizeTo","resizeBy","scroll","scrollTo","scrollBy","getSelection","find","webkitRequestAnimationFrame","webkitCancelAnimationFrame","fetch","btoa","atob","setTimeout","clearTimeout","setInterval","clearInterval","createImageBitmap","close","focus","blur","postMessage","onappinstalled","onbeforeinstallprompt","crypto","indexedDB","webkitStorageInfo","sessionStorage","localStorage","chrome","applicationCache","onpointerrawupdate","trustedTypes","speechSynthesis","webkitRequestFileSystem","webkitResolveLocalFileSystemURL","openDatabase","caches","ondevicemotion","ondeviceorientation","ondeviceorientationabsolute"];
        
        
            //load the current list of global variables
            let thisdocVars = Object.keys(window);
        
            //remove from the current list any variables that's in the browser default list
            stdChromeVars.forEach(DelFunc);
                function DelFunc(item) {
                    thisdocVars.forEach((e,i)=>{if(e==item){thisdocVars.splice(i, 1);}});
                }
        
            //separate variables into functions and variables
            let thisdocfunc = [];
            let thisdocvar = [];
            thisdocVars.forEach((e)=>{if(typeof window[e]=="function"){thisdocfunc.push(e);}else{thisdocvar.push(e);}});
            console.log("Global Functions:\n" + thisdocfunc);
            console.log("Global Variables:\n" + thisdocvar);
            //Ctrl+Shift+i to see console in chrome
        }
        

        【讨论】:

        • 这没有列出使用但声明的变量,不是我的问题的答案
        【解决方案9】:

        您可以尝试使用我所做的 JetBrains PhpStorm,您可以在任何系统上免费试用 30 天。然后你检查 JSLint 或 JSHint 或两者我不记得然后你所有未使用的变量都带有下划线,用不同的颜色突出显示(根据主题)并在滚动条上可见,当你将鼠标悬停在它们上面时,它会显示未使用的变量;

        编辑: 我认为社区版现在是免费的。

        【讨论】:

        • OP 询问的是可通过浏览器控制台访问的全局变量,而不是直接通过分析源代码,这正是 WebStorm 所做的。
        • 我认为你是对的@DanDascalescu 但我也认为这是检查使用了哪些变量的唯一方法。所以我不会删除这个答案。
        • 我其实觉得比挖掘窗口页面中已经添加的内容更有用。有一个工具可以准确地告诉你从未使用过的东西,嗯,对我来说看起来要好一些。
        猜你喜欢
        • 1970-01-01
        • 2019-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 2011-09-09
        相关资源
        最近更新 更多