当前,我正在尝试使用window.open
从渲染器进程中打开外部URL。为此,我创建了nodeIntegration
时设置了nativeWindowOpen
并且true
是BrowserWindow
。如果我使用的电子版本至少为8.x,则可以使用,但电子版7.x会崩溃。
我在下面有一个代码示例:
package.json
{
"name": "window-open-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "electron ."
},
"author": "",
"license": "ISC",
"dependencies": {
"electron": "7.3.2" // If I update to version 8.0.0, It will work.
}
}
index.js
const { app, BrowserWindow, shell } = require("electron");
function startTest() {
let parent_window = new BrowserWindow({
show: true,
title: "parent",
webPreferences: {
nativeWindowOpen: true,
nodeIntegration: true,
// nodeIntegrationInSubFrames: true,
},
});
parent_window.loadFile("parent.html");
parent_window.webContents.on("new-window", (event, url, frameName) => {
event.preventDefault();
shell.openExternal(url);
});
}
app.on("ready", startTest);
parent.html
<body>
Main window
</body>
<script>
window.open('https://google.com');
</script>