【问题标题】:Function in Electron does not work but in Node doesElectron 中的功能不起作用,但在 Node 中起作用
【发布时间】:2021-08-01 06:02:05
【问题描述】:

我在 Electron 和 Node 上运行相同的函数,但得到不同的结果:

从命令行使用 Node 调用函数工作正常,我得到了连接设备的列表:

节点 my_func.js

var iosDevice = require("node-ios-device");

iosDevice.devices(function (err, devices) {
  if (err) {
    console.error("Error!", err);
  } else {
    console.log(devices);
  }
});

但是将相同的代码块放在 Electron 的 main.js 中 (它不在任何其他渲染器文件中,它只是使用标准从命令行调用:$ npm run start), 产生以下结果:

main.js

const { app, BrowserWindow } = require("electron");
var iosDevice = require("node-ios-device");

iosDevice.devices(function (err, devices) {
  if (err) {
    console.error("Error!", err);
  } else {
    console.log(devices);
  }
});

// SET ENVIRONTMENT
process.env.NODE_ENV = "development";
// process.env.NODE_ENV = "production";

const isDev = process.env.NODE_ENV !== "production" ? true : false;
// CHECK PLATFORM
const isMac = process.platform === "darwin" ? true : false;

let mainWindow;

function createMainWindow() {
  mainWindow = new BrowserWindow({
    title: "test",
    width: 700,
    height: 195,
    icon: "./assets/icons/Icon_256x256.png",
    resizable: isDev ? true : false,
    backgroundColor: "white",
    show: false,
    webPreferences: {
      nodeIntegration: true,
      // sandbox: true,
      // contextIsolation: false,
    },
  });

  mainWindow.loadFile("./app/index.html");

  mainWindow.webContents.on("did-finish-load", function () {
    mainWindow.show();
  });
}

app.on("ready", () => {
  createMainWindow();
});

app.on("window-all-closed", () => {
  if (!isMac) {
    app.quit();
  }
});

app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createMainWindow();
  }
});
  • 控制台不显示任何结果
  • 显然,创建了三个应用实例,在 Dock 中弹出 3 个应用图标:

Three app icons show up

这是 package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "Viking",
  "license": "ISC",
  "devDependencies": {
    "electron": "^11.4.4"
  },
  "dependencies": {
    "node-ios-device": "^1.9.2"
  }
}

并且需要 npm "node-ios-device" 包不会出现任何问题。

知道如何使用 Electron 而不是 Node 来完成相同的任务吗?

谢谢。


更新

在尝试了不同的事情之后,这是我最接近解决方案的地方:

我将函数移动到另一个文件,然后从 main.js 文件中调用它,生成一个子进程并使用节点调用该函数:

ipcMain.on("dev:getinfo", (event, options) => {
  let child_proc;
  let devices;

  child_proc = spawn("node", ["device.js"]);

  child_proc.stdout.on("data", (data) => {
    devices = `${data}`;
  });

  child_proc.on("exit", function (code, signal) {
    console.log(devices);
  });
});

它可以工作,正在开发中......

但我觉得它不会在生产中工作,因为我想分发这个应用程序的用户没有安装 Node.js,而且我不知道如何以这样的方式捆绑电子用户无需安装 Node.js。

所以一个问题解决了,但另一个问题出现了????

【问题讨论】:

  • 你能调试一下看看我们哪里出错了吗?要求调用失败了吗?您是否在禁用节点的渲染器中调用此代码?我们需要看到更多的电子代码
  • 感谢您的回答,main.js 文件包含该函数,我只是用这个基本命令运行电子:
  • 我刚刚更新了原始消息的内容,以增加一点清晰度,谢谢
  • 抱歉询问。 “控制台”是指“浏览器控制台”还是“终端”? main.js 中的 console.log 不会仅在终端中显示在浏览器控制台中。当您调用 npm run start 时,电子将打开,您可能希望 console.log 显示在浏览器控制台中。

标签: javascript node.js console electron instance


【解决方案1】:

关于我上面评论的更多信息......

如果你想从主进程到渲染器(即电子前端或网页)访问iOS设备,请尝试使用 webContents 或 ipcMain/ipcRenderer 事件发射器。

   // In the main process.
    const { app, BrowserWindow } = require("electron");
    const { iosDevice } = require("node-ios-device");
    
    -- previous snips --

    let win = null
    
    app.whenReady().then(() => {
      win = new BrowserWindow({ width: 800, height: 600 })
      win.loadURL(`file://${__dirname}/index.html`)
      win.webContents.on('did-finish-load', () => {
        let devices = iosDevice.devices(function (err, devices) {
                 if (err) {
                     console.error("Error!", err);
                 } else {
                     console.log(devices);
                     return devices;
                 }
        });

        win.webContents.send('send-devices', devices);
      })
    })

然后在你的 index.html..

<!-- index.html -->
<html>
<body>
  <script>
    require('electron').ipcRenderer.on('send-devices', (event, message) => {
      console.log(message) // Prints the devices
    })
  </script>
</body>
</html>

//https://www.electronjs.org/docs/api/web-contents#contentssendchannel-args

【讨论】:

  • 谢谢,我也希望控制台中的设备列表,因为我从命令行(终端)运行应用程序,所以我可以访问该输出。我尝试了您的建议,但得到了相同的结果:在 Dock 和终端中弹出 3 个应用程序图标,并且网络浏览器没有显示任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 2016-10-29
  • 1970-01-01
相关资源
最近更新 更多