Morious
Morious

Reputation: 85

Electron: remove beforeunload event listeners

I have an electron-application that is used to show webpages which I have no control over.
The app is used so a different page can be shown every few seconds.
One of the pages shown attaches an 'beforeunload' listener like so

    window.addEventListener('beforeunload', function(event) {
    event.returnValue="test";
});

This causes electron to fail when loading a new url, so the switching does not work anymore.
This is a known issue: https://github.com/electron/electron/issues/9966
Even worse, is also prevents the whole application from being closed.

Is there anything that can be done from the main process that removes/disables the beforeunload listener, so the switching works again?
To test this, I have a fiddle that shows this behavior:
https://gist.github.com/9a8acc3bf5dface09d46aae36807f6f9

Upvotes: 2

Views: 1061

Answers (1)

AlekseyHoffman
AlekseyHoffman

Reputation: 2694

You can simply prevent this event:

const { BrowserWindow, dialog } = require('electron')
const win = new BrowserWindow({ width: 800, height: 600 })

win.webContents.on('will-prevent-unload', (event) => {
  event.preventDefault()
})

See Electron docs for details

Upvotes: 3

Related Questions