Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tangoabcdelta/77a1c7bde45c784ba145355422c3189a to your computer and use it in GitHub Desktop.

Select an option

Save tangoabcdelta/77a1c7bde45c784ba145355422c3189a to your computer and use it in GitHub Desktop.
JS Snippets

To isolate user-defined properties in the window object, you can compare the current keys of window against a "clean" iframe context. This effectively filters out all browser-native APIs and built-ins.

Here is a concise snippet you can paste directly into your console:

((win) => {
  const iframe = document.createElement('iframe');
  iframe.style.display = 'none';
  document.body.appendChild(iframe);
  const nativeProps = Object.getOwnPropertyNames(iframe.contentWindow);
  document.body.removeChild(iframe);

  const userProps = Object.getOwnPropertyNames(win)
    .filter(prop => !nativeProps.includes(prop));

  console.log("User-defined properties:", userProps);
  userProps.forEach(p => console.log(p, '=>', win[p]));
})(window);

How it works:

  • The Sandbox: It creates a temporary, empty iframe which contains a pristine window object. Any property present in this frame is considered "native" to the environment.
  • The Filter: It gets all property names from the actual window and filters out anything that exists in the clean iframe context.
  • Safety: By logging the keys first and then accessing the values, it avoids the JSON.stringify circular reference trap while still allowing you to inspect the values directly in the console.

Tips for usage:

  • Scope: If you are running this on a page with heavy security headers (CSP), ensure the iframe creation is permitted.
  • Dynamic Data: If a library attaches properties lazily, you may want to wrap this in a setTimeout if you suspect the script hasn't finished loading yet.
/**
* js snippet that gives you all the user deffined properties in the global window context for a particular site which I can paste in console and find out the details of
* I don't want to get into taht circular reference dilemma because JSON.stringify wont work there
* I want something small, concise, clean, easy to remember (after few retries of course) that does this
*
* note - It should only print the user defined ones, not the built in ones
*/
((win) => {
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
const nativeProps = Object.getOwnPropertyNames(iframe.contentWindow);
document.body.removeChild(iframe);
const userProps = Object.getOwnPropertyNames(win)
.filter(prop => !nativeProps.includes(prop));
console.log("User-defined properties:", userProps);
userProps.forEach(p => console.log(p, '=>', win[p]));
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment