问题
My app is electron with a BrowserWindow
loading a local page index.html.
I call npm run start
a script to run electron main.js
, the app opens and the html loaded.
Can I add an argument to the script that will load different html file into the BrowserWindow
?
In the main.js file the code is :
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({
webPreferences:{
webSecurity:false
},
fullscreen : false });//, alwaysOnTop : true , kiosk : true })
mainWindow.setMenu(null);
// and load the index.html of the app.
let url = `file://${__dirname}/index.html`; \\ index.html should be determined by argument passed at start.
mainWindow.loadURL(url,loadOptions);
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
回答1:
The way of passing arguments will be same, the only thing you have to take care is path of electron. In package.json
its written npm
start will perform electron main.js
. So you will have to execute this command explicitly and pass arguments with "proper path of electron" i.e ./node_modules/.bin/electron
. Then the command will be
./node_modules/.bin/electron main.js argv1 argv2
and these arguments you can access by process.argv
in main.js
and If wish you to access these parameters in your app then there are following things to do :
1.In your main.js define a variable like
global.sharedObject = {prop1: process.argv}
2.In your app just include remote and use this sharedObject
var remote = require('electron').remote,
arguments = remote.getGlobal('sharedObject').prop1;
console.log(arguments);
3.Output will be ["argv1", "argv2"]
Source : https://discuss.atom.io/t/how-to-pass-command-arguments-in-electron/17247
回答2:
UninstallDisplayName
This lets you specify a custom name for the program's entry in the Add/Remove Programs Control Panel applet. The value may include constants. If this directive is not specified or is blank, Setup will use the value of [Setup] section directive AppVerName for the name.
"build": {
"nsis": {
"uninstallDisplayName": "Your app name..."
}
}
Read more:
NSIS
UninstallDisplayName
回答3:
To pass the command line arguments to electron app:
./node_modules/.bin/electron main.js --arg1=value --arg2=value
It can be retrieved like this in the main.js:
import { app } from "electron";
app.commandLine.getSwitchValue("arg1");
app.commandLine.getSwitchValue("arg2");
来源:https://stackoverflow.com/questions/39349017/how-to-run-an-electron-app-with-arguments