以电子方式加载网站和html文件



所以我是电子的新手,在我的第一个项目中,我决定为浏览器游戏做一个简单的客户端。本质上,我想做的是加载网站,然后在上面添加一些HTML,以添加某些功能,如关闭/最小化/最大化,以及一个不是默认窗口的导航栏。

我遇到的问题是,我似乎无法加载网站,然后加载上面的HTML(如果可能的话(。有人能帮我吗?

const { app, BrowserWindow /* ipcMain */ } = require('electron')
const path = require('path')
function createWindow () {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
frame: false,

webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadURL('https://bapbap.gg')
}

/*win.loadFile('index.html')*/
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
/*
ipcMain.on('close', () => {
app.quit()
})
*/
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})

这是可能的。

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>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
<button>Minimize</button>
<button>Maximize</button>
<button>Close</button>
</div>
<webview src="https://bapbap.gg"></webview>
</body>
</html>

index.js

const { app, BrowserWindow, /* ipcMain */ 
webContents} = require("electron");
const path = require("path");

function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
frame: false,
webPreferences: {
webviewTag: true,
},
});
win.loadFile("index.html");
}
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
/*
ipcMain.on('close', () => {
app.quit()
})
*/

style.css

body {
display: flex;
flex-direction: column;
margin: 0;
height: 100vh;
}
webview {
flex: 1;
}

最新更新