May 25, 2021 Electron
App modules are designed to control the lifecycle of the entire app.
The following example shows how to exit the app when the last window is closed:
var app = require('app'); app.on('window-all-closed', function() { app.quit(); });
The app object triggers the following events:
It is triggered when the application completes the underlying startup. I n Windows and Linux, the will-finish-launching event is the same as the ready event; You should often set up listeners for open-file and open-url here and start crash reports and automatic updates.
In most cases, you should only do all the business in the ready event processor.
Triggered when Electron completes initialization.
Triggered when all windows are closed.
This time can only be triggered if the app has not exited. If the user presses Cmd-Q, or the developer calls app.quit(), Electron will first try to close all windows before triggering the will-quit event, in which case window-all-closed will not be triggered.
Return:
It is triggered when the application starts closing its window. Calling event.preventDefault() will prevent the default behavior of terminating the application.
Return:
Triggered when all windows have been closed and the app is about to exit. Calling event.preventDefault() will prevent the default behavior of terminating the application.
You can see the difference between the will-quit event and the window-all-closed event in the description of the window-all-closed event.
Return:
Triggered when the application is exiting.
Return:
Triggered when the user wants to open a file in the app. 20> Open-file events are often triggered when the app is already open and the system wants to open the file again using the app. Open-file is also triggered when a file is dragged into the dock and the app is not already running. Make sure that the open-file event is monitored when the app starts, even before the ready event is triggered, to handle this situation.
If you want to handle this event, you should call event.preventDefault(). In a Windows system, you need to parse process.argv to get the file path.
Return:
It is triggered when the user wants to open a url in the app. The URL format must be identified in advance to be opened by your app.
If you want to handle this event, you should call event.preventDefault().
Return:
Triggered when an app is activated, it is often used when clicking on the app's dock icon.
Return:
Triggered when a BrowserWindow loses focus.
Return:
Triggered when a BrowserWindow gets the focus.
Return:
Triggered when a BrowserWindow is created.
Return:
When validation of the certificate certificate on url fails, if you need to trust the certificate, you need to block the default behavior event.preventDefault() and call callback (true).
session.on('certificate-error', function(event, webContents, url, error, certificate, callback) { if (url == "https://github.com") { // 验证逻辑。 event.preventDefault(); callback(true); } else { callback(false); } });
Return:
Triggered when a client authentication is requested.
Url refers to the web address that requests client authentication and needs to pass in a certificate from a list of certificates when calling callback.
You need to call event.preventDefault() to prevent your app from automatically using the first certificate for validation. Here's what it looks like:
app.on('select-certificate', function(event, host, url, list, callback) { event.preventDefault(); callback(list[0]); })
Return:
It is triggered when the webContents do an HTTP landing verification.
By default, Electron cancels all validation behavior, and if you need to override this behavior, you need to block the default behavior with event.preventDefault() and use callback (username, password) for validation.
app.on('login', function(event, webContents, request, authInfo, callback) { event.preventDefault(); callback('username', 'secret'); })
Triggered when the GPU process crashes.
App objects have the following methods:
Note that some methods can only be used for specific operating systems.
Try to close all the windows. 20> The before-quit event will be triggered first. If all windows are successfully closed, the will-quit event will be triggered and the app will be closed by default.
This method ensures that all beforeunload and unload event processors are executed correctly. If a window's beforeunload event processor returns false, the entire app may cancel its exit.
Hide all app windows, not minimize them.
Hide and rediscofocus all windows, and they are not automatically selected.
Exit the app with exitCode.
All windows are closed immediately and the user is not asked. The two events, before-quit and will-quit, are not triggered
Returns the path to the file where the current app is located.
Returns a special folder or file path related to the name parameter. Throw an Error when it fails.
You can request the following path by name:
The path to override a name is path, which can be a folder or a file, which is related to the type of name. I f the folder to which this path points does not exist, the folder will be created by this method. Error is thrown if an error occurs.
The name parameter can only be defined in app.getPath.
By default, cookies and caches for web pages are stored in the userData folder. If you want to change this location, you need to override the path to userData before the ready event in the app module is triggered.
Returns the version of the loaded application. If the application's package.json file does not have a version number written, returns the version of the current package or executable file.
Returns the name in the package.json file for the current application.
Because of the naming rules for npm, the name field is usually a short, small-case string. H owever, the full name of the app name is usually capital letters, and you should use a productName field alone to represent the full name of your application. Electron gives preference to this field as the app name.
Rewrite the name of the current app
Returns the language of the current application.
Add path to the list of recently visited documents.
This list is managed by the operating system. In Windows you can access it through the task bar, and in OS X you can access it through the dock menu.
Clear the list of recently accessed documents.
Add tasks to the Tasks category for JumpList features in Windows.
The Task object format in tasks is as follows:
Task Object
Dynamically set whether a certificate is always sent for HTTP NTLM or Negotiate authentication. T ypically, Electron only sends NTLM/Kerberos certificates to the local network, such as computers that are in the same domain as you. However, if the network is not well set up, this automatic probe may fail, so you can customize electron's behavior for all URLs through this interface.
This method allows your app to have up to one instance at a time, otherwise your app can run multiple times and produce multiple instances. You can use this interface to ensure that only one instance is running normally and that all remaining instances are terminated and exited.
If multiple instances are running at the same time, callback in the first instance being run is called as callback (argv, workingDirectory). T he remaining instances are terminated. argv is an array that contains a list of command line parameters for this instance, and workingDirectory is the current running directory for this instance. Typically, we use to remind users that the app has been opened by activating it on the home screen and de-minimizing it.
Callback is not likely to be called after the app's ready event.
If the current instance is the first instance, false is returned in this method to ensure that it continues to run. Otherwise, true will be returned to let it exit immediately.
In OS X, if a user opens an app through Finder, open-file, or open-url, the system enforces that only one instance is running. But if the user opens through the command line, this system mechanism is ignored, so you still need this method to ensure that the app runs as a single instance.
Here's a simple example. We can use this example to learn how to make sure your app runs as a single instance.
var myWindow = null; var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) { // 当另一个实例运行的时候,这里将会被调用,我们需要激活应用的窗口 if (myWindow) { if (myWindow.isMinimized()) myWindow.restore(); myWindow.focus(); } return true; }); // 这个实例是多余的实例,需要退出 if (shouldQuit) { app.quit(); return; } // 创建窗口、继续加载应用、应用逻辑等…… app.on('ready', function() { });
Change the Application User Model ID of your current app to id .
If DWM composition (Aero Glass) is enabled, this method returns true, otherwise false. You can use this method to decide whether to turn on transparent window effects, because transparent window effects are not valid if the user does not turn on DWM.
For example:
let browserOptions = {width: 1000, height: 800}; // 只有平台支持的时候才使用透明窗口 if (process.platform !== 'win32' || app.isAeroGlassEnabled()) { browserOptions.transparent = true; browserOptions.frame = false; } // 创建窗口 win = new BrowserWindow(browserOptions); // 转到某个网页 if (browserOptions.transparent) { win.loadURL('file://' + __dirname + '/index.html'); } else { // 没有透明特效,我们应该用某个只包含基本样式的替代解决方案。 win.loadURL('file://' + __dirname + '/fallback.html'); }
Add a command-line switch to Chromium with the optional parameter value.
Note This method does not affect process.argv, which we usually use to control some of the underlying Chromium behavior.
Add a command line argument directly to Chromium, which must be quoted and formatted correctly.
Note This method does not affect process.argv.
When critical is passed in, the app in the dock will start bouncing until the app is activated or the request is canceled.
When informational is passed in, the icon in the dock bounces for only one second. However, the request is still activated until the app is activated or the request is canceled.
The return value returned by this method represents the ID of the request.
Cancel the request for this id.
Set the string that the app displays in the dock.
Returns the string that the app displays in the dock.
Hide the icons applied to the dock.
Displays the icon applied to the dock.
Set the app's dock menu .
Set the icon that the app displays in the dock.