Define all non-local variables in Windows namespace?

I would like to identify all variables in the namespace of the browser window that are missing on page load about:blank

(or empty html page) in order to watch for new libraries or random global changes due to coding errors. How can I reliably define the variables that should be present?

I thought maybe I could use MDNs list of window property / method / handler , which led me to create this whitelist:

{
    "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
}

      

When I did some debugging in Chrome, I saw that there are window properties such as screenLeft

that appear to be natively implemented in Chrome but are not listed from MDN.

The only method I can think of is to execute first a script that commits the variables in the window's namespace, and then look for something not in that list later:

//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>
      

Run codeHide result


Is there a way to reliably identify variables added to the Windows namespace?

+3


source to share





All Articles