📝 Update API documentation to ES6 [ci skip]
This commit is contained in:
parent
178496afe5
commit
5a9f28e034
28 changed files with 168 additions and 176 deletions
|
@ -6,8 +6,8 @@ The following example shows how to quit the application when the last window is
|
|||
closed:
|
||||
|
||||
```javascript
|
||||
const app = require('electron').app;
|
||||
app.on('window-all-closed', function() {
|
||||
const { app } = require('electron');
|
||||
app.on('window-all-closed', () => {
|
||||
app.quit();
|
||||
});
|
||||
```
|
||||
|
@ -218,7 +218,7 @@ should prevent the default behavior with `event.preventDefault()` and call
|
|||
`callback(username, password)` with the credentials.
|
||||
|
||||
```javascript
|
||||
app.on('login', function(event, webContents, request, authInfo, callback) {
|
||||
app.on('login', (event, webContents, request, authInfo, callback) => {
|
||||
event.preventDefault();
|
||||
callback('username', 'secret');
|
||||
})
|
||||
|
@ -462,9 +462,9 @@ An example of activating the window of primary instance when a second instance
|
|||
starts:
|
||||
|
||||
```javascript
|
||||
var myWindow = null;
|
||||
let myWindow = null;
|
||||
|
||||
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
|
||||
const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
|
||||
// Someone tried to run a second instance, we should focus our window.
|
||||
if (myWindow) {
|
||||
if (myWindow.isMinimized()) myWindow.restore();
|
||||
|
@ -478,7 +478,7 @@ if (shouldQuit) {
|
|||
}
|
||||
|
||||
// Create myWindow, load the rest of the app, etc...
|
||||
app.on('ready', function() {
|
||||
app.on('ready', () => {
|
||||
});
|
||||
```
|
||||
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
|
||||
```javascript
|
||||
// In the main process.
|
||||
const BrowserWindow = require('electron').BrowserWindow;
|
||||
const { BrowserWindow } = require('electron');
|
||||
|
||||
// Or in the renderer process.
|
||||
const BrowserWindow = require('electron').remote.BrowserWindow;
|
||||
const { BrowserWindow } = require('electron').remote;
|
||||
|
||||
var win = new BrowserWindow({ width: 800, height: 600, show: false });
|
||||
win.on('closed', function() {
|
||||
let win = new BrowserWindow({ width: 800, height: 600, show: false });
|
||||
win.on('closed', () => {
|
||||
win = null;
|
||||
});
|
||||
|
||||
|
@ -315,7 +315,7 @@ Commands are lowercased with underscores replaced with hyphens and the
|
|||
e.g. `APPCOMMAND_BROWSER_BACKWARD` is emitted as `browser-backward`.
|
||||
|
||||
```javascript
|
||||
someWindow.on('app-command', function(e, cmd) {
|
||||
someWindow.on('app-command', (e, cmd) => {
|
||||
// Navigate the window back when the user hits their mouse back button
|
||||
if (cmd === 'browser-backward' && someWindow.webContents.canGoBack()) {
|
||||
someWindow.webContents.goBack();
|
||||
|
|
|
@ -7,11 +7,11 @@ your app's main script before the [ready][ready] event of the [app][app] module
|
|||
is emitted:
|
||||
|
||||
```javascript
|
||||
const app = require('electron').app;
|
||||
const { app } = require('electron');
|
||||
app.commandLine.appendSwitch('remote-debugging-port', '8315');
|
||||
app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1');
|
||||
|
||||
app.on('ready', function() {
|
||||
app.on('ready', () => {
|
||||
// Your code here
|
||||
});
|
||||
```
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
The following example shows how to write a string to the clipboard:
|
||||
|
||||
```javascript
|
||||
const clipboard = require('electron').clipboard;
|
||||
const { clipboard } = require('electron');
|
||||
clipboard.writeText('Example String');
|
||||
```
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ This module does not include a web interface so you need to open
|
|||
result.
|
||||
|
||||
```javascript
|
||||
const contentTracing = require('electron').contentTracing;
|
||||
const { contentTracing } = require('electron');
|
||||
|
||||
const options = {
|
||||
categoryFilter: '*',
|
||||
|
@ -18,8 +18,8 @@ const options = {
|
|||
contentTracing.startRecording(options, function() {
|
||||
console.log('Tracing started');
|
||||
|
||||
setTimeout(function() {
|
||||
contentTracing.stopRecording('', function(path) {
|
||||
setTimeout(() => {
|
||||
contentTracing.stopRecording('', (path) => {
|
||||
console.log('Tracing data recorded to ' + path);
|
||||
});
|
||||
}, 5000);
|
||||
|
|
|
@ -6,7 +6,7 @@ The following is an example of automatically submitting a crash report to a
|
|||
remote server:
|
||||
|
||||
```javascript
|
||||
const crashReporter = require('electron').crashReporter;
|
||||
const { crashReporter } = require('electron');
|
||||
|
||||
crashReporter.start({
|
||||
productName: 'YourName',
|
||||
|
|
|
@ -5,9 +5,9 @@ microphone, camera, or screen.
|
|||
|
||||
```javascript
|
||||
// In the renderer process.
|
||||
var desktopCapturer = require('electron').desktopCapturer;
|
||||
var { desktopCapturer } = require('electron');
|
||||
|
||||
desktopCapturer.getSources({types: ['window', 'screen']}, function(error, sources) {
|
||||
desktopCapturer.getSources({types: ['window', 'screen']}, (error, sources) => {
|
||||
if (error) throw error;
|
||||
for (var i = 0; i < sources.length; ++i) {
|
||||
if (sources[i].name == "Electron") {
|
||||
|
|
|
@ -6,7 +6,7 @@ An example of showing a dialog to select multiple files and directories:
|
|||
|
||||
```javascript
|
||||
var win = ...; // BrowserWindow in which to show the dialog
|
||||
const dialog = require('electron').dialog;
|
||||
const { dialog } = require('electron');
|
||||
console.log(dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', 'multiSelections' ]}));
|
||||
```
|
||||
|
||||
|
@ -14,7 +14,7 @@ The Dialog is opened from Electron's main thread. If you want to use the dialog
|
|||
object from a renderer process, remember to access it using the remote:
|
||||
|
||||
```javascript
|
||||
const dialog = require('electron').remote.dialog;
|
||||
const { dialog } = require('electron').remote;
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
|
|
@ -8,17 +8,17 @@ control the download item.
|
|||
|
||||
```javascript
|
||||
// In the main process.
|
||||
win.webContents.session.on('will-download', function(event, item, webContents) {
|
||||
win.webContents.session.on('will-download', (event, item, webContents) => {
|
||||
// Set the save path, making Electron not to prompt a save dialog.
|
||||
item.setSavePath('/tmp/save.pdf');
|
||||
console.log(item.getMimeType());
|
||||
console.log(item.getFilename());
|
||||
console.log(item.getTotalBytes());
|
||||
item.on('updated', function() {
|
||||
item.on('updated', () => {
|
||||
console.log('Received bytes: ' + item.getReceivedBytes());
|
||||
});
|
||||
item.on('done', function(e, state) {
|
||||
if (state == "completed") {
|
||||
item.on('done', (e, state) => {
|
||||
if (state === "completed") {
|
||||
console.log("Download successfully");
|
||||
} else {
|
||||
console.log("Download is cancelled or interrupted that can't be resumed");
|
||||
|
|
|
@ -15,16 +15,16 @@ Example on getting a real path from a dragged-onto-the-app file:
|
|||
</div>
|
||||
|
||||
<script>
|
||||
var holder = document.getElementById('holder');
|
||||
holder.ondragover = function () {
|
||||
const holder = document.getElementById('holder');
|
||||
holder.ondragover = () => {
|
||||
return false;
|
||||
};
|
||||
holder.ondragleave = holder.ondragend = function () {
|
||||
holder.ondragleave = holder.ondragend = () => {
|
||||
return false;
|
||||
};
|
||||
holder.ondrop = function (e) {
|
||||
holder.ondrop = (e) => {
|
||||
e.preventDefault();
|
||||
var file = e.dataTransfer.files[0];
|
||||
const file = e.dataTransfer.files[0];
|
||||
console.log('File you dragged here is', file.path);
|
||||
return false;
|
||||
};
|
||||
|
|
|
@ -14,8 +14,8 @@ To create a frameless window, you need to set `frame` to `false` in
|
|||
|
||||
|
||||
```javascript
|
||||
const BrowserWindow = require('electron').BrowserWindow;
|
||||
var win = new BrowserWindow({ width: 800, height: 600, frame: false });
|
||||
const { BrowserWindow } = require('electron');
|
||||
let win = new BrowserWindow({ width: 800, height: 600, frame: false });
|
||||
```
|
||||
|
||||
### Alternatives on OS X
|
||||
|
@ -28,7 +28,7 @@ the window controls ("traffic lights") for standard window actions.
|
|||
You can do so by specifying the new `titleBarStyle` option:
|
||||
|
||||
```javascript
|
||||
var win = new BrowserWindow({ 'titleBarStyle': 'hidden' });
|
||||
let win = new BrowserWindow({ 'titleBarStyle': 'hidden' });
|
||||
```
|
||||
|
||||
## Transparent window
|
||||
|
@ -37,7 +37,7 @@ By setting the `transparent` option to `true`, you can also make the frameless
|
|||
window transparent:
|
||||
|
||||
```javascript
|
||||
var win = new BrowserWindow({ transparent: true, frame: false });
|
||||
let win = new BrowserWindow({ transparent: true, frame: false });
|
||||
```
|
||||
|
||||
### Limitations
|
||||
|
|
|
@ -12,12 +12,11 @@ event of the app module is emitted.
|
|||
|
||||
```javascript
|
||||
const electron = require('electron');
|
||||
const app = electron.app;
|
||||
const globalShortcut = electron.globalShortcut;
|
||||
const { app, globalShortcut } = electron;
|
||||
|
||||
app.on('ready', function() {
|
||||
app.on('ready', () => {
|
||||
// Register a 'CommandOrControl+X' shortcut listener.
|
||||
var ret = globalShortcut.register('CommandOrControl+X', function() {
|
||||
const ret = globalShortcut.register('CommandOrControl+X', function() {
|
||||
console.log('CommandOrControl+X is pressed');
|
||||
});
|
||||
|
||||
|
@ -29,7 +28,7 @@ app.on('ready', function() {
|
|||
console.log(globalShortcut.isRegistered('CommandOrControl+X'));
|
||||
});
|
||||
|
||||
app.on('will-quit', function() {
|
||||
app.on('will-quit', () => {
|
||||
// Unregister a shortcut.
|
||||
globalShortcut.unregister('CommandOrControl+X');
|
||||
|
||||
|
|
|
@ -23,13 +23,13 @@ processes:
|
|||
|
||||
```javascript
|
||||
// In main process.
|
||||
const ipcMain = require('electron').ipcMain;
|
||||
ipcMain.on('asynchronous-message', function(event, arg) {
|
||||
const { ipcMain } = require('electron');
|
||||
ipcMain.on('asynchronous-message', (event, arg) => {
|
||||
console.log(arg); // prints "ping"
|
||||
event.sender.send('asynchronous-reply', 'pong');
|
||||
});
|
||||
|
||||
ipcMain.on('synchronous-message', function(event, arg) {
|
||||
ipcMain.on('synchronous-message', (event, arg) => {
|
||||
console.log(arg); // prints "ping"
|
||||
event.returnValue = 'pong';
|
||||
});
|
||||
|
@ -37,10 +37,10 @@ ipcMain.on('synchronous-message', function(event, arg) {
|
|||
|
||||
```javascript
|
||||
// In renderer process (web page).
|
||||
const ipcRenderer = require('electron').ipcRenderer;
|
||||
const { ipcRenderer } = require('electron');
|
||||
console.log(ipcRenderer.sendSync('synchronous-message', 'ping')); // prints "pong"
|
||||
|
||||
ipcRenderer.on('asynchronous-reply', function(event, arg) {
|
||||
ipcRenderer.on('asynchronous-reply', (event, arg) => {
|
||||
console.log(arg); // prints "pong"
|
||||
});
|
||||
ipcRenderer.send('asynchronous-message', 'ping');
|
||||
|
|
|
@ -15,16 +15,14 @@ the user right clicks the page:
|
|||
```html
|
||||
<!-- index.html -->
|
||||
<script>
|
||||
const remote = require('electron').remote;
|
||||
const Menu = remote.Menu;
|
||||
const MenuItem = remote.MenuItem;
|
||||
const { Menu, MenuItem } = require('electron').remote;
|
||||
|
||||
var menu = new Menu();
|
||||
menu.append(new MenuItem({ label: 'MenuItem1', click: function() { console.log('item 1 clicked'); } }));
|
||||
const menu = new Menu();
|
||||
menu.append(new MenuItem({ label: 'MenuItem1', click: () => { console.log('item 1 clicked'); } }));
|
||||
menu.append(new MenuItem({ type: 'separator' }));
|
||||
menu.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox', checked: true }));
|
||||
|
||||
window.addEventListener('contextmenu', function (e) {
|
||||
window.addEventListener('contextmenu', (e) => {
|
||||
e.preventDefault();
|
||||
menu.popup(remote.getCurrentWindow());
|
||||
}, false);
|
||||
|
@ -35,7 +33,7 @@ An example of creating the application menu in the render process with the
|
|||
simple template API:
|
||||
|
||||
```javascript
|
||||
var template = [
|
||||
const template = [
|
||||
{
|
||||
label: 'Edit',
|
||||
submenu: [
|
||||
|
@ -80,33 +78,32 @@ var template = [
|
|||
{
|
||||
label: 'Reload',
|
||||
accelerator: 'CmdOrCtrl+R',
|
||||
click: function(item, focusedWindow) {
|
||||
if (focusedWindow)
|
||||
focusedWindow.reload();
|
||||
click: (item, focusedWindow) => {
|
||||
if (focusedWindow) focusedWindow.reload();
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Toggle Full Screen',
|
||||
accelerator: (function() {
|
||||
if (process.platform == 'darwin')
|
||||
accelerator: (() => {
|
||||
if (process.platform === 'darwin')
|
||||
return 'Ctrl+Command+F';
|
||||
else
|
||||
return 'F11';
|
||||
})(),
|
||||
click: function(item, focusedWindow) {
|
||||
click: (item, focusedWindow) => {
|
||||
if (focusedWindow)
|
||||
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
|
||||
}
|
||||
},
|
||||
{
|
||||
label: 'Toggle Developer Tools',
|
||||
accelerator: (function() {
|
||||
accelerator: (() => {
|
||||
if (process.platform == 'darwin')
|
||||
return 'Alt+Command+I';
|
||||
else
|
||||
return 'Ctrl+Shift+I';
|
||||
})(),
|
||||
click: function(item, focusedWindow) {
|
||||
click: (item, focusedWindow) => {
|
||||
if (focusedWindow)
|
||||
focusedWindow.webContents.toggleDevTools();
|
||||
}
|
||||
|
@ -135,14 +132,14 @@ var template = [
|
|||
submenu: [
|
||||
{
|
||||
label: 'Learn More',
|
||||
click: function() { require('electron').shell.openExternal('http://electron.atom.io') }
|
||||
click: () => { require('electron').shell.openExternal('http://electron.atom.io') }
|
||||
},
|
||||
]
|
||||
},
|
||||
];
|
||||
|
||||
if (process.platform == 'darwin') {
|
||||
var name = require('electron').remote.app.getName();
|
||||
if (process.platform === 'darwin') {
|
||||
const name = require('electron').remote.app.getName();
|
||||
template.unshift({
|
||||
label: name,
|
||||
submenu: [
|
||||
|
@ -181,7 +178,7 @@ if (process.platform == 'darwin') {
|
|||
{
|
||||
label: 'Quit',
|
||||
accelerator: 'Command+Q',
|
||||
click: function() { app.quit(); }
|
||||
click: () => { app.quit(); }
|
||||
},
|
||||
]
|
||||
});
|
||||
|
@ -197,7 +194,7 @@ if (process.platform == 'darwin') {
|
|||
);
|
||||
}
|
||||
|
||||
var menu = Menu.buildFromTemplate(template);
|
||||
const menu = Menu.buildFromTemplate(template);
|
||||
Menu.setApplicationMenu(menu);
|
||||
```
|
||||
|
||||
|
|
|
@ -9,15 +9,15 @@ For example, when creating a tray or setting a window's icon, you can pass an
|
|||
image file path as a `String`:
|
||||
|
||||
```javascript
|
||||
var appIcon = new Tray('/Users/somebody/images/icon.png');
|
||||
var window = new BrowserWindow({icon: '/Users/somebody/images/window.png'});
|
||||
const appIcon = new Tray('/Users/somebody/images/icon.png');
|
||||
let window = new BrowserWindow({icon: '/Users/somebody/images/window.png'});
|
||||
```
|
||||
|
||||
Or read the image from the clipboard which returns a `nativeImage`:
|
||||
|
||||
```javascript
|
||||
var image = clipboard.readImage();
|
||||
var appIcon = new Tray(image);
|
||||
const image = clipboard.readImage();
|
||||
const appIcon = new Tray(image);
|
||||
```
|
||||
|
||||
## Supported Formats
|
||||
|
|
|
@ -8,8 +8,8 @@ event of the `app` module is emitted.
|
|||
For example:
|
||||
|
||||
```javascript
|
||||
app.on('ready', function() {
|
||||
require('electron').powerMonitor.on('suspend', function() {
|
||||
app.on('ready', () => {
|
||||
require('electron').powerMonitor.on('suspend', () => {
|
||||
console.log('The system is going to sleep');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,9 +5,9 @@
|
|||
For example:
|
||||
|
||||
```javascript
|
||||
const powerSaveBlocker = require('electron').powerSaveBlocker;
|
||||
const { powerSaveBlocker } = require('electron');
|
||||
|
||||
var id = powerSaveBlocker.start('prevent-display-sleep');
|
||||
const id = powerSaveBlocker.start('prevent-display-sleep');
|
||||
console.log(powerSaveBlocker.isStarted(id));
|
||||
|
||||
powerSaveBlocker.stop(id);
|
||||
|
|
|
@ -27,8 +27,8 @@ the global scope when node integration is turned off:
|
|||
|
||||
```javascript
|
||||
// preload.js
|
||||
var _setImmediate = setImmediate;
|
||||
var _clearImmediate = clearImmediate;
|
||||
const _setImmediate = setImmediate;
|
||||
const _clearImmediate = clearImmediate;
|
||||
process.once('loaded', function() {
|
||||
global.setImmediate = _setImmediate;
|
||||
global.clearImmediate = _clearImmediate;
|
||||
|
|
|
@ -7,13 +7,13 @@ An example of implementing a protocol that has the same effect as the
|
|||
|
||||
```javascript
|
||||
const electron = require('electron');
|
||||
const app = electron.app;
|
||||
const { app } = electron;
|
||||
const path = require('path');
|
||||
|
||||
app.on('ready', function() {
|
||||
var protocol = electron.protocol;
|
||||
const { protocol } = electron;
|
||||
protocol.registerFileProtocol('atom', function(request, callback) {
|
||||
var url = request.url.substr(7);
|
||||
const url = request.url.substr(7);
|
||||
callback({path: path.normalize(__dirname + '/' + url)});
|
||||
}, function (error) {
|
||||
if (error)
|
||||
|
@ -95,9 +95,9 @@ should be called with either a `Buffer` object or an object that has the `data`,
|
|||
Example:
|
||||
|
||||
```javascript
|
||||
protocol.registerBufferProtocol('atom', function(request, callback) {
|
||||
protocol.registerBufferProtocol('atom', (request, callback) => {
|
||||
callback({mimeType: 'text/html', data: new Buffer('<h5>Response</h5>')});
|
||||
}, function (error) {
|
||||
}, (error) => {
|
||||
if (error)
|
||||
console.error('Failed to register protocol')
|
||||
});
|
||||
|
|
|
@ -14,8 +14,7 @@ similar to Java's [RMI][rmi]. An example of creating a browser window from a
|
|||
renderer process:
|
||||
|
||||
```javascript
|
||||
const remote = require('electron').remote;
|
||||
const BrowserWindow = remote.BrowserWindow;
|
||||
const { BrowserWindow } = require('electron').remote;
|
||||
|
||||
var win = new BrowserWindow({ width: 800, height: 600 });
|
||||
win.loadURL('https://github.com');
|
||||
|
@ -69,26 +68,22 @@ For instance you can't use a function from the renderer process in an
|
|||
|
||||
```javascript
|
||||
// main process mapNumbers.js
|
||||
exports.withRendererCallback = function(mapper) {
|
||||
exports.withRendererCallback = (mapper) => {
|
||||
return [1,2,3].map(mapper);
|
||||
}
|
||||
|
||||
exports.withLocalCallback = function() {
|
||||
return exports.mapNumbers(function(x) {
|
||||
return x + 1;
|
||||
});
|
||||
exports.withLocalCallback = () => {
|
||||
return exports.mapNumbers(x => x + 1);
|
||||
}
|
||||
```
|
||||
|
||||
```javascript
|
||||
// renderer process
|
||||
var mapNumbers = require("remote").require("./mapNumbers");
|
||||
const mapNumbers = require("remote").require("./mapNumbers");
|
||||
|
||||
var withRendererCb = mapNumbers.withRendererCallback(function(x) {
|
||||
return x + 1;
|
||||
})
|
||||
const withRendererCb = mapNumbers.withRendererCallback(x => x + 1);
|
||||
|
||||
var withLocalCb = mapNumbers.withLocalCallback()
|
||||
const withLocalCb = mapNumbers.withLocalCallback()
|
||||
|
||||
console.log(withRendererCb, withLocalCb) // [true, true, true], [2, 3, 4]
|
||||
```
|
||||
|
@ -104,7 +99,7 @@ For example, the following code seems innocent at first glance. It installs a
|
|||
callback for the `close` event on a remote object:
|
||||
|
||||
```javascript
|
||||
remote.getCurrentWindow().on('close', function() {
|
||||
remote.getCurrentWindow().on('close', () => {
|
||||
// blabla...
|
||||
});
|
||||
```
|
||||
|
|
|
@ -14,15 +14,13 @@ An example of creating a window that fills the whole screen:
|
|||
|
||||
```javascript
|
||||
const electron = require('electron');
|
||||
const app = electron.app;
|
||||
const BrowserWindow = electron.BrowserWindow;
|
||||
const { app, BrowserWindow } = electron;
|
||||
|
||||
var mainWindow;
|
||||
let mainWindow;
|
||||
|
||||
app.on('ready', function() {
|
||||
var electronScreen = electron.screen;
|
||||
var size = electronScreen.getPrimaryDisplay().workAreaSize;
|
||||
mainWindow = new BrowserWindow({ width: size.width, height: size.height });
|
||||
app.on('ready', () => {
|
||||
const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize;
|
||||
mainWindow = new BrowserWindow({ width, height });
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -30,16 +28,15 @@ Another example of creating a window in the external display:
|
|||
|
||||
```javascript
|
||||
const electron = require('electron');
|
||||
const app = electron.app;
|
||||
const BrowserWindow = electron.BrowserWindow;
|
||||
const { app, BrowserWindow } = electron;
|
||||
|
||||
var mainWindow;
|
||||
let mainWindow;
|
||||
|
||||
app.on('ready', function() {
|
||||
app.on('ready', () => {
|
||||
var electronScreen = electron.screen;
|
||||
var displays = electronScreen.getAllDisplays();
|
||||
var externalDisplay = null;
|
||||
for (var i in displays) {
|
||||
for (let i in displays) {
|
||||
if (displays[i].bounds.x != 0 || displays[i].bounds.y != 0) {
|
||||
externalDisplay = displays[i];
|
||||
break;
|
||||
|
|
|
@ -9,12 +9,12 @@ property of [`webContents`](web-contents.md) which is a property of
|
|||
[`BrowserWindow`](browser-window.md).
|
||||
|
||||
```javascript
|
||||
const BrowserWindow = require('electron').BrowserWindow;
|
||||
const { BrowserWindow } = require('electron');
|
||||
|
||||
var win = new BrowserWindow({ width: 800, height: 600 });
|
||||
let win = new BrowserWindow({ width: 800, height: 600 });
|
||||
win.loadURL("http://github.com");
|
||||
|
||||
var ses = win.webContents.session;
|
||||
const ses = win.webContents.session;
|
||||
```
|
||||
|
||||
## Methods
|
||||
|
@ -47,7 +47,7 @@ You can create a `Session` object in the `session` module:
|
|||
```javascript
|
||||
const session = require('electron').session;
|
||||
|
||||
var ses = session.fromPartition('persist:name');
|
||||
const ses = session.fromPartition('persist:name');
|
||||
```
|
||||
|
||||
### Instance Events
|
||||
|
@ -66,9 +66,9 @@ Calling `event.preventDefault()` will cancel the download and `item` will not be
|
|||
available from next tick of the process.
|
||||
|
||||
```javascript
|
||||
session.defaultSession.on('will-download', function(event, item, webContents) {
|
||||
session.defaultSession.on('will-download', (event, item, webContents) => {
|
||||
event.preventDefault();
|
||||
require('request')(item.getURL(), function(data) {
|
||||
require('request')(item.getURL(), (data) => {
|
||||
require('fs').writeFileSync('/somewhere', data);
|
||||
});
|
||||
});
|
||||
|
@ -84,19 +84,19 @@ The `cookies` gives you ability to query and modify cookies. For example:
|
|||
|
||||
```javascript
|
||||
// Query all cookies.
|
||||
session.defaultSession.cookies.get({}, function(error, cookies) {
|
||||
session.defaultSession.cookies.get({}, (error, cookies) => {
|
||||
console.log(cookies);
|
||||
});
|
||||
|
||||
// Query all cookies associated with a specific url.
|
||||
session.defaultSession.cookies.get({ url : "http://www.github.com" }, function(error, cookies) {
|
||||
session.defaultSession.cookies.get({ url : "http://www.github.com" }, (error, cookies) => {
|
||||
console.log(cookies);
|
||||
});
|
||||
|
||||
// Set a cookie with the given cookie data;
|
||||
// may overwrite equivalent cookies if they exist.
|
||||
var cookie = { url : "http://www.github.com", name : "dummy_name", value : "dummy" };
|
||||
session.defaultSession.cookies.set(cookie, function(error) {
|
||||
const cookie = { url : "http://www.github.com", name : "dummy_name", value : "dummy" };
|
||||
session.defaultSession.cookies.set(cookie, (error) => {
|
||||
if (error)
|
||||
console.error(error);
|
||||
});
|
||||
|
@ -285,8 +285,8 @@ Calling `setCertificateVerifyProc(null)` will revert back to default certificate
|
|||
verify proc.
|
||||
|
||||
```javascript
|
||||
myWindow.webContents.session.setCertificateVerifyProc(function(hostname, cert, callback) {
|
||||
if (hostname == 'github.com')
|
||||
myWindow.webContents.session.setCertificateVerifyProc((hostname, cert, callback) => {
|
||||
if (hostname === 'github.com')
|
||||
callback(true);
|
||||
else
|
||||
callback(false);
|
||||
|
@ -305,9 +305,9 @@ Sets the handler which can be used to respond to permission requests for the `se
|
|||
Calling `callback(true)` will allow the permission and `callback(false)` will reject it.
|
||||
|
||||
```javascript
|
||||
session.fromPartition(partition).setPermissionRequestHandler(function(webContents, permission, callback) {
|
||||
session.fromPartition(partition).setPermissionRequestHandler((webContents, permission, callback) => {
|
||||
if (webContents.getURL() === host) {
|
||||
if (permission == "notifications") {
|
||||
if (permission === "notifications") {
|
||||
callback(false); // denied.
|
||||
return;
|
||||
}
|
||||
|
@ -342,11 +342,11 @@ called with an `response` object when `listener` has done its work.
|
|||
|
||||
```javascript
|
||||
// Modify the user agent for all requests to the following urls.
|
||||
var filter = {
|
||||
const filter = {
|
||||
urls: ["https://*.github.com/*", "*://electron.github.io"]
|
||||
};
|
||||
|
||||
session.defaultSession.webRequest.onBeforeSendHeaders(filter, function(details, callback) {
|
||||
session.defaultSession.webRequest.onBeforeSendHeaders(filter, (details, callback) => {
|
||||
details.requestHeaders['User-Agent'] = "MyAgent";
|
||||
callback({cancel: false, requestHeaders: details.requestHeaders});
|
||||
});
|
||||
|
|
|
@ -7,7 +7,7 @@ The `shell` module provides functions related to desktop integration.
|
|||
An example of opening a URL in the user's default browser:
|
||||
|
||||
```javascript
|
||||
const shell = require('electron').shell;
|
||||
const { shell } = require('electron');
|
||||
|
||||
shell.openExternal('https://github.com');
|
||||
```
|
||||
|
|
|
@ -8,10 +8,10 @@ const app = electron.app;
|
|||
const Menu = electron.Menu;
|
||||
const Tray = electron.Tray;
|
||||
|
||||
var appIcon = null;
|
||||
app.on('ready', function(){
|
||||
let appIcon = null;
|
||||
app.on('ready', () => {
|
||||
appIcon = new Tray('/path/to/my/icon');
|
||||
var contextMenu = Menu.buildFromTemplate([
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{ label: 'Item1', type: 'radio' },
|
||||
{ label: 'Item2', type: 'radio' },
|
||||
{ label: 'Item3', type: 'radio', checked: true },
|
||||
|
@ -20,7 +20,6 @@ app.on('ready', function(){
|
|||
appIcon.setToolTip('This is my application.');
|
||||
appIcon.setContextMenu(contextMenu);
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
__Platform limitations:__
|
||||
|
|
|
@ -9,12 +9,12 @@ the [`BrowserWindow`](browser-window.md) object. An example of accessing the
|
|||
`webContents` object:
|
||||
|
||||
```javascript
|
||||
const BrowserWindow = require('electron').BrowserWindow;
|
||||
const { BrowserWindow } = require('electron');
|
||||
|
||||
var win = new BrowserWindow({width: 800, height: 1500});
|
||||
let win = new BrowserWindow({width: 800, height: 1500});
|
||||
win.loadURL("http://github.com");
|
||||
|
||||
var webContents = win.webContents;
|
||||
let webContents = win.webContents;
|
||||
```
|
||||
|
||||
## Events
|
||||
|
@ -341,10 +341,10 @@ Initiates a download of the resource at `url` without navigating. The
|
|||
Returns URL of the current web page.
|
||||
|
||||
```javascript
|
||||
var win = new BrowserWindow({width: 800, height: 600});
|
||||
let win = new BrowserWindow({width: 800, height: 600});
|
||||
win.loadURL("http://github.com");
|
||||
|
||||
var currentURL = win.webContents.getURL();
|
||||
let currentURL = win.webContents.getURL();
|
||||
```
|
||||
|
||||
### `webContents.getTitle()`
|
||||
|
@ -614,22 +614,22 @@ By default, an empty `options` will be regarded as:
|
|||
```
|
||||
|
||||
```javascript
|
||||
const BrowserWindow = require('electron').BrowserWindow;
|
||||
const { BrowserWindow } = require('electron');
|
||||
const fs = require('fs');
|
||||
|
||||
var win = new BrowserWindow({width: 800, height: 600});
|
||||
win.loadURL("http://github.com");
|
||||
let win = new BrowserWindow({width: 800, height: 600});
|
||||
win.loadURL('http://github.com');
|
||||
|
||||
win.webContents.on("did-finish-load", function() {
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
// Use default printing options
|
||||
win.webContents.printToPDF({}, function(error, data) {
|
||||
win.webContents.printToPDF({}, (error, data) => {
|
||||
if (error) throw error;
|
||||
fs.writeFile("/tmp/print.pdf", data, function(error) {
|
||||
fs.writeFile('/tmp/print.pdf', data, (error) => {
|
||||
if (error)
|
||||
throw error;
|
||||
console.log("Write PDF successfully.");
|
||||
})
|
||||
})
|
||||
console.log('Write PDF successfully.');
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -704,12 +704,13 @@ An example of sending messages from the main process to the renderer process:
|
|||
|
||||
```javascript
|
||||
// In the main process.
|
||||
var window = null;
|
||||
app.on('ready', function() {
|
||||
window = new BrowserWindow({width: 800, height: 600});
|
||||
window.loadURL('file://' + __dirname + '/index.html');
|
||||
window.webContents.on('did-finish-load', function() {
|
||||
window.webContents.send('ping', 'whoooooooh!');
|
||||
let mainWindow = null;
|
||||
|
||||
app.on('ready', () => {
|
||||
mainWindow = new BrowserWindow({width: 800, height: 600});
|
||||
mainWindow.loadURL(`file://${__dirname}/index.html`);
|
||||
mainWindow.webContents.on('did-finish-load', () => {
|
||||
mainWindow.webContents.send('ping', 'whoooooooh!');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
@ -719,7 +720,7 @@ app.on('ready', function() {
|
|||
<html>
|
||||
<body>
|
||||
<script>
|
||||
require('electron').ipcRenderer.on('ping', function(event, message) {
|
||||
require('electron').ipcRenderer.on('ping', (event, message) => {
|
||||
console.log(message); // Prints "whoooooooh!"
|
||||
});
|
||||
</script>
|
||||
|
@ -835,8 +836,8 @@ Returns true if the process of saving page has been initiated successfully.
|
|||
```javascript
|
||||
win.loadURL('https://github.com');
|
||||
|
||||
win.webContents.on('did-finish-load', function() {
|
||||
win.webContents.savePage('/tmp/test.html', 'HTMLComplete', function(error) {
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
win.webContents.savePage('/tmp/test.html', 'HTMLComplete', (error) => {
|
||||
if (!error)
|
||||
console.log("Save page successfully");
|
||||
});
|
||||
|
@ -873,13 +874,13 @@ try {
|
|||
console.log("Debugger attach failed : ", err);
|
||||
};
|
||||
|
||||
win.webContents.debugger.on('detach', function(event, reason) {
|
||||
win.webContents.debugger.on('detach', (event, reason) => {
|
||||
console.log("Debugger detached due to : ", reason);
|
||||
});
|
||||
|
||||
win.webContents.debugger.on('message', function(event, method, params) {
|
||||
if (method == "Network.requestWillBeSent") {
|
||||
if (params.request.url == "https://www.github.com")
|
||||
win.webContents.debugger.on('message', (event, method, params) => {
|
||||
if (method === "Network.requestWillBeSent") {
|
||||
if (params.request.url === "https://www.github.com")
|
||||
win.webContents.debugger.detach();
|
||||
}
|
||||
})
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
An example of zooming current page to 200%.
|
||||
|
||||
```javascript
|
||||
var webFrame = require('electron').webFrame;
|
||||
const { webFrame } = require('electron');
|
||||
|
||||
webFrame.setZoomFactor(2);
|
||||
```
|
||||
|
|
|
@ -31,18 +31,20 @@ and displays a "loading..." message during the load time:
|
|||
|
||||
```html
|
||||
<script>
|
||||
onload = function() {
|
||||
var webview = document.getElementById("foo");
|
||||
var indicator = document.querySelector(".indicator");
|
||||
onload = () => {
|
||||
const webview = document.getElementById('foo');
|
||||
const indicator = document.querySelector('.indicator');
|
||||
|
||||
var loadstart = function() {
|
||||
indicator.innerText = "loading...";
|
||||
const loadstart = () => {
|
||||
indicator.innerText = 'loading...';
|
||||
}
|
||||
var loadstop = function() {
|
||||
indicator.innerText = "";
|
||||
|
||||
const loadstop = () => {
|
||||
indicator.innerText = '';
|
||||
}
|
||||
webview.addEventListener("did-start-loading", loadstart);
|
||||
webview.addEventListener("did-stop-loading", loadstop);
|
||||
|
||||
webview.addEventListener('did-start-loading', loadstart);
|
||||
webview.addEventListener('did-stop-loading', loadstop);
|
||||
}
|
||||
</script>
|
||||
```
|
||||
|
@ -211,7 +213,7 @@ The `webview` tag has the following methods:
|
|||
**Example**
|
||||
|
||||
```javascript
|
||||
webview.addEventListener("dom-ready", function() {
|
||||
webview.addEventListener("dom-ready", () => {
|
||||
webview.openDevTools();
|
||||
});
|
||||
```
|
||||
|
@ -642,10 +644,12 @@ Fired when the guest page attempts to open a new browser window.
|
|||
The following example code opens the new url in system's default browser.
|
||||
|
||||
```javascript
|
||||
webview.addEventListener('new-window', function(e) {
|
||||
var protocol = require('url').parse(e.url).protocol;
|
||||
const { shell } = require('electron');
|
||||
|
||||
webview.addEventListener('new-window', (e) => {
|
||||
const protocol = require('url').parse(e.url).protocol;
|
||||
if (protocol === 'http:' || protocol === 'https:') {
|
||||
require('electron').shell.openExternal(e.url);
|
||||
shell.openExternal(e.url);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
@ -700,7 +704,7 @@ The following example code navigates the `webview` to `about:blank` when the
|
|||
guest attempts to close itself.
|
||||
|
||||
```javascript
|
||||
webview.addEventListener('close', function() {
|
||||
webview.addEventListener('close', () => {
|
||||
webview.src = 'about:blank';
|
||||
});
|
||||
```
|
||||
|
@ -719,7 +723,7 @@ between guest page and embedder page:
|
|||
|
||||
```javascript
|
||||
// In embedder page.
|
||||
webview.addEventListener('ipc-message', function(event) {
|
||||
webview.addEventListener('ipc-message', (event) => {
|
||||
console.log(event.channel);
|
||||
// Prints "pong"
|
||||
});
|
||||
|
@ -728,8 +732,8 @@ webview.send('ping');
|
|||
|
||||
```javascript
|
||||
// In guest page.
|
||||
var ipcRenderer = require('electron').ipcRenderer;
|
||||
ipcRenderer.on('ping', function() {
|
||||
var { ipcRenderer } = require('electron');
|
||||
ipcRenderer.on('ping', () => {
|
||||
ipcRenderer.sendToHost('pong');
|
||||
});
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue