MZ Title menu link

Hello,

I want to add an entry in the title menu to open an URL, using the player's browser. I have the code to do that, but when I open the URL, I cannot interact anymore with the game, as if I lost the focus. I can still refresh the game with F5, I can still here the main menu music playing, but I can't scroll, or anything in the menu.

JavaScript:
(function () {

    var commandList = Window_TitleCommand.prototype.makeCommandList;

    Window_TitleCommand.prototype.makeCommandList = function () {
        commandList.call(this);
        this.addCommand("CLICK ME", 'openURL');
    };

    var commandWindow = Scene_Title.prototype.createCommandWindow;

    Scene_Title.prototype.createCommandWindow = function () {
        commandWindow.call(this);
        this._commandWindow.setHandler('openURL', this.commandOpenURL.bind(this));
    };

    Scene_Title.prototype.commandOpenURL = function () {
        require('nw.gui').Shell.openExternal('https://example.com/');
    };

})();

There may be a subtlety that I did not understand.
Spoiler
Have you tried using the native open method instead? E.g.
JavaScript:
Scene_Title.prototype.commandOpenURL = function() {
  window.open("https://example.com");
};
More info:

Edit: no sorry, I just tested and got the same freeze effect. 😴 Doing more testing...

Edit 2: OK, I'm not sure why it freezes, but this sort of thing seems to work:
JavaScript:
Scene_Title.prototype.commandOpenURL = function() {
    const url = "https://forums.rpgmakerweb.com";
    if (Utils.isNwjs()) require('nw.gui').Shell.openExternal(url);
    else open(url);
    SceneManager.goto(SceneManager._scene.constructor);
};
I.e. go to "this scene" immediately afterwards. I guess making the window lose focus mid-loop might be interrupting the game loop in some way.

Edit 3: better solution + explanation by casper below! 👋
It works like a charm, thank you !
Just wanted to add that the "freeze" happens because when OK input is processed, the window is deactivated by default. The game isn't frozen, the window is just deactivated and not accepting inputs which might give the appearance of a freeze. You can see this in Window_Selectable.prototype.processOk:

JavaScript:
Window_Selectable.prototype.processOk = function() {
    if (this.isCurrentItemEnabled()) {
        this.playOkSound();
        this.updateInputData();
        this.deactivate();
        this.callOkHandler();
    } else {
        this.playBuzzerSound();
    }
};

All you should need to get around this is
JavaScript:
this._commandWindow.activate();

For example:

JavaScript:
Scene_Title.prototype.commandOpenURL = function () {
    require('nw.gui').Shell.openExternal('https://example.com/');
    this._commandWindow.activate();
};

Although as caethyril has done you should really check if you are in an environment where "require" is defined or else it'll crash (such as on mobile/web), and use the native open as a backup so the full code would be

JavaScript:
Scene_Title.prototype.commandOpenURL = function() {
    const url = "https://forums.rpgmakerweb.com";
    (Utils.isNwjs()) ? require('nw.gui').Shell.openExternal(url) : window.open(url);
    this._commandWindow.activate();
};
Thank you for this explanation !

Original thread: https://forums.rpgmakerweb.com/threads/title-menu-link.140624/