You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ignoring or displaying beforeunload prompt out of our control/context #8468
I maintain an Electron app which wraps Google Music (google-music-electron). It provides light integrations with UI hooks for media key support and tray information. We have been on an older version of Electron for a while (0.36.x) and are attempting to upgrade to 1.4.15 but we're running into a snag with the beforeunload behavior introduced in [email protected].
Google Music will display a beforeunload response when music is playing (i.e. "You still have music playing."). This prevent close actions from working until the user stops the music. We have verified that removing the event listener via Dev Tools' window.getEventListeners() works but we have no way of accessing/removing the beforeunload listener normally.
# Open a vanilla Electron window with no application
electron
In the Electron window's Dev Tools, run the following:
// Navigate to Google Musicwindow.location='https://music.google.com/';// Sign in/start playing music// Attempt to close window manually and verify it doesn't do anything and provides no feedback// Remove `beforeunload` listenerswindow.getEventListeners(window).beforeunload.forEach(function(listener){listener.remove();});// Attempt to close window manually and see it works
In a preload script to disable GPM's unload blockers? If you execute that code during the exit event on the BrowserWindow it should remove the listeners before they are called.
@twolfson There is a way to execute devtools only JS from within Electron (I can't find it right now but will continue looking).
You could potentially use a spy technique on window.addEventListener and simply not allow the website to add listeners for beforeunload. The pattern I refer to would look something like.
Yea, I thought about stubbing/spying but it's even dirtier and could cause unexpected breakages if implemented improperly.
With respect to executing the DevTools context, neat and TIL. I think I always assumed that you had to have DevTools to run them but good to know you don't 👍
I think if push comes to shove I'll prob do the "music pause" workaround as it's targeted and won't break other intended functionality. However before doing that I wanted to get Electron's official plans for beforeunload before taking action to prevent doing unnecessary work
Activity
MarshallOfSound commentedon Jan 22, 2017
Merging into #2579
As a work around for now can't you run your code
In a preload script to disable GPM's unload blockers? If you execute that code during the
exitevent on the BrowserWindow it should remove the listeners before they are called.https://github.com/electron/electron/blob/master/docs/api/browser-window.md#event-close
twolfson commentedon Jan 22, 2017
Unfortunately
window.getEventListenersis only accessible within Dev Tools.https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#geteventlistenersobject
We have a couple of workaround options but they are more hacks than appropriate behavior as they're specific to our application:
beforeunloadlistener to pause music and then re-runwindow.close()in case of race conditionsBrowserWindow.on('close')withprocess.exit()which ignores BrowserWindow requests (unlikeapp.quit())MarshallOfSound commentedon Jan 22, 2017
@twolfson There is a way to execute devtools only JS from within Electron (I can't find it right now but will continue looking).
You could potentially use a spy technique on
window.addEventListenerand simply not allow the website to add listeners forbeforeunload. The pattern I refer to would look something like.EDIT:
twolfson commentedon Jan 22, 2017
Yea, I thought about stubbing/spying but it's even dirtier and could cause unexpected breakages if implemented improperly.
With respect to executing the DevTools context, neat and TIL. I think I always assumed that you had to have DevTools to run them but good to know you don't 👍
I think if push comes to shove I'll prob do the "music pause" workaround as it's targeted and won't break other intended functionality. However before doing that I wanted to get Electron's official plans for
beforeunloadbefore taking action to prevent doing unnecessary workMarshallOfSound commentedon Jan 22, 2017
Official plans and such for
beforeunloadwill go through #2579