Program start parameter
When your application starts up, you may need to take along parameters that affect the execution logic of the application. For example, by setting startup parameters, the program can be changed to a different skin.
For example, in Windows, right-click the properties of Windows EXE, and the following interface is displayed
In the target column, is the path of exe program, we can add special parameters after this path to meet our needs.
“C:\Program Files (x86)\Tencent\WeChat\WeChat.exe” –args theme=dark
We added a theme parameter to the path with a value of dark. This parameter can be read when the application starts to display the interface with the theme of dark. How does this happen in the Electron program?
In fact, the principle is very simple. We all know that the Electron main process is actually a Node process, so you can directly obtain the startup parameters through the method supported by Node. In the past, implementing the Web Server with Node would configure the number of workers by passing –worker=2 and then using process.argv. The same thing can be done in electron.
// Main process main.js
const { app, BrowserWindow } = require('electron');
console.log(process.argv);
Copy the code
The output from the code above is shown below
As you can see, getting the startup parameters in the main process is quite simple. What about renderers? This brings us back to the question of how the main process and the renderer communicate with each other in the previous chapter
// render process renderer.js
const remote = require('electron').remote
alert(remote.process.argv)
Copy the code
For more information, see the previous chapter.
Chrome command line switch
In addition to the Electron startup parameters, another important startup parameter switch is the Chrome browser’s own. Electron implements Windows by integrating with Chrome, so each window is essentially a Chrome browser process. In Chrome, there are many features that are not enabled by default for security reasons or are still in the experimental stage, and need to be manually enabled through command line parameters. For example, you can add –remote-debugging-port=port to chrome.exe to specify the port to enable remote debugging.
Electron also supports this feature. If you have special requirements, you can use the following methods to enable the desired feature
/ / main process. The main js const {} app = the require (' electron) app.com mandLine. AppendSwitch (' remote - was debugging - port, '8315') app.com mandLine. AppendSwitch (' - disable - HTTP cache - ', 'true') app) on (' ready '() = > {} / / your code)Copy the code