我正在为Windows开发电子测试应用程序。

目标是一个自动启动的应用程序,该应用程序在Windows用户每次登录时都会显示弹出消息。

场景:


我正在执行npm start来打包我的代码。 (index.js +
package.json)
我执行生成的.exe,它将弹出我的消息。 (到目前为止一切都很好,对吧?)
我从Windows(如果重要的话,请按CTRL + L ALT + DEL)注销,然后再次登录以测试该应用程序。
弹出窗口打开,但默认的电子欢迎页面也打开。


package.json

{
    "name": "test",
    "description": "",
    "version": "0.0.1",
    "main": "index.js",

    "scripts": {
        "test": "electron .",
        "start": "electron-packager . Test --platform=win32 --arch=x64 --overwrite"
    },

    "author": "",
    "license": "MIT",

    "build": {
        "appId": "com.test.test",
        "win": {
          "target": "NSIS"
        }
    },

    "dependencies": {
        "auto-launch": "^5.0.5"
    },

    "devDependencies": {
        "electron": "latest",
        "electron-packager": "^12.1.1"
    }
}


index.js

const {app, dialog} = require("electron");
const AutoLaunch = require("auto-launch");

app.on("ready", function(){
  dialog.showMessageBox({ message: "We are ready to take off! :-)", buttons: ["OK"] });

  let autoLaunch = new AutoLaunch({
    name: "test"
    // path: app.getPath("exe")
  }).isEnabled().then((isEnabled) => {
    if (!isEnabled){
            autoLaunch.enable();
            dialog.showMessageBox({ message: "AutoLaunch enabled.", buttons: ["OK"] });
    }
    else dialog.showMessageBox({ message: "AutoLaunch already enabled.", buttons: ["OK"] });
  });

  app.quit();
});


编辑:发现了一个阻止package.json被更新的问题。一个简单的npm info导致完全意外的输出。

最佳答案

哇,完全忘记了这个问题。
解决方案如下:

更新所有依赖项。
确保.html / .js文件的路径是绝对正确的。

绝对路径以/开头
例如/files/index.html

这就是它的工作原理!

07-24 18:01
查看更多