Advertisement
NonplayerCharacter

JS | List global variables

Apr 8th, 2022
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://stackoverflow.com/a/52693392/10559302
  2. /*
  3. It filters Object.keys(window) based on three principles:
  4.  
  5. 1. Things that are null or undefined are usually not interesting to look at.
  6. 2. Most scripts will define a bunch of event handlers (i.e. functions) but they are also usually not interesting to dump out.
  7. 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.
  8. */
  9. Object.keys(window).filter(x => typeof(window[x]) !== 'function' &&
  10.   Object.entries(
  11.     Object.getOwnPropertyDescriptor(window, x)).filter(e =>
  12.       ['value', 'writable', 'enumerable', 'configurable'].includes(e[0]) && e[1]
  13.     ).length === 4)
  14.  
  15. /*
  16. An interactive way to 'search' would be opening the devtools (e.g. in chrome) typing window + Enter and then click the triangle to expand the object tree.
  17. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement