electron app弹出默认对话框后页面失去焦点问题

       最近再做electron app程序的做删除数据操作的时候遇到一个诡异的bug,页面点击删除按钮后,弹出确认对话框后,页面失去焦点,文本框无法点击输入任何参数,但是使用浏览器操作正常,最后确定是electron的bug,electron在弹出window默认对话框时会失去焦点,在githup上找到的解决方案是自己实现对话框覆盖window自带对话框,我的做法是覆盖window自带的alert和confirm方法,不多说了,现在贴代码。

var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf(' electron/') > -1){
            const { dialog } = require('electron').remote;//修改默认对话框,修复electron弹出默认对话框后页面失去焦点的bug
            alert = function(str){
                  var options = {
                    type: 'warning',
                    buttons: ["确定"],
                    defaultId: 0,
                    cancelId:0,
                    detail:str,
                    message: ''
                  }
                  dialog.showMessageBoxSync(null,options)
            }
            confirm = function(str){
                  var options = {
                    type: 'warning',
                    buttons: ["确认","取消"],
                    defaultId: 0,
                    cancelId:1,
                    detail:'',
                    message: str
                  }
                  var flag = dialog.showMessageBoxSync(null,options);
                  if(flag==0){
                      return true;
                  }else{
                      return false;
                  }
           }
}

参考资料:https://github.com/electron/electron/issues/20400

原文地址:https://www.cnblogs.com/yinliang/p/12175076.html