【问题标题】:Can't open Electron webview links with target = blank无法打开目标 = 空白的 Electron webview 链接
【发布时间】:2018-08-01 13:02:52
【问题描述】:

我正在使用 Electron 我有一个显示外部网站的 web 视图,但我无法成功显示通常由该网站上的链接打开且目标 = _blank 的附加窗口。

<a href="mentions.html" target="_blank">Mentions légales </a> 

我试过了

webpreferences="nativeWindowOpen=yes" allowpopups

但它没有改变。

【问题讨论】:

    标签: webview window electron


    【解决方案1】:

    使用 webview,您实际上可以很容易地在主进程中处理这些。

    如果需要,它还允许您禁用 nodeIntegration。

    // Listen for web contents being created
    app.on('web-contents-created', (e, contents) => {
    
      // Check for a webview
      if (contents.getType() == 'webview') {
    
        // Listen for any new window events
        contents.on('new-window', (e, url) => {
          e.preventDefault()
          shell.openExternal(url)
        })
      }
    })
    

    【讨论】:

    • 如果有人正在寻找如何将此事件附加到 BrowserView:browserView.webContents.on('new-window', (e, url) =&gt; { /* handle window open */ });
    【解决方案2】:

    在深入研究文档后,我编写了这段代码(代码位于渲染器中):

    const {BrowserWindow} = require('electron').remote
    

    .......

     webview1.addEventListener('new-window', (e) => {
        const protocol = require('url').parse(e.url).protocol
        if (protocol === 'http:' || protocol === 'https:') {
          //shell.openExternal(e.url)
          let win = new BrowserWindow({width: 800, height: 600})
          win.loadURL(e.url);
        }
      })
    

    shell.openExternal(e.url) 行在默认浏览器的选项卡中打开链接的 url。 通过使用新的 BrowserWindow,新的窗口就是 Electron Window。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2012-05-08
    • 2016-03-19
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    相关资源
    最近更新 更多