gpt4 book ai didi

python - 无法读取 Javascript 文件 Electron JS 中未定义的属性 'join'

转载 作者:行者123 更新时间:2023-12-03 12:30:58 26 4
gpt4 key购买 nike

我在 Electron JS 中有一个应用程序调用 python 函数来执行 python 脚本。当脚本执行时,它应该将数据发送回 Electron JS GUI 并显示它。

我遇到的问题是它说连接未定义:

weather.js:9 Uncaught TypeError: Cannot read property 'join' of undefined at get_weather (weather.js:9) at HTMLButtonElement.onclick (weather.html:14)

这是我的 JavaScript 文件:

let {PythonShell} = require('python-shell')
var path = require("path")

function get_weather() {

var city = document.getElementById("city").value

var options = {
scriptPath : path.join(__dirname, '/../engine/'),
args : [city]
}

let pyshell = new PythonShell('weatherApp.py', options);


pyshell.on('message', function(message) {
swal(message);
})
document.getElementById("city").value = "";
}

“scriptPath : path.join(__dirname, '/../engine/')”这一行似乎是有问题的代码。

我的gui.html文件如下:

<html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>

<h1>Get your local weather ...</h1>
<br>
<br>
<label>Enter city name here: <label>
<input id="city" type = "text" placeholder="City">
<button type = "button" value="clickme" onclick="get_weather()">Get Weather</button>
<!--- <button class="btn btn-success" onclick="get_weather();">Go!</button> -->
<br>
<br>
<br>
<script src="/home/ironmantis7x/Documents/BSSLLC/projects/node_electron/electronDemoApps/guiApp/gui/linkers/weather.js"></script>
<p><button type="button"><a href="gui.html">Back to Main Page</a></button>

</body>
</html>

我需要修复哪些错误才能使其正常工作?

谢谢。

最佳答案

问题

自从 Electron 5 nodeIntegration 在窗口中默认被禁用。由于普通浏览器 API 不知道 requirejoin,因此您在尝试时会出错。

重新启用节点集成

您可以再次启用 nodeIntegration,但由于某种原因它被禁用了。请务必阅读并理解 electron security tutorial .

使用预加载脚本

另一种方法是使用预加载脚本。让我们看一下 BrowserWindow documentation .

当创建一个新的 BrowserWindow 时,您可以添加几个选项。对于这种情况,我们需要 webPreferences.preload 选项:

Specifies a script that will be loaded before other scripts run in the page. This script will always have access to node APIs no matter whether node integration is turned on or off. The value should be the absolute file path to the script. When node integration is turned off, the preload script can reintroduce Node global symbols back to the global scope.

请注意,预加载脚本是在渲染器进程中运行的。

示例

以下是一个示例应用程序,它打开一个带有按钮的窗口,该按钮使用 Electron 对话框来选择文件。这不适用于禁用的 nodeIntegration 但由于我们的预加载脚本,我们将 dialog.showOpenDialog() 重新引入了我们的窗口。

main.js

const { app, BrowserWindow } = require("electron");
const { join } = require("path");

let win;

app.on("ready", () => {
win = new BrowserWindow({
webPreferences: {
//this is the default since electron 5
nodeIntegration: false,
//here you load your preload script
preload: join(__dirname, "preload.js")
}
});

win.loadURL(join(__dirname, "index.html"));
});

preload.js

const { dialog } = require("electron").remote;

window.mystuff = {
selectFile
};

async function selectFile() {
const files = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"]
});

return files;
}

index.html

<html>
<body>
<main>
<button onclick="myFunction()">select file</button>
<ul id="foo"></ul>
</main>
<script>
async function myFunction() {
//the function provided by the preload script
const files = await window.mystuff.selectFile();

const list = document.getElementById("foo");

for (const file of files) {
const node = document.createElement("LI");
const textNode = document.createTextNode(file);
node.appendChild(textNode);
list.appendChild(node);
}
}
</script>
</body>
</html>

通过 IPC 发送事件

如果您不确定您的功能是否应该在窗口中公开,您也可以通过 ipcRenderer 发送事件。

preload.js

const { ipcRenderer } = require("electron");


window.mystuff = {
selectFile
};


function selectFile() {
return new Promise(resolve => {
ipcRenderer.on("selected-files", (e, files) => {
resolve(files);
});

ipcRenderer.send("select-files");
});
}

main.js 中的附加部分


ipcMain.on("select-files", async () => {
const files = await dialog.showOpenDialog({
properties: ["openFile", "multiSelections"]
});

win.webContents.send("selected-files", files);
});

关于python - 无法读取 Javascript 文件 Electron JS 中未定义的属性 'join',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57546385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com