docs: ensure app.md
examples are runnable (#38416)
docs: ensure app.md examples are runnable
This commit is contained in:
parent
06cc9caeaa
commit
ec85be3fb0
1 changed files with 41 additions and 21 deletions
|
@ -7,7 +7,7 @@ Process: [Main](../glossary.md#main-process)
|
||||||
The following example shows how to quit the application when the last window is
|
The following example shows how to quit the application when the last window is
|
||||||
closed:
|
closed:
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
app.on('window-all-closed', () => {
|
app.on('window-all-closed', () => {
|
||||||
app.quit()
|
app.quit()
|
||||||
|
@ -296,7 +296,7 @@ Emitted when failed to verify the `certificate` for `url`, to trust the
|
||||||
certificate you should prevent the default behavior with
|
certificate you should prevent the default behavior with
|
||||||
`event.preventDefault()` and call `callback(true)`.
|
`event.preventDefault()` and call `callback(true)`.
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
|
|
||||||
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
|
app.on('certificate-error', (event, webContents, url, error, certificate, callback) => {
|
||||||
|
@ -328,7 +328,7 @@ and `callback` can be called with an entry filtered from the list. Using
|
||||||
`event.preventDefault()` prevents the application from using the first
|
`event.preventDefault()` prevents the application from using the first
|
||||||
certificate from the store.
|
certificate from the store.
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
|
|
||||||
app.on('select-client-certificate', (event, webContents, url, list, callback) => {
|
app.on('select-client-certificate', (event, webContents, url, list, callback) => {
|
||||||
|
@ -361,7 +361,7 @@ The default behavior is to cancel all authentications. To override this you
|
||||||
should prevent the default behavior with `event.preventDefault()` and call
|
should prevent the default behavior with `event.preventDefault()` and call
|
||||||
`callback(username, password)` with the credentials.
|
`callback(username, password)` with the credentials.
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
|
|
||||||
app.on('login', (event, webContents, details, authInfo, callback) => {
|
app.on('login', (event, webContents, details, authInfo, callback) => {
|
||||||
|
@ -481,7 +481,7 @@ Returns:
|
||||||
|
|
||||||
Emitted when Electron has created a new `session`.
|
Emitted when Electron has created a new `session`.
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
|
|
||||||
app.on('session-created', (session) => {
|
app.on('session-created', (session) => {
|
||||||
|
@ -566,7 +566,7 @@ started after current instance exited.
|
||||||
An example of restarting current instance immediately and adding a new command
|
An example of restarting current instance immediately and adding a new command
|
||||||
line argument to the new instance:
|
line argument to the new instance:
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
|
|
||||||
app.relaunch({ args: process.argv.slice(1).concat(['--relaunch']) })
|
app.relaunch({ args: process.argv.slice(1).concat(['--relaunch']) })
|
||||||
|
@ -951,7 +951,7 @@ List, nor will it be displayed.
|
||||||
|
|
||||||
Here's a very simple example of creating a custom Jump List:
|
Here's a very simple example of creating a custom Jump List:
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app } = require('electron')
|
||||||
|
|
||||||
app.setJumpList([
|
app.setJumpList([
|
||||||
|
@ -1034,8 +1034,8 @@ use this method to ensure single instance.
|
||||||
An example of activating the window of primary instance when a second instance
|
An example of activating the window of primary instance when a second instance
|
||||||
starts:
|
starts:
|
||||||
|
|
||||||
```javascript
|
```js
|
||||||
const { app } = require('electron')
|
const { app, BrowserWindow } = require('electron')
|
||||||
let myWindow = null
|
let myWindow = null
|
||||||
|
|
||||||
const additionalData = { myKey: 'myValue' }
|
const additionalData = { myKey: 'myValue' }
|
||||||
|
@ -1055,9 +1055,9 @@ if (!gotTheLock) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Create myWindow, load the rest of the app, etc...
|
|
||||||
app.whenReady().then(() => {
|
app.whenReady().then(() => {
|
||||||
myWindow = createWindow()
|
myWindow = new BrowserWindow({})
|
||||||
|
myWindow.loadURL('https://electronjs.org')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1180,12 +1180,16 @@ case the user's DNS configuration does not include a provider that supports
|
||||||
DoH.
|
DoH.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
const { app } = require('electron')
|
||||||
|
|
||||||
|
app.whenReady().then(() => {
|
||||||
app.configureHostResolver({
|
app.configureHostResolver({
|
||||||
secureDnsMode: 'secure',
|
secureDnsMode: 'secure',
|
||||||
secureDnsServers: [
|
secureDnsServers: [
|
||||||
'https://cloudflare-dns.com/dns-query'
|
'https://cloudflare-dns.com/dns-query'
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
This API must be called after the `ready` event is emitted.
|
This API must be called after the `ready` event is emitted.
|
||||||
|
@ -1336,7 +1340,10 @@ To work with Electron's `autoUpdater` on Windows, which uses [Squirrel][Squirrel
|
||||||
you'll want to set the launch path to Update.exe, and pass arguments that specify your
|
you'll want to set the launch path to Update.exe, and pass arguments that specify your
|
||||||
application name. For example:
|
application name. For example:
|
||||||
|
|
||||||
``` javascript
|
``` js
|
||||||
|
const { app } = require('electron')
|
||||||
|
const path = require('path')
|
||||||
|
|
||||||
const appFolder = path.dirname(process.execPath)
|
const appFolder = path.dirname(process.execPath)
|
||||||
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
|
const updateExe = path.resolve(appFolder, '..', 'Update.exe')
|
||||||
const exeName = path.basename(process.execPath)
|
const exeName = path.basename(process.execPath)
|
||||||
|
@ -1405,11 +1412,22 @@ Show the platform's native emoji picker.
|
||||||
Returns `Function` - This function **must** be called once you have finished accessing the security scoped file. If you do not remember to stop accessing the bookmark, [kernel resources will be leaked](https://developer.apple.com/reference/foundation/nsurl/1417051-startaccessingsecurityscopedreso?language=objc) and your app will lose its ability to reach outside the sandbox completely, until your app is restarted.
|
Returns `Function` - This function **must** be called once you have finished accessing the security scoped file. If you do not remember to stop accessing the bookmark, [kernel resources will be leaked](https://developer.apple.com/reference/foundation/nsurl/1417051-startaccessingsecurityscopedreso?language=objc) and your app will lose its ability to reach outside the sandbox completely, until your app is restarted.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
// Start accessing the file.
|
const { app, dialog } = require('electron')
|
||||||
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(data)
|
const fs = require('fs')
|
||||||
// You can now access the file outside of the sandbox 🎉
|
|
||||||
|
|
||||||
// Remember to stop accessing the file once you've finished with it.
|
let filepath
|
||||||
|
let bookmark
|
||||||
|
|
||||||
|
dialog.showOpenDialog(null, { securityScopedBookmarks: true }, (filepaths, bookmarks) => {
|
||||||
|
filepath = filepaths[0]
|
||||||
|
bookmark = bookmarks[0]
|
||||||
|
fs.readFileSync(filepath)
|
||||||
|
})
|
||||||
|
|
||||||
|
// ... restart app ...
|
||||||
|
|
||||||
|
const stopAccessingSecurityScopedResource = app.startAccessingSecurityScopedResource(bookmark)
|
||||||
|
fs.readFileSync(filepath)
|
||||||
stopAccessingSecurityScopedResource()
|
stopAccessingSecurityScopedResource()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1450,6 +1468,8 @@ By default, if an app of the same name as the one being moved exists in the Appl
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
const { app, dialog } = require('electron')
|
||||||
|
|
||||||
app.moveToApplicationsFolder({
|
app.moveToApplicationsFolder({
|
||||||
conflictHandler: (conflictType) => {
|
conflictHandler: (conflictType) => {
|
||||||
if (conflictType === 'exists') {
|
if (conflictType === 'exists') {
|
||||||
|
|
Loading…
Reference in a new issue