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;
});
}
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With