当 nativeWindowOpen 在父窗口中为 true 时,electron.js 中 webview 中基于 target=_blank 的链接未打开

2022-01-10 00:00:00 electron javascript webview

我有简单的 electron.js 应用程序,electron-quick-start 我正在使用 <webview>.

I have simple electron.js application, electron-quick-start in which I am using <webview>.

默认情况下,设置了 target=_blank 的链接不会在 webview 内打开.

By default links in which target=_blank is set does not open inside webview.

通过收听 new 有一些解决方法-window 事件在 不能打开目标 = 空白的 Electron webview 链接

但是当 nativeWindowOpen: true in webPreferences 在父窗口中时,此解决方法不起作用.当我在 MacOS 上添加 allowpopups 并出现错误 exited with signal SIGSEGV

But this workaround does not work when nativeWindowOpen: true in webPreferences in parent window. Rather application get crashed when I am adding allowpopups on MacOS with error exited with signal SIGSEGV

const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      webviewTag: true,
      nativeWindowOpen: true // This flag is not letting recieve new-window event from webview
    }
  })

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
    <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <webview id="foo" src="https://www.w3schools.com/tags/att_a_target.asp" style="display:inline-flex; width:640px; height:480px" allowpopups></webview>

    <h1>click on Try it yuorself button in webview</h1>

    <!-- You can also require other files to run in this process -->
    <!-- <script src="./renderer.js"></script> -->
  </body>
</html>

如何在 target=_blank 工作的情况下获取 webview 链接,并在父窗口中保持 nativeWindowOpen: true?

How can I get webview links where target=_blank working keeping nativeWindowOpen: true in parent window?

nativeWindowOpen 将根据 true-changed-nativewindowopen-defaults-to-true" rel="nofollow noreferrer">计划中的重大 API 更改 (15.0)

nativeWindowOpen is going to be set true by default according to Planned Breaking API Changes (15.0)

推荐答案

这是一个错误 https://github.com/electron/electron/pull/29874 已修复.

This was a bug https://github.com/electron/electron/pull/29874 which got fixed.

electron 12.0.14 或更高版本可以很好地解决这个问题.

electron version 12.0.14 or higher is working fine for this problem.

相关文章