List all js global variables used by site (not all defined!)

前端 未结 9 1876
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 13:16

What is the way to list all global variables that have been used by the site? Can any browser javascript debugger do that? By used I mean READ, not changed/added. Detect ifr

9条回答
  •  春和景丽
    2020-12-04 13:38

    This one-liner will get you pretty close, and does not require installing anything additional, or running code before the page loads:

    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)
    

    It filters Object.keys(window) based on three principles:

    1. Things that are null or undefined are usually not interesting to look at.
    2. Most scripts will define a bunch of event handlers (i.e. functions) but they are also usually not interesting to dump out.
    3. Properties on window that are set by the browser itself, are usually defined in a special way, and their property descriptors reflect that. Globals defined with the assignment operator (i.e. window.foo = 'bar') have a specific-looking property descriptor, and we can leverage that. Note, if the script defines properties using Object.defineProperty with a different descriptor, we'll miss them, but this is very rare in practice.

提交回复
热议问题