Help with script sandboxing open window timer

I have the next piece of code (the url is an example), and it opens to another website outside the game thanks to Chromium.

JavaScript:
window.win = window.open('https://www.rpgmakerweb.com/');
$gameSwitches.setValue(34,true)
if(win.closed) {
$gameSwitches.setValue(34,false)}

Switch #34 is created in order to Start a Timer coutdown of 90 seconds, created like the ones of https://kokororeflections.com/countdown-timer-in-rpg-maker-ace-mv/


I believe, as script is sandboxed, when the script ends without knowing if switch window.win is closed, timer doesn't stop.

Is there any way to make window.win a global switch in order to stop the timer when it is closed?

It certainly sounds like a tongue twister.

It would help me the same if window.open could be save as a gameSwitch/gameVariable. Is it possible?
I think this is a timing issue: you want the switch to turn off when the window is closed, like an event trigger. What you've currently got, though, is something that checks whether the window is closed immediately after you've opened it. 😅

I had a little look at this page:
Based on that, I tried the following in a Script command:
JavaScript:
window.win = window.open('https://www.rpgmakerweb.com/');
window.win.addEventListener('unload', function(event) {
  alert('Test: second window closed!');
});
...but it doesn't seem to work as I'd hoped. 😢

An alternative could be to check the closed flag directly, using Conditional Branch > Script:
JavaScript:
window.win.closed === true
This should return true iff the second window is closed at the time of the check. (Technically you don't need the "=== true" bit, but I've left it in there anyway for clarity.) You could then have the game wait for it (in series or parallel depending on the event trigger) by using Loop/Break Loop commands, e.g.
Code:
◆Loop
  ◆Comment:Check every few frames
  ◆Wait:10 frames
  ◆If:Script:window.win.closed === true
    ◆Comment:Window closed, proceed!
    ◆Break Loop
    ◆
  :End
  ◆
:Repeat Above
An alternative could be to check the closed flag directly, using Conditional Branch > Script:
JavaScript:
window.win.closed == true
This should return true if the second window is closed at the time of the check. (Technically you don't need the "=== true" bit, but I've left it in there anyway for clarity.)
This piece seems what i am looking for, but it is truly necessary to use a parallel event?

Isn't there a way to make window.win global in order to check it?
I'm not sure what you mean. For an RPG Maker MV game, window is the global object, so win (a top-level property of window) is a global variable, accessible from anywhere within the game. Unless I'm missing something, though, that doesn't include a "trigger" for when one of its properties changes value. 😅

As mentioned, I tried to set up such a trigger, i.e. a DOM event listener, but I couldn't identify the correct setup (maybe someone else knows more about it and can help there). Thus my alternative suggestion: set up your own "listener" using RPG Maker event commands.

For the workaround I suggested, if you want the player to be able to move and act while the second window is open then you will need a Parallel event; otherwise you could use any event trigger you like. Parallel events (especially simple ones like this) are generally not bad for performance if set up correctly. 🙂

(A common example of "incorrect" event setup is having a Loop that's supposed to check something over multiple frames, but has no Wait commands. This will cause the game to slow to a crawl or freeze up until the loop ends. 👋)
Code:
window.win = window.win.open('url');
$gameSwitches.setValue(34,true)
if(win.closed) {
$gameSwitches.setValue(34,false)}

This is in a script box of what i want ($game switches are a timer)... but a script box doesn't make win global... only during the lapse of that script box...

Original thread: https://forums.rpgmakerweb.com/threads/help-with-script-sandboxing-open-window-timer.121988/