Skip to content

Ignoring or displaying beforeunload prompt out of our control/context #8468

Closed

Description

@twolfson
  • Electron version: 1.4.15
  • Operating system: Linux Mint 17.1

Background

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.

Reference issue: twolfson/google-music-electron#44

We were wondering if there were a way to/can we add a feature to either:

  • Ignore the beforeunload event via an option
  • Display the beforeunload action like in Chrome
  • Something else that allows the user to close the window when music is playing

For reference, we asked on Slack but we didn't receive any responses =/

https://atomio.slack.com/archives/electron/p1484801505004057

How to reproduce

Run the following steps:

# Open a vanilla Electron window with no application
electron

In the Electron window's Dev Tools, run the following:

// Navigate to Google Music
window.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` listeners
window.getEventListeners(window).beforeunload.forEach(function (listener) {
  listener.remove();
});

// Attempt to close window manually and see it works

Activity

  1. MarshallOfSound commented on Jan 22, 2017

    @MarshallOfSound
    Member

    Merging into #2579

    As a work around for now can't you run your code

    window.getEventListeners(window).beforeunload.forEach(function (listener) {
      listener.remove();
    });

    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.

    https://github.com/electron/electron/blob/master/docs/api/browser-window.md#event-close

    Emitted when the window is going to be closed. It's emitted before the beforeunload and unload event of the DOM.

  2. twolfson commented on Jan 22, 2017

    @twolfson
    ContributorAuthor

    Unfortunately window.getEventListeners is 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:

    • Add our own beforeunload listener to pause music and then re-run window.close() in case of race conditions
      • Specific to our application because of music bindings
    • Use BrowserWindow.on('close') with process.exit() which ignores BrowserWindow requests (unlike app.quit())
      • Specific to our application because some apps could have multiple windows
  3. MarshallOfSound commented on Jan 22, 2017

    @MarshallOfSound
    Member

    @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.

    const originalAddEventListener = window.addEventListener;
    window.addEventListener = (eventName, ...args) => {
      if (eventName === 'beforeunload') return;
      return originalAddEventListener(eventName, ...args);
    };

    EDIT:

    require('electron').remote.getCurrentWindow().devToolsWebContents.executeJavaScript
  4. twolfson commented on Jan 22, 2017

    @twolfson
    ContributorAuthor

    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

  5. MarshallOfSound commented on Jan 22, 2017

    @MarshallOfSound
    Member

    Official plans and such for beforeunload will go through #2579

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions