The following modules are available to the main process in Electron:

The module describe
app Responsible for controlling the entire life cycle of the application
autoUpdater The module provides a toSquirrelAutomatically update the interface of the framework
BrowserWindow Can be used to create a new browser window
contentTracing Used to collectChromiumTrace information generated by the content module
dialog Use to display native system dialogs, such as open file dialogs
global-Shortcut Keyboard shortcuts for registering and logging out globally
ipcMain The module isEventEmitterAn instance object used in the main process that can send synchronous or asynchronous messages to interact with the renderer
Menu Use to create a native menu, such as a context menu
MenuItem Add menu subitems to the back and forth menu
powerMonitor Used to show battery power changes, can only be used in the main process, and only if the Ready event has been issued
powerSaveBlocker It prevents the system from going into power-saving mode, sleep mode
protocol Used to register a custom protocol or to declare the use of an existing protocol
session To create a newSessionObject, save local objects and other operations
webContents This is aEventEmitter, responsible for rendering and controlling a web page, is aBrowserWindowThe properties of the
Tray aTrayOne representing an operating system notification areaicon, is usually bound to a context menu

App module

APP modules are designed to control the entire application life cycle.

Example:

For example, exit the application when the last window is closed:

const app = require('app');

app.on('window-all-closed', function(){
    app.quit();
});

The App object can emit the following events:

The event describe
will-finish-launching When the program completes the basic startup, similar toreadyThe event
ready whenElectronTriggered when initialization is complete
window-all-closed Triggered when all Windows have been closed. Triggered only when the program is about to be pushed to exit. If you call theapp.quit()It won’t fire
before-quit Called when the program begins to close the windowevent.prevertDefault()The default behavior of the application is prevented
will-quit This event is emitted when the program is about to exit and the window has already closed
quit Triggered when the application is exiting

AutoUpdater module

The autoupater module provides an interface to the Squirrel automatic update framework.

The AutoOutPdater object fires the following events:

The event describe
error Triggered when an update occurs with an error
checking-for-update Triggered when you start checking for updates
update-available Triggered when an update is found available, the update pack download starts automatically
update-not-available Triggered when no updates are available
update-downloaded Triggered when the update download is complete

BrowserWindow module

The BrowserWindow module is used to create a new BrowserWindow.

Example:
// In the main process const BrowserWindow = require('electron').BrowserWindow; // const BrowserWindow = require('electron').remote.BrowserWindow; var win = new BrowserWindow({ width: 800, height: 400, show: false }); win.on('closed', function() { win = null; }); win.loadURL('https://github.com'); win.show();

The BrowserWindow object can fire the following events:

The event describe
close It fires when the window is about to close, it’s inDOMbeforeunloadunloadEvent before firing, usedevent.preventDefault()You can cancel this operation
closed When the window is closed, when you receive this event, you should delete the reference object to the closed window and avoid using it again
unresponsive Trigger an event when the interface is stuck
responsive Triggered when interface recovery is stuck
blur Triggered when the window loses focus
focus Triggered when the window gets focus
maximize Triggered when the window is maximized
unmaximize Triggered when the window exit is maximized
minimize Triggered when the window is minimized
restore Triggered when the window is restored from minimization
resize In the windowsizeTriggered when you change
move Triggered when the window movesOS XThe alias formoved

ContentTracing module

The ContentTracing module is used to collect the search data generated by the underlying Chromium content module. This module does not have a web interface, so we need to add Chrome :// Tracing/to the Chrome browser to load the generated files and view the results.

Example:
const contentTracing = require('contentTracing').;

const options = {
  categoryFilter: '*',
  traceOptions: 'record-until-full,enable-sampling'
}

contentTracing.startRecording(options, function() {
  console.log('Tracing started');

  setTimeout(function() {
    contentTracing.stopRecording('', function(path) {
      console.log('Tracing data recorded to ' + path);
    });
  }, 3000);
});

The methods of the Content-Tracing module are as follows:

methods describe
getCategories Gets a set of taxonomy groups that can be changed to a new code path
startRecording Start recording to all processes. Once a request is received to start recording, the recording is started immediately and is recorded asynchronously in the child processes
stopRecording Stops recording of all child processes
startMonitoring Start listening to all processes
stopMonitoring Stop listening on all child processes
captureMonitoringSnapshot Gets the lookup data for the current listening
getTraceBufferUsag By looking forbufferProcess to get the percentage maximum usage
setWatchEvent Called at any time on any process when a specified event occurscallback
cancelWatchEvent cancelwatchEvent, which might be caused if a lookup is startedwatchError in callback function for event

Dialog module

The Dialog module provides APIs to display native system dialogs, such as open file boxes or alert boxes. So web applications can give users the same experience as system applications.

Example:

A dialog box example that shows how to select files and directories:

var win = ... ; // Display the dialog const dialog = require('electron').dialog; console.log(dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', 'multiSelections' ]}));

Global – shortcut module

The global-shortcut module is used to set up shortcuts for various custom operations. Note that the shortcut keys registered with this module are global to the system and do not use this module before the application module responds to the Ready message.

Example:
var app = require('app'); var globalShortcut = require('electron').globalShortcut; App. on('ready', function() {var ret = globalShortcut. Register (' CTRL +x', 'CTRL +x'); function() { console.log('ctrl+x is pressed'); }) if (! ret) { console.log('registration failed'); } / / check whether registered the 'CTRL + x' shortcut. The console log (globalShortcut. IsRegistered (' CTRL + x)); }); App. On (' will - quit ', function () {/ / logout "CTRL + x" shortcut globalShortcut. Unregister (CTRL + x "); / / cancel all shortcuts globalShortcut. UnregisterAll (); });

IpcMain module

The IPCMain module is an instance of the EventEmitter class that, when used in the main process, controls the asynchronous or synchronous messages sent from the renderer process that trigger events.

Example:

Here is an example of sending and processing messages between the main process and the renderer process.

Main process:

const ipcMain = require('electron').ipcMain; ipcMain.on('asynchronous-message', function(event, arg) { console.log(arg); // PingEvent.Sender.Send (' Asynchronous -reply', 'Pong '); }); ipcMain.on('synchronous-message', function(event, arg) { console.log(arg); // output ping event.returnValue = 'pong'; });

Render process:

const ipcRenderer = require('electron').ipcRenderer; console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // Pong iPcrenderer. on(' Asynchronous - Reply ', function(event, arg) {console.log(arg); // output Pong}); ipcRenderer.send('asynchronous-message', 'ping');

The menu module

The Menu module can be used to create native menus, which can be used as application menus and content menus.

Example:

Create a new menu syntax:

var menu = new Menu();

MenuItem module

The MenuItem module adds menu subitems to the back and forth menu.

Example:

Create a new MenuItem syntax:

var menuItem = new MenuItem(options)

Options options look like this:

  • click
  • role
  • type
  • label
  • sublabel
  • accelerator
  • icon
  • enabled
  • visible
  • checked
  • submenu
  • id
  • position

PowerMonitor module

The PowerMonitor module is used to listen for changes in the energy sector and can only be used in the main process. The app module cannot be used after the ready event is triggered.

Example:
App. on('ready', function() {require('electron').powermonitor. on('suspend', function() {console.log(' The system will go to sleep '); }); });

The PowerMonitor object fires the following events:

The event describe
suspend Triggered when the system is suspended
resume Triggered while system recovery continues to work
on-ac Triggered when the system is using alternating current
on-battery Triggered when the system is using battery power

PowerSaveBlocker module

The PowerSaveBlocker module is designed to prevent the application from going into sleep mode, thus allowing the application to keep the system and screen working.

Example:
const powerSaveBlocker = require('electron').powerSaveBlocker;

var id = powerSaveBlocker.start('prevent-display-sleep');
console.log(powerSaveBlocker.isStarted(id));

powerSaveBlocker.stop(id);

The PowerMonitor module has the following methods:

methods describe
start It starts to stop the system from going into sleep mode
stop To specifyblockerStop the active
isStarted returnboolean, whether the correspondingpowerSaveBlockerHas been launched

The protocol module

The protocol module can register a custom protocol or use an existing protocol. This module is only available after the app module’s ready event is triggered.

Example:

Using a protocol similar to the file:// functionality:

const electron = require('electron'); const app = electron.app; const path = require('path'); app.on('ready', function() { var protocol = electron.protocol; protocol.registerFileProtocol('atom', function(request, callback) { var url = request.url.substr(7); callback({path: path.normalize(__dirname + '/' + url)}); }, function (error) {if (error) console.error(' protocol failed ')}); });

The session module

The session module can be used to create a new session object.

Example:
const BrowserWindow = require('electron').BrowserWindow;

var win = new BrowserWindow({ width: 800, height: 600 });
win.loadURL("http://github.com");

var ses = win.webContents.session;

WebContents module

WebContents is an EventEmitter that renders and controls a web page and is a property of a BrowserWindow object.

Example:
const BrowserWindow = require('electron').BrowserWindow;

var win = new BrowserWindow({width: 800, height: 1500});
win.loadURL("https://www.9xkd.com/");

var webContents = win.webContents;

The WebContents object can emit the following events:

The event describe
did-finish-load Emit an event when the navigation is complete,onloadThe event is complete
did-fail-load This is something likedid-finish-load, but is issued when the load fails or the load is canceled
did-frame-finish-load When aframeEmit an event when the navigation is complete
did-start-loading whentabspinnerstartspinningwhen
did-stop-loading whentabspinnerThe end of thespinningwhen
did-get-response-details An event is emitted when details about the requested resource are available
did-get-redirect-request When specifyingframeAn event is emitted when the document in the
page-favicon-updated whenpageReceive the iconurlWhen the event is emitted
new-window whenpageRequest open specifiedurlThe event is emitted when the window is opened
will-navigate When a user orpageEmit an event when you want to start navigation
did-navigate Emit an event when a navigation ends
did-navigate-in-page An event is emitted when in-page navigation occurs
crashed Issue an event when the renderer process crashes
plugin-crashed Issue an event when the plug-in process crashes
destroyed whenwebContentsAn event is emitted when it is deleted
devtools-opened Issue an event when the developer toolbar is opened
devtools-closed Issue an event when the developer toolbar is closed
devtools-focused Issue an event when the developer toolbar gets focus or opens
certificate-error When verifying a certificate orurlIssue an event when it fails
select-client-certificate Issue an event when a client certificate is requested
login whenwebContentsIssue an event when you want to do basic validation
found-in-page An event is issued when an in-page search using WebContents. FindInPage is performed and an available value is found
media-started-playing Issue an event when the media starts playing
media-paused Issue an event when the media stops playing
did-change-theme-color whenpageThis is usually due to the introduction of onemetaThe label
cursor-changed An event is emitted when the type of mouse changes

Link: https://www.9xkd.com/