Is there a way to block popups?


#1

Hello,
I started playing with Electron and I’m not able to find a way to manage popups:
When a webpage opens a popup (window.open), electron will create a window for it.

I want to be able to control/block the “popup” creation if needed.

On CEF, I was using OnBeforePopup from the CefLifeSpanHandler.

Is there a way in Electron to control that ?

Thanks,


#2

Hello,

In a event Webview, you can to control action of open new Window

webview[0].addEventListener('new-window', (res) => {
                    ...
})

You can allow too for webview allowed all popup


#3

Hello,
Thanks for your help. I don’t work with webview but directly with the main BrowserWindow, but your code let me a clue: https://github.com/electron/electron/blob/master/docs/api/web-contents.md#event-new-window

 win.webContents.on('new-window', function(e, url) {
    logData("stop popup", e)
    logData("stop popup", url)
    e.preventDefault();
  });

Seems to work :).