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