Search code examples
javascripthtmlelectronwindow.openmaximize-window

How to maximize every `window.open` call in Electron?


I've known that we could use the following code to maximize the main window:

const mainWindow = new BrowserWindow({...})

mainWindow.maximize()

But what about the window.open calls from within the main window? How to maximize them as well?

Using new BrowserWindow({ fullscreen: true }) is not an option, since that would cause the opened windows to have no title bar.


Solution

  • You just need to intercept the window creation and maximize them there (this will also affect windows opened from clicking on hyperlinks with target="_blank")

    Assuming you're on Electron 12 or higher:

    mainWindow.webContents.on('did-create-window', win => win.maximize())
    

    As for the second part of the question in your comment, you'll want to hide the window before it's ready to show:

    mainWindow.webContents.setWindowOpenHandler(() => ({
      action: 'allow',
      overrideBrowserWindowOptions: { show: false },
    }))
    
    mainWindow.webContents.on('did-create-window', win =>
      win.once('ready-to-show', () => win.maximize()),
    )