electron嵌入网页
一、创建项目
1. 创建mian.js
代码如下(示例):
// main.js
const electron = require("electron")
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const globalShortcut = electron.globalShortcut
// 声明要打开的窗口
let mainWindow = null
app.on("ready",()=>{
mainWindow = new BrowserWindow({
width:800,
height:800,
webPreferences:{
nodeIntegration:true, //开启node接口
contextIsolation:false,
webSecurity: false,
}
});
// 打开新窗口 BrosweWindow 初始化
require('@electron/remote/main').initialize();
require("@electron/remote/main").enable(mainWindow.webContents);
// 打开调试工具
mainWindow.webContents.openDevTools();
// mainWindow.loadFile("index.html");
mainWindow.loadFile("./html/index3.html"); // 在浏览器打开网页
// 嵌入网页
// BrowserView 主线程模块,在主进程中使用
let view = new BrowserView()
mainWindow.setBrowserView(view);
view.setBounds({
x:0,
y:120,
width:400,
height:400
})
view.setBackgroundColor("#F5F5F5");
})
// 当所有窗口被关闭了,退出。
app.on('window-all-closed', function() {
if (process.platform != 'darwin') {
app.quit();
}
});
2. 创建页面 html
index.html
代码如下(示例):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>内嵌网页</title>
</head>
<body>
<webview id="foo" src="https://www.github.com/" style="display:inline-flex; width:640px; height:480px"></webview>
</body>
</html>