Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron.js Prevent refresh for created window

I'm trying to disable Cmd+R or F5 for refreshing my electron app like so:

  globalShortcut.register('CommandOrControl+R', () => false);
  globalShortcut.register('F5', () => false);

But, unfortunately, it cause preventing refresh entirely for all frames, even for other browsers.

How can I register such shortcuts only for my created window?

ALTERNATIVE: I guess, we could use Mousetrap as an option for such operation, but I wonder - is there any kinda built in method for such operation?

like image 309
WebArtisan Avatar asked Jul 05 '18 09:07

WebArtisan


1 Answers

This is the most appropriate way to prevent Window refresh. The other approaches don't prevent window.reload() calls.

The ev object hold information about what triggered the unload event, it could be used to tailor the outcome of the event in any way you wish.

window.addEventListener('beforeunload', (ev) => {
      // Setting any value other than undefined here will prevent the window
      // from closing or reloading
      ev.returnValue = true;
    });
like image 52
João Eduardo Avatar answered Oct 03 '22 07:10

João Eduardo