Accessing browser window variables

I'm trying to access a global variable that exists in a browser window (not in KM).
For example, in Chrome's console, I can access the variable TWA, but doing that through the 'Execute JavaScript in Front Browser action it's always undefined. I also tried 'document.TWA' and 'window.TWA', but they are always undefined.


Is this not possible with KM?

You need to use document.kmvar.variableName that is mentioned in the guide
[actions:Execute a JavaScript in Browser [Keyboard Maestro Wiki]]

eg

I understand that the KM variables are contained in document.kmvar. But I'm trying to access a global variable.
From the Chrome console of the site I can access the variables as 'TWA', global.TWA or window.TWA. I need to look up some information in that variable to make my KM macro work.

Unfortunately when trying to access 'window', the browser returns only the attributes that exist on the Window object defined by Chrome and not any global variables set via the site's JS code.
e.g. window.devicePixelRatio or just devicePixelRatio returns 2 (as expected) but window.TWA is undefined and alert(window.hasOwnProperty('devicePixelRatio')) is true, while alert(window.hasOwnProperty('TWA')) is not.

Any idea how to access custom global vars via KM?

The reason why TWA cannot be accessed is because it is not a global variable. It is not defined by var but defined by const keyword.

<script>
    const TWA = "my data";  // This cannot be accessed by KM
    var TTA = "another data"; // this is global variable, accessible by window.TTA
</script>

Chrome console can access TWA because it is a developer console having privileged access to all top-level variables (including those declare by let, const) defined in script tag. KM can only access global variables as far as I know.

I see. Oddly enough it's declared as a const but it is also assigned to window:

<script>
  const TWA = {...}
  window.TWA = TWA
</script>

Thank you for the clarification.