Confirm beforeunload in Electron(在 Electron 中确认 beforeunload)
问题描述
我知道有数百个问题,例如:我如何防止电子中的近距离事件"或类似的问题.
在 beforeunload
事件中实现确认框(电子消息框)后,我能够关闭我的应用程序并取消关闭事件.由于开发工具始终处于打开状态,我没有意识到它在开发工具关闭时不起作用...
i know that there are hundreds of questions like: "How can i prevent close event in electron" or something like that.
After implementing a confirmation box (electron message box) in the beforeunload
event i was able to close my app and cancel the close event. Since the dev tools are always open, i didn't recognize that it doesn't work while the dev tools are closed...
window.onbeforeunload = e =>
{
// show a message box with "save", "don't save", and "cancel" button
let warning = remote.dialog.showMessageBox(...)
switch(warning)
{
case 0:
console.log("save");
return;
case 1:
console.log("don't save");
return;
case 2:
console.log("cancel");
return false;
// e.returnValue = "false";
// e.returnValue = false;
}
};
因此,当开发工具打开时,我可以通过保存关闭应用程序,而不保存并取消事件.
关闭开发工具后,取消按钮不再起作用.
So, when the dev tools are opened, i can close the app with saving, without saving and cancel the event.
When the dev tools are closed, the cancel button doesn't work anymore.
顺便说一句:
window.onbeforeunload = e =>
{
return false;
alert("foo");
};
将取消关闭事件,显然不会显示消息(无论开发工具是打开还是关闭)
will cancel the close event and obviously wouldn't show the message (doesn't matter if dev tools are open or closed)
window.onbeforeunload = e =>
{
alert("foo");
return false;
};
如果开发工具打开,按确定后将取消关闭事件,如果开发工具关闭,按确定后将关闭应用程序
will cancel the close event after pressing ok if dev tools are open and will close the app after pressing ok if dev tools are closed
我故意使用消息框的同步 api,在我写这个问题时,我发现一个两个窗口的应用程序 (new remote.BrowserWindow()
) 的行为与使用开发工具.
Intentionally i'm using the synchronous api of the message box and while i'm writing this question i figured out that a two windowed app (new remote.BrowserWindow()
) will behave exactly like with the dev tools.
有人知道我该如何解决这个问题吗?
非常感谢提前
Has anyone an idea how i can resolve this problem?
Many thanks in advance
推荐答案
而不是 onbeforeunload
更喜欢使用事件 close
.从这个事件中,您将能够在整个关闭过程完成之前捕获关闭事件(事件 closed
).使用 close
,您将能够控制并在需要完成关闭时停止.
Instead of onbeforeunload
prefer working with the event close
. From this event, you'll be able to catch the closing event before the whole closure process is completed (event closed
). With close
, you'll be able to take the control and stop whenever you need the completion of the closure.
这在您创建 BrowserWindow 时是可能的,最好是在主进程中:
This is possible when you create your BrowserWindow, preferably in the main process:
// Create the browser window.
window = new BrowserWindow({});
// Event 'close'
window.on('close', (e) => {
// Do your control here
if (bToStop) {
e.preventDefault();
}
})
// Event 'closed'
window.on('closed', (e) => {
// Fired only if you didn't called e.preventDefault(); above!
})
此外,请注意函数 e.preventDefault()
正在整个代码中传播.如果您需要回到 Electron 的自然行为,则需要将变量 e.defaultPrevented
切换为 false
.
In addition, be aware that the function e.preventDefault()
is spreading in the whole code. If you need to be back to the natural behaviour of Electron, you need to toggle the variable e.defaultPrevented
to false
.
实际上,似乎 e.preventDefault()
函数正在将变量 e.defaultPrevented
处理为 true
直到它发生任何变化.
Actually, it seems e.preventDefault()
function is handling the variable e.defaultPrevented
to true
until any change on it.
这篇关于在 Electron 中确认 beforeunload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Electron 中确认 beforeunload
基础教程推荐
- 在for循环中使用setTimeout 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01