The following modules in Electron are available to both the main process and the renderer process:
The module | describe |
---|---|
clipboard | Provides methods for copy and paste operations |
crashReporter | Enable sending app crash reports |
nativeImage | inelectron The image can be obtained from the file path or usednativeImage object |
screen | Retrieves information about screen size, display, mouse position, etc |
shell | Provides the ability to correlate with other desktop clients |
The clipboard module
The Clipboard module provides methods for copy-and-paste operations.
Example:
For example, to write a string to Clipboard:
const clipboard = require('electron').clipboard;
clipboard.writeText('xkd');
The Clipboard module has the following methods:
methods | describe |
---|---|
readText | In plain text form fromclipboard Returns the content |
readText | In plain text form fromclipboard Returns the content |
writeText | In plain text toclipboard Add content |
readHtml | returnclipboard Tag content in the |
readHtml | returnclipboard Tag content in the |
writeHtml | toclipboard addmarkup content |
writeHtml | toclipboard addmarkup content |
readImage | fromclipboard In returnNativeImage content |
writeImage | toclipboard writesimage |
readRtf | from clipboard In returnRTF content |
writeRtf | toclipboard writesRTF The format oftext |
clear | emptyclipboard content |
availableFormats | returnclipboard An array of supported formats |
has | returnclipboard Support for specifyingdata The format of the |
read | readclipboard 的 data |
write | toclipboard writedata |
CrashReporter module
The CrashReporter module is enabled to send application crash reports.
Example:
For example, auto-submit a crash report to the server:
const crashReporter = require('electron').crashReporter;
crashReporter.start({
productName: 'my_electron',
companyName: 'shushuo',
submitURL: 'https://9xkd.com/url-to-submit',
autoSubmit: true
});
The Crash-Reporter module has the following methods:
methods | describe |
---|---|
start | In use of otherscrashReporter APIs Before using |
getLastCrashReport | Returns the date and date of the last crash reportID If no crash report has been sent, or if crash report collection has not started, it will be returnednull |
getUploadedReports | Returns all uploaded crash reports, each containing the upload date andID |
NativeImage module
We can get an image in Electron either by the path to the file or by using a nativeImage object.
Example:
For example, when creating a tray or setting an icon for a window, you can use a string image path:
var appIcon = new Tray('/Users/somebody/images/icon.png');
var window = new BrowserWindow({icon: '/Users/somebody/images/window.png'});
Or read the image from the clipboard:
var image = clipboard.readImage();
var appIcon = new Tray(image);
PNG and JPEG image formats are currently supported. It is best to use the PNG format because it supports transparency and lossless compression. The ICO icon format is also available in Windows.
The NativeImage class has the following methods:
methods | describe |
---|---|
createEmpty | Create an empty onenativeImage The instance |
createFromPath | From the specifiedpath Create a new onenativeImage The instance |
createFromBuffer | frombuffer Create a new onenativeImage Instance, defaultscaleFactor Is 1.0 |
CreateFromDataURL from dataURL | Create a new onenativeImage The instance |
Screen module
Screen module is an EventEmitter that retrieves screen size, display, mouse position, etc. The app module cannot be used until its ready event is triggered.
Example:
For example, let’s create a window that fills the entire screen:
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
var mainWindow;
app.on('ready', function() {
var electronScreen = electron.screen;
var size = electronScreen.getPrimaryDisplay().workAreaSize;
mainWindow = new BrowserWindow({ width: size.width, height: size.height });
});
The Screen module has the following events:
The event | describe |
---|---|
display-added | When adding thenewDisplay Emit event when |
display-removed | When moving outoldDisplay Emit event when |
display-metrics-changed | When adisplay One or more of thesemetrics An event is emitted when the change occurs |
The Screen module has the following method:
methods | describe |
---|---|
getCursorScreenPoint | Returns the absolute path of the current mouse |
getPrimaryDisplay | Return to the primarydisplay |
getAllDisplays | Returns a currently availabledisplay An array of |
getDisplayMatching | Returns the one most closely related to the provided boundary rangedisplay |
getDisplayNearestPoint | Returns the nearest to the specified pointdisplay |
Shell module
The shell module provides the associated functionality to integrate with other desktop clients.
Example:
Open the URL in the user default browser:
var shell = require('shell');
shell.openExternal('https://www.9xkd.com/');
The shell module’s method looks like this:
methods | describe |
---|---|
showItemInFolde | Open the folder where the file resides, which is normally selected as well |
openIte | Open the file with default open mode |
openExterna | Turn on external protocols with system default Settings |
moveItemToTra | Deletes the specified path file and returns the status value of this operation (Boolean type) |
beep | Play BEEP sound |