简体   繁体   English

如何使用 Electron-React 应用程序运行外部脚本

[英]How to run external script using an Electron-React app

I am trying to write a desktop app using Electron (with React).我正在尝试使用 Electron(使用 React)编写桌面应用程序。

When a user clicks on a button in the desktop app it will run an external Node.js script.当用户单击桌面应用程序中的按钮时,它将运行外部 Node.js 脚本。 I want to use Electron to build a GUI that is able to call a script to do some work after the user clicks the button.我想使用 Electron 构建一个 GUI,该 GUI 能够在用户单击按钮后调用脚本来完成一些工作。

Look into the child_process node js module.查看child_process节点 js 模块。 You can implement something like this:你可以实现这样的东西:

On the client side:在客户端:

const { ipcRenderer } = require("electron");
document.getElementById("someButton").addEventListener(e => {
  ipcRenderer.send("runScript");
});

On the electron side:在电子方面:

const { ipcMain } = require("electron");
const exec = require('child_process').exec;

ipcMain.on("runScript", (event, data) => {
   exec("node script.js", (error, stdout, stderr) => { 
        console.log(stdout);
    });
});

Keep in mind, in order to use ipcRenderer on the client side you may need to enable nodeIntegration in your browser window.请记住,为了在客户端使用 ipcRenderer,您可能需要在浏览器窗口中启用nodeIntegration

To be able to kill a process you would have to use the child_process.spawn method and save that in a variable then run variableThatProcessWasSpawned.stdin.pause();为了能够child_process.spawn进程,您必须使用child_process.spawn方法并将其保存在一个变量中,然后运行variableThatProcessWasSpawned.stdin.pause(); then variableThatProcessWasSpawned.kill() .然后variableThatProcessWasSpawned.kill() For example例如

const spawn = require('child_process').spawn ;

let process = null;

ipcMain.on("runScript", (event, data) => {
   process = spawn("node script.js");
});

ipcMain.on("killProcess", (event, data) => {
   if(process !== null) {
       process.stdin.pause();
       process.stdin.kill();
       process = null;
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在电子反应应用程序中将 powershell 输出显示为流 - Display powershell output as a stream in electron-react app 在电子反应应用程序中多次触发 NodeJS 事件 - NodeJS events triggering multiple times in electron-react app 如何使用Electron运行和打包外部可执行文件? - How to run and pack external executable using Electron? 在Electron应用程序中运行python脚本 - Run python script in Electron app 如何从 Electron/React App 访问脚本? - How can i access script from Electron/React App? 如何使用参数运行电子应用程序? - How to run an electron app with arguments? 如何在Docker上运行电子应用 - How to run an electron app on docker Electron-React:MaxListenersExceededWarning:检测到可能的 EventEmitter memory 泄漏。 21 个 updateDeviceList 监听器添加到 [EventEmitter] - Electron-React: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 21 updateDeviceList listeners added to [EventEmitter] Electron 应用程序在生产模式下无法通过 child_process.spawnSync API 在 Mac 中运行外部脚本,但在 Linux 中完美运行 - Electron app in production mode fails to run external script via child_process.spawnSync API in Mac but works perfectly in Linux 如何使用 electron forge 对 electron 应用程序进行签名? - How to sign electron app using electron forge?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM