javascriptelectronelectron-builderelectron-packager

Open a link in electron created a new browser window How to Apply set Browser window properties on that window?


I have one main Electron window which is loaded by loadURL. In that window we a different a-tag which automatically created a new BrowserWindow with default setting. how can we change the Browserwindow properties on that window like.

new BrowserWindow({
 width: width,
 show: true,
 height: height,
 frame: false,
 transparent: true,
}

Solution

  • You need to use the setWindowOpenHandler method to intercept the window launch and configure the properties:

    mainWindow.webContents.setWindowOpenHandler((details) => {
        return {
            action: "allow",
            overrideBrowserWindowOptions: {
                width: 100,
                frame: false,
                // etc.
            }
    });
    

    Note this handler will fire for all windows opened from your main window, so you might want to check the details.disposition property or figure out in some other way if you want to mess with the window configuration (e.g. window.open will also trigger this)