识别窗口命名空间中的所有非本机变量?

时间:2015-01-12 20:10:33

标签: javascript

我想识别浏览器窗口命名空间中的所有变量,这些变量在加载about:blank页面(或空白html页面)时不存在,以便因编码错误而监视新库或意外全局变量。如何可靠地确定应该存在的变量?

我想也许我可以用MDNs list of window property/method/handlers导致我创建这个白名单:

{
    "applicationCache": 1,
    "closed": 1,
    "Components": 1,
    "console": 1,
    "content": 1,
    "controllers": 1,
    "crypto": 1,
    "defaultStatus": 1,
    "devicePixelRatio": 1,
    "dialogArguments": 1,
    "directories": 1,
    "document": 1,
    "frameElement": 1,
    "frames": 1,
    "fullScreen": 1,
    "globalStorage": 1,
    "history": 1,
    "innerHeight": 1,
    "innerWidth": 1,
    "length": 1,
    "location": 1,
    "locationbar": 1,
    "localStorage": 1,
    "menubar": 1,
    "messageManager": 1,
    "mozAnimationStartTime": 1,
    "mozInnerScreenX": 1,
    "mozInnerScreenY": 1,
    "mozPaintCount": 1,
    "name": 1,
    "navigator": 1,
    "opener": 1,
    "outerHeight": 1,
    "outerWidth": 1,
    "pageXOffset": 1,
    "pageYOffset": 1,
    "sessionStorage": 1,
    "parent": 1,
    "performance": 1,
    "personalbar": 1,
    "pkcs11": 1,
    "returnValue": 1,
    "screen": 1,
    "screenX": 1,
    "screenY": 1,
    "scrollbars": 1,
    "scrollMaxX": 1,
    "scrollMaxY": 1,
    "scrollX": 1,
    "scrollY": 1,
    "self": 1,
    "sidebar": 1,
    "status": 1,
    "statusbar": 1,
    "toolbar": 1,
    "top": 1,
    "window": 1,
    "alert": 1,
    "back": 1,
    "blur": 1,
    "captureEvents": 1,
    "clearImmediate": 1,
    "close": 1,
    "confirm": 1,
    "disableExternalCapture": 1,
    "dispatchEvent": 1,
    "dump": 1,
    "enableExternalCapture": 1,
    "find": 1,
    "focus": 1,
    "forward": 1,
    "getAttention": 1,
    "getAttentionWithCycleCount": 1,
    "getComputedStyle": 1,
    "getDefaulComputedStyle": 1,
    "getSelection": 1,
    "home": 1,
    "matchMedia": 1,
    "maximize": 1,
    "minimize": 1,
    "moveBy": 1,
    "moveTo": 1,
    "mozRequestAnimationFrame": 1,
    "open": 1,
    "openDialog": 1,
    "postMessage": 1,
    "print": 1,
    "prompt": 1,
    "releaseEvents": 1,
    "removeEventListener": 1,
    "resizeBy": 1,
    "resizeTo": 1,
    "restore": 1,
    "routeEvent": 1,
    "scroll": 1,
    "scrollBy": 1,
    "scrollByLines": 1,
    "scrollByPages": 1,
    "scrollTo": 1,
    "setCursor": 1,
    "setImmediate": 1,
    "setResizable": 1,
    "showModalDialog": 1,
    "sizeToContent": 1,
    "stop": 1,
    "updateCommands": 1,
    "ondevicelight": 1,
    "ondevicemotion": 1,
    "ondeviceorientation": 1,
    "ondeviceproximity": 1,
    "onmozbeforepaint": 1,
    "onpaint": 1,
    "onunload": 1,
    "onuserproximity": 1,
    "DOMParser": 1,
    "GeckoActiveXObject": 1,
    "QueryInterface": 1,
    "XMLSerializer": 1,
    "XPCNativeWrapper": 1,
    "XPCSafeJSObjectWrapper": 1
}

当我在Chrome中进行一些调试时,我发现有些像screenLeft这样的窗口属性似乎是在Chrome中本机实现的,但不在MDN的列表中。

我能想到的唯一方法是首先执行一个脚本来捕获窗口命名空间中的变量,然后在以后查找不在该列表中的任何内容:

//would have to be the first JS included in your page
getGlobals = (function () {
  var okGlobals = { getGlobals: true }, getGlobals = function() {
    document.getElementById('notOkGlobals').innerHTML = '';
    for (var v in window) {
      //not sure why this is giving me v='v' (seems to be SOs snippet functionality)
      if (!okGlobals[v] && v !== 'v') {
        document.getElementById('notOkGlobals').innerHTML += '<br/>' + v;
      }
    }
  };

  //hydrate with whatever is in window now
  for (v in window) {
    okGlobals[v] = true;
  }

  return getGlobals;
})();

//code executed later that drops something into window
(function() {
  var notAGlobal = 2;
  accidentalGlobal = 2;
})();

//run this any time to see changes from when the page was loaded
getGlobals();
<div id="notOkGlobals"></div>

有没有办法可靠地识别添加到窗口命名空间的变量?

0 个答案:

没有答案