从已经运行的Electron Application获取命令行参数?

时间:2018-01-02 19:48:01

标签: javascript node.js electron

目前我需要从Electron应用程序获取命令行参数。因此,如果使用命令行参数启动应用程序,我需要使用渲染器过程中的参数(webview是特定的。)

首次打开应用程序(使用onload)时,这样可以正常工作,但是当应用程序已经运行且用户尝试使用args从cmd重新打开应用程序时,我遇到了问题。

目前,我正在使用:

let closeDupeApp = app.makeSingleInstance(function(commandLine, workingDirectory) {
    // Someone tried to run a second instance, we should focus our window.
    if (mainWindow) {
        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.focus();
    }
});

防止第二个窗口打开。但是,我需要commandLine参数,我需要将它们发送到渲染器进程。

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:2)

您可以在主进程中阅读process.argv并将其粘贴到文件的末尾:// url作为BrowserWindow.open()中的查询字符串。我经常这样做。您可以对数组上的encodeURIComponent进行JSON编码,以便在渲染器进程中获得一个很好的列表。

答案 1 :(得分:1)

找到解决方案:

main.js

let closeDupeApp = app.makeSingleInstance(function(commandLine, workingDirectory) {
    if (mainWindow) {
        // Send args to renderer
        mainWindow.webContents.send("cmdArgs", commandLine);

        if (mainWindow.isMinimized()) mainWindow.restore();
        mainWindow.focus();
    }
});

rendererFile.js

require('electron').ipcRenderer.on('cmdArgs', function(event, cmdArgs) {
    // STUFF
}
相关问题