在 Electron 中确认 beforeunload

Confirm beforeunload in Electron(在 Electron 中确认 beforeunload)
本文介绍了在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)