electron 中拦截内嵌页面 beforeunload 的弹窗提示

window 的 beforeunload 事件提示在electron 不兼容,弹窗提示不出来,还会导致莫名其妙的假死问题,下面记录一下解决方法。

1. 如果仅需要拦截弹窗:

 win.webContents.on('will-prevent-unload', (event) => {
    event.preventDefault(); // 忽略 beforeunload 事件
  });

2. 如果需要弹窗提示:

  win.webContents.on('will-prevent-unload', (event) => {
    const choice = dialog.showMessageBoxSync(win, {
      type: 'question',
      buttons: ['Leave', 'Stay'],
      title: 'Do you want to leave this site?',
      message: 'Changes you made may not be saved.',
      defaultId: 0,
      cancelId: 1,
    });
    if (choice === 0) {
      event.preventDefault(); // 忽略 beforeunload 事件
    }
  });
}

以上。

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-11 09:08:08       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-11 09:08:08       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-11 09:08:08       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-11 09:08:08       20 阅读

热门阅读

  1. Lua 数字格式化

    2024-05-11 09:08:08       11 阅读
  2. 神经网络的偏见

    2024-05-11 09:08:08       10 阅读
  3. BS架构和CS架构的区别

    2024-05-11 09:08:08       11 阅读
  4. uni-app小知识点记录

    2024-05-11 09:08:08       10 阅读
  5. 【DL】FocalLoss的PyTorch实现

    2024-05-11 09:08:08       13 阅读
  6. IPsec协议:保障网络通信的安全利器

    2024-05-11 09:08:08       11 阅读