showMessageBox before unload?


I’m working on a Markdown editor, called WriteDown. I’d like to check if the file has been saved onbeforeunload and choose accordingly to user input. My current code is below.

window.onbeforeunload = function(e) {
  if ( input.value != fs.readFileSync(input.dataset.filePath, 'utf8') ) {
    dialog.showMessageBox(remote.getCurrentWindow(), {
      type: "warning",
      buttons: ['Cancel', 'Save', 'Don\'t Save'],
      defaultId: 0,
      title: "Close without saving?",
      message: "Are you sure you want to close WriteDown without saving your work?",
      cancelId: 0
    }, function(index) {
      if ( index == 1 ) {

      if ( index !== 0 ) {
    return false

Now, the code above works, but only to some degree. When the user clicks “Cancel,” the message box is hidden and WriteDown stays open. When the user clicks “Save,” the file is saved and WriteDown is closed. However, when clicking “Don’t Save,” things don’t quite work right.

The message box is closed momentarily and then reopened, and WriteDown stays open. As far as I can tell, remote.getCurrentWindow().close() is attempting to close the message box as it for some reason has not already closed. Considering that it works as expected for “Cancel” and “Save,” I’m a bit confused.

Thanks in advance!


So, could anyone give me a few pointers for getting this working? Even a general area to look in the code of another program such as Atom would be useful.