我正在使用Electron,我有一个webview用于显示外部网站,但我无法成功显示由该网站上的链接通常打开并且目标为_blank的额外窗口。
<a href="mentions.html" target="_blank">Mentions légales </a>
我尝试了一下
webpreferences="nativeWindowOpen=yes" allowpopups
但是它没有改变。
使用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)
})
}
})
在深入研究文档后,我编写了以下代码(位于渲染器中):
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窗口。
browserView.webContents.on('new-window', (e, url) => { /* handle window open */ });
- solo