djk*_*ato 4 javascript node.js electron
我是 Electron 的新手,我真的一直在努力让它工作。我遇到了无法解释的行为,所以总结一下:我无法使 Electron 和 html 之间的通信正常工作
“未捕获的 ReferenceError:需要未定义”在网站内,即使我有 nodeIntegration:true
File Tree:
./
index.html
index.js
package-lock.json
package.json
node_modules/
Run Code Online (Sandbox Code Playgroud)
索引.js:
const electron = require("electron");
const Ffmpeg = require("fluent-ffmpeg");
const CmdExec = require('child_process');
const {
app,
BrowserWindow,
ipcMain
} = electron;
function createWindow() {
//If I put the main window ini into here, and then call app.on("ready", createWindow()); app says
//"Cant create window before ready", even though I just moved the funcion from inside ready to here..
}
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true
}
});
mainWindow.loadURL(`${__dirname}/index.html`);
});
ipcMain.on("video:submit", (event, path) =>{
CmdExec.exec("echo hello", (value)=>{console.log(value)});
});
Run Code Online (Sandbox Code Playgroud)
html:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="">
</head>
<body>
<h1>WELCOME!</h1>
<script src="" async defer></script>
<form action="">
<div>
<br>
<label for=""></label>
<input type="file" accept="video/*" name="" id="">
</div>
<button type="submit">get info</button>
</form>
<script>
const electron = require("electron");
electron.send('perform-action', args);
document.querySelector("form").addEventListener("submit", (event) => {
event.preventDefault();
const {path} = document.querySelector("input").files[0];
window.api.send("video:submit", path);
});
//Tried multiple methos Ive found on stackoverflow,, dont think I implemented them right
//though
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
包.json:
{
"name": "media_encoder",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"electron": "electron ."
},
"author": "",
"license": "ISC",
"dependencies": {
"electron": "^12.0.0"
}
}
Run Code Online (Sandbox Code Playgroud)
Electron 12 现在默认contextIsolation为true,这会禁用 Node(这里是发行说明;这里是PR)。
以下是有关此更改的讨论。nodeIntegration因为它的价值将在未来的 Electron 版本中删除。
解决此问题的最简单方法是简单地禁用上下文隔离:
mainWindow = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
Run Code Online (Sandbox Code Playgroud)
话虽如此,contextIsolation出于安全原因,您可能需要考虑保持启用状态。请参阅此文档,解释为何此功能可增强应用程序的安全性。
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |