最近再做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; } } }