MV Is there any way to detect the game being closed or minimized?

I would like to know if there is a plugin that indicates whether the game has been closed or minimized and can account for this in some way, for example

The game asks "please let me free close the game" and I close the game when I open it and load it and the mission continues, and something happens with this action of closing the game or at least minimizing it, I've searched the entire internet and I can't find anything that can help me :asad:
For standalone deployments you should be able to do something like this on boot (e.g. in a plugin):
JavaScript:
if (Utils.isNwjs()) {
  var onClose = function() { if (confirm("Quit the game?")) nw.App.quit(); };  // confirm
  var onSmall = function() { $gameSwitches.setValue(1, true); };  // turn on switch 1
  var w = nw.Window.get();
  w.on("close", onClose);
  w.on("minimize", onSmall);
}
This might be helpful:

(For MZ users interested in this: MZ already assigns a handler to the close event, which invokes nw.App.quit(), skipping all subsequent handlers. You can remedy this by calling nw.Window.get().removeAllListeners("close") first, then attach your custom handler.)
For standalone deployments you should be able to do something like this on boot (e.g. in a plugin):
JavaScript:
if (Utils.isNwjs()) {
  var onClose = function() { if (confirm("Quit the game?")) nw.App.quit(); };  // confirm
  var onSmall = function() { $gameSwitches.setValue(1, true); };  // turn on switch 1
  var w = nw.Window.get();
  w.on("close", onClose);
  w.on("minimize", onSmall);
}
This might be helpful:

(For MZ users interested in this: MZ already assigns a handler to the close event, which invokes nw.App.quit(), skipping all subsequent handlers. You can remedy this by calling nw.Window.get().removeAllListeners("close") first, then attach your custom handler.)

Thank you very, very, very much, I did some tests here, and it worked really well, it worked really well, I'm really very grateful, what name could I put in the special thanks credits? 🙂🙂
Great! You can credit me as Caethyril if you like. 👋

Original thread: https://forums.rpgmakerweb.com/threads/is-there-any-way-to-detect-the-game-being-closed-or-minimized.175015/