From 5a9f28e034330406164de6a7a846b99113f4eaa4 Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Wed, 4 May 2016 11:59:02 -0600 Subject: [PATCH] :memo: Update API documentation to ES6 [ci skip] --- docs/api/app.md | 20 ++++----- docs/api/browser-window.md | 10 ++--- docs/api/chrome-command-line-switches.md | 4 +- docs/api/clipboard.md | 2 +- docs/api/content-tracing.md | 6 +-- docs/api/crash-reporter.md | 2 +- docs/api/desktop-capturer.md | 4 +- docs/api/dialog.md | 4 +- docs/api/download-item.md | 8 ++-- docs/api/file-object.md | 10 ++--- docs/api/frameless-window.md | 8 ++-- docs/api/global-shortcut.md | 9 ++-- docs/api/ipc-main.md | 10 ++--- docs/api/menu.md | 37 ++++++++-------- docs/api/native-image.md | 8 ++-- docs/api/power-monitor.md | 4 +- docs/api/power-save-blocker.md | 4 +- docs/api/process.md | 4 +- docs/api/protocol.md | 10 ++--- docs/api/remote.md | 21 ++++----- docs/api/screen.md | 21 ++++----- docs/api/session.md | 32 +++++++------- docs/api/shell.md | 2 +- docs/api/system-preferences.md | 2 +- docs/api/tray.md | 7 ++- docs/api/web-contents.md | 55 ++++++++++++------------ docs/api/web-frame.md | 2 +- docs/api/web-view-tag.md | 38 ++++++++-------- 28 files changed, 168 insertions(+), 176 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index 1f682933d23..382dd9dbc58 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -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'); }) @@ -386,12 +386,12 @@ default protocol handler. ### `app.isDefaultProtocolClient(protocol)` _OS X_ _Windows_ -* `protocol` String - The name of your protocol, without `://`. +* `protocol` String - The name of your protocol, without `://`. This method checks if the current executable is the default handler for a protocol -(aka URI scheme). If so, it will return true. Otherwise, it will return false. +(aka URI scheme). If so, it will return true. Otherwise, it will return false. -**Note:** On OS X, you can use this method to check if the app has been registered as the default protocol handler for a protocol. You can also verify this by checking `~/Library/Preferences/com.apple.LaunchServices.plist` on the OS X machine. +**Note:** On OS X, you can use this method to check if the app has been registered as the default protocol handler for a protocol. You can also verify this by checking `~/Library/Preferences/com.apple.LaunchServices.plist` on the OS X machine. Please refer to [Apple's documentation][LSCopyDefaultHandlerForURLScheme] for details. The API uses the Windows Registry and LSCopyDefaultHandlerForURLScheme internally. @@ -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', () => { }); ``` @@ -568,5 +568,5 @@ Sets the `image` associated with this dock icon. [tasks]:http://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#tasks [app-user-model-id]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd378459(v=vs.85).aspx [CFBundleURLTypes]: https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-102207-TPXREF115 -[LSCopyDefaultHandlerForURLScheme]: +[LSCopyDefaultHandlerForURLScheme]: https://developer.apple.com/library/mac/documentation/Carbon/Reference/LaunchServicesReference/#//apple_ref/c/func/LSCopyDefaultHandlerForURLScheme diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 7289057b40e..ea882205983 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -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(); diff --git a/docs/api/chrome-command-line-switches.md b/docs/api/chrome-command-line-switches.md index 17059435eaa..e51ffe5e2eb 100644 --- a/docs/api/chrome-command-line-switches.md +++ b/docs/api/chrome-command-line-switches.md @@ -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 }); ``` diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index 58e5a1c1b09..38bd8659422 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -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'); ``` diff --git a/docs/api/content-tracing.md b/docs/api/content-tracing.md index e347790b63f..e6f6b7a0d2d 100644 --- a/docs/api/content-tracing.md +++ b/docs/api/content-tracing.md @@ -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); diff --git a/docs/api/crash-reporter.md b/docs/api/crash-reporter.md index 1f8229eaa79..4e4b49df0f7 100644 --- a/docs/api/crash-reporter.md +++ b/docs/api/crash-reporter.md @@ -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', diff --git a/docs/api/desktop-capturer.md b/docs/api/desktop-capturer.md index dc523c3a42f..9c6d220112b 100644 --- a/docs/api/desktop-capturer.md +++ b/docs/api/desktop-capturer.md @@ -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") { diff --git a/docs/api/dialog.md b/docs/api/dialog.md index a5f61a53e3f..84aac7c63bf 100644 --- a/docs/api/dialog.md +++ b/docs/api/dialog.md @@ -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 diff --git a/docs/api/download-item.md b/docs/api/download-item.md index 4418f61c19f..6d15176aa09 100644 --- a/docs/api/download-item.md +++ b/docs/api/download-item.md @@ -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"); diff --git a/docs/api/file-object.md b/docs/api/file-object.md index 31c6feddb50..2484e960e90 100644 --- a/docs/api/file-object.md +++ b/docs/api/file-object.md @@ -15,16 +15,16 @@ Example on getting a real path from a dragged-onto-the-app file: @@ -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(); } }) diff --git a/docs/api/web-frame.md b/docs/api/web-frame.md index 4b7d04b6f0f..dcd595d3d97 100644 --- a/docs/api/web-frame.md +++ b/docs/api/web-frame.md @@ -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); ``` diff --git a/docs/api/web-view-tag.md b/docs/api/web-view-tag.md index 5f4a422a13a..a7410e146ae 100644 --- a/docs/api/web-view-tag.md +++ b/docs/api/web-view-tag.md @@ -31,18 +31,20 @@ and displays a "loading..." message during the load time: ```html ``` @@ -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'); }); ```