最近再作electron app程序的作删除数据操做的时候遇到一个诡异的bug,页面点击删除按钮后,弹出确认对话框后,页面失去焦点,文本框没法点击输入任何参数,可是使用浏览器操做正常,最后肯定是electron的bug,electron在弹出window默认对话框时会失去焦点,在githup上找到的解决方案是本身实现对话框覆盖window自带对话框,个人作法是覆盖window自带的alert和confirm方法,很少说了,如今贴代码。git
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/20400github