【问题标题】:HTML Button to call app.quit() function using electron使用电子调用 app.quit() 函数的 HTML 按钮
【发布时间】:2018-06-09 20:29:46
【问题描述】:

我最近发现电子作为 GUI“制造商”。我问我的编程老师,是否可以使用电子运行 bash 脚本。

他给了我一个 GitHub 的链接(遗憾的是被微软收购了),它与一个 bash 脚本一起工作

我最近编写了一个脚本,它使用 wine 安装了 Origin Launcher,并且运行良好。但是现在,我想尝试用一个 GUI 来覆盖它,这样不熟悉 Linux 终端的人就有一个漂亮的 GUI。

现在,我观看了有关如何创建电子应用程序的视频。我一步一步地跟着它,它奏效了。

在我的index.html 文档中,我制作了两个按钮,一个用于启动Installation,第二个用于cancel。

我在 html 文件的头部包含了main.js

<head>
    <title>LoriginSetup</title>
    <link href="assets/css/main.css" rel="stylesheet">
    <script src="main.js"></script>
</head>

在 Body 内部,我使用 onclick 调用 main.js 内部的函数。

函数调用quitApplication()

<div class="btns">
  <button class="btn btnstart" id="startinstall">Start Installation</button>
  <button class="btn btncancel" id="cancelinstall" onclick="quitApplication()">Cancel Installation</button>
</div>

我使用alert('') 测试了函数。

警报工作正常,但是当我将警报替换为 app.quit(); 时,它不再工作了。

app.on('ready', function(){
  //Create new window
  mainWindow = new BrowserWindow({});
  //Load html into window
  mainWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file',
    slashes: true
  }));

  //Build Menu from MenuTemplate
  const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
  //Insert Menu
  Menu.setApplicationMenu(mainMenu);
});

function quitApplication(){
    alert('This Button is Working');
}

我不知道我做错了什么,但我很感激我能得到的每一个帮助。

【问题讨论】:

    标签: javascript html linux bash electron


    【解决方案1】:

    从主 Electron JS 中删除 quitApplication() 并将其添加到您的 main.js 中:

    const electron = require('electron');
    const remote = electron.remote;
    
    function quitApplication(){
        if (process.platform !== 'darwin') { remote.app.exit(); }
    }
    

    如果您想将此代码保留在主 Electon JS 文件中,另一种方法是使用 IPC:https://electronjs.org/docs/api/ipc-main。渲染器可以调用主进程中退出的方法。

    【讨论】:

    • 非常感谢,remote.app.exit() 的上半部分工作了:)
    猜你喜欢
    • 1970-01-01
    • 2010-12-29
    • 2019-11-06
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多