最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to force the closing of an Electron app? - Stack Overflow

programmeradmin0浏览0评论

I have an electron app that loads the URL of the web version of the app. However I cannot close the application by clicking in the X button and I dont have access to the webapp. I have tried this:

let count = BrowserWindow.getAllWindows().length; 
///returns 1, so there is only 1 window)
                        
window.onbeforeunload=function(e){
    e.returnValue=undefined;
    return undefined;
}
window.close();
app.quit();

Nothing happens.

app.exit(0) works, but I dont want to use it. Is there a way I can close the app with window.close and app.quit?

EDIT: The problem are those global beforeunload events. If i click remove in the devtools I am able to close the app.

Also this let names= window.eventNames();returns an array that does not have any beforeunload events

I have an electron app that loads the URL of the web version of the app. However I cannot close the application by clicking in the X button and I dont have access to the webapp. I have tried this:

let count = BrowserWindow.getAllWindows().length; 
///returns 1, so there is only 1 window)
                        
window.onbeforeunload=function(e){
    e.returnValue=undefined;
    return undefined;
}
window.close();
app.quit();

Nothing happens.

app.exit(0) works, but I dont want to use it. Is there a way I can close the app with window.close and app.quit?

EDIT: The problem are those global beforeunload events. If i click remove in the devtools I am able to close the app.

Also this let names= window.eventNames();returns an array that does not have any beforeunload events

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 10, 2019 at 17:51 kkicakkica 4,1041 gold badge22 silver badges41 bronze badges 4
  • Wait, are you using window global (renderer) inside electron (main) code? – Lukas Bobinas Commented Jul 10, 2019 at 19:29
  • can you just post the repo link? – Lukas Bobinas Commented Jul 10, 2019 at 19:33
  • I am creating a BrowserWindow and then loading a url in that window. I cant post the link :( – kkica Commented Jul 10, 2019 at 19:38
  • 1 I'm no JavaScript hero but I think using window as a variable name is a "bad idea". – spring Commented Jul 11, 2019 at 1:54
Add a ment  | 

2 Answers 2

Reset to default 1

In the file where you created the BrowserWindow, you can attach an event to the 'close' handler for the window. For Electron, this is usually main.js.

const {app, BrowserWindow} = require('electron');
let mainWindow = null;
app.on('ready', () => {
    mainWindow = new BrowserWindow({
       //options here
    });
    mainWindow.loadURL([url here]);

    //attach event handler here
    mainWindow.on("close", () => {
       mainWindow = null;
       app.quit();
    });
});

For good measure, in the case that you may have more than one window, place this in your main.js file.

app.on('window-all-closed', () => {
     if(process.platform !== "darwin")
         app.quit();
});

You need to save the BrowserWindow object reference. Then listen for a 'closed' event and dereference it:

const { app, BrowserWindow } = require('electron');

let win; // = new BrowserWindow();

win.on('closed', () => {
  win = null
})

After dereference, it's all simple, just listen for 'window-all-closed' event on app:

app.on('window-all-closed', () => {
  app.quit()
})
发布评论

评论列表(0)

  1. 暂无评论