From 3d2a7545314f68c21f3fb92fb0a6eb9edcceabd3 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 20 Nov 2023 23:50:08 -0800 Subject: [PATCH] chore: extend linting of code blocks in the docs (#40245) * chore: extend linting of code blocks in the docs * chore: combine lint:markdownlint and lint:markdown scripts --- docs/api/accelerator.md | 2 +- docs/api/browser-view.md | 2 +- docs/api/browser-window.md | 24 ++-- docs/api/client-request.md | 6 +- docs/api/clipboard.md | 2 +- docs/api/command-line-switches.md | 4 +- docs/api/command-line.md | 2 +- docs/api/content-tracing.md | 2 +- docs/api/context-bridge.md | 12 +- docs/api/cookies.md | 2 +- docs/api/crash-reporter.md | 2 +- docs/api/debugger.md | 2 +- docs/api/desktop-capturer.md | 6 +- docs/api/dialog.md | 6 +- docs/api/dock.md | 2 +- docs/api/download-item.md | 2 +- docs/api/environment-variables.md | 2 +- docs/api/global-shortcut.md | 2 +- docs/api/incoming-message.md | 2 +- docs/api/ipc-renderer.md | 2 +- docs/api/menu.md | 8 +- docs/api/native-image.md | 8 +- docs/api/net-log.md | 2 +- docs/api/net.md | 2 +- docs/api/power-save-blocker.md | 2 +- docs/api/protocol.md | 12 +- docs/api/push-notifications.md | 2 +- docs/api/screen.md | 23 ++-- docs/api/service-workers.md | 2 +- docs/api/session.md | 36 +++--- docs/api/shell.md | 2 +- docs/api/structures/printer-info.md | 2 +- docs/api/system-preferences.md | 6 +- docs/api/touch-bar.md | 2 +- docs/api/tray.md | 4 +- docs/api/web-contents.md | 24 ++-- docs/api/web-frame-main.md | 4 +- docs/api/web-frame.md | 8 +- docs/api/web-request.md | 2 +- docs/api/webview-tag.md | 12 +- docs/api/window-open.md | 4 +- docs/breaking-changes.md | 8 +- docs/development/creating-api.md | 2 +- docs/faq.md | 8 +- docs/tutorial/application-debugging.md | 2 +- docs/tutorial/asar-archives.md | 12 +- docs/tutorial/context-isolation.md | 18 +-- docs/tutorial/dark-mode.md | 2 +- docs/tutorial/devices.md | 10 +- docs/tutorial/devtools-extension.md | 2 +- docs/tutorial/examples.md | 9 -- docs/tutorial/in-app-purchases.md | 2 +- docs/tutorial/installation.md | 2 +- docs/tutorial/ipc.md | 36 +++--- docs/tutorial/keyboard-shortcuts.md | 66 +++++++++-- .../launch-app-from-url-in-another-app.md | 14 +-- docs/tutorial/macos-dock.md | 6 +- docs/tutorial/multithreading.md | 4 +- docs/tutorial/native-file-drag-drop.md | 50 ++++++++- docs/tutorial/notifications.md | 45 ++++++-- docs/tutorial/offscreen-rendering.md | 34 +++++- docs/tutorial/progress-bar.md | 13 ++- docs/tutorial/recent-documents.md | 8 +- docs/tutorial/represented-file.md | 28 ++--- docs/tutorial/security.md | 2 +- docs/tutorial/updates.md | 10 +- docs/tutorial/window-customization.md | 24 ++-- docs/tutorial/windows-taskbar.md | 12 +- package.json | 4 +- script/lint.js | 104 +++++++++++++++++- yarn.lock | 6 +- 71 files changed, 510 insertions(+), 286 deletions(-) diff --git a/docs/api/accelerator.md b/docs/api/accelerator.md index e1fac744ad8b..7a019918a41b 100644 --- a/docs/api/accelerator.md +++ b/docs/api/accelerator.md @@ -15,7 +15,7 @@ Shortcuts are registered with the [`globalShortcut`](global-shortcut.md) module using the [`register`](global-shortcut.md#globalshortcutregisteraccelerator-callback) method, i.e. -```javascript +```js const { app, globalShortcut } = require('electron') app.whenReady().then(() => { diff --git a/docs/api/browser-view.md b/docs/api/browser-view.md index 4db9ebc300e4..f117aec4670a 100644 --- a/docs/api/browser-view.md +++ b/docs/api/browser-view.md @@ -16,7 +16,7 @@ module is emitted. ### Example -```javascript +```js // In the main process. const { app, BrowserView, BrowserWindow } = require('electron') diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 283e9581499d..3af05f03ad3c 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -7,7 +7,7 @@ Process: [Main](../glossary.md#main-process) This module cannot be used until the `ready` event of the `app` module is emitted. -```javascript +```js // In the main process. const { BrowserWindow } = require('electron') @@ -38,7 +38,7 @@ While loading the page, the `ready-to-show` event will be emitted when the rende process has rendered the page for the first time if the window has not been shown yet. Showing the window after this event will have no visual flash: -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow({ show: false }) win.once('ready-to-show', () => { @@ -59,7 +59,7 @@ For a complex app, the `ready-to-show` event could be emitted too late, making the app feel slow. In this case, it is recommended to show the window immediately, and use a `backgroundColor` close to your app's background: -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow({ backgroundColor: '#2e2c29' }) @@ -85,7 +85,7 @@ For more information about these color types see valid options in [win.setBackgr By using `parent` option, you can create child windows: -```javascript +```js const { BrowserWindow } = require('electron') const top = new BrowserWindow() @@ -101,7 +101,7 @@ The `child` window will always show on top of the `top` window. A modal window is a child window that disables parent window, to create a modal window, you have to set both `parent` and `modal` options: -```javascript +```js const { BrowserWindow } = require('electron') const top = new BrowserWindow() @@ -188,7 +188,7 @@ window should be closed, which will also be called when the window is reloaded. In Electron, returning any value other than `undefined` would cancel the close. For example: -```javascript +```js window.onbeforeunload = (e) => { console.log('I do not want to be closed') @@ -351,7 +351,7 @@ Commands are lowercased, underscores are replaced with hyphens, and the `APPCOMMAND_` prefix is stripped off. e.g. `APPCOMMAND_BROWSER_BACKWARD` is emitted as `browser-backward`. -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() win.on('app-command', (e, cmd) => { @@ -456,7 +456,7 @@ Returns `BrowserWindow | null` - The window with the given `id`. Objects created with `new BrowserWindow` have the following properties: -```javascript +```js const { BrowserWindow } = require('electron') // In this example `win` is our instance const win = new BrowserWindow({ width: 800, height: 600 }) @@ -780,7 +780,7 @@ Closes the currently open [Quick Look][quick-look] panel. Resizes and moves the window to the supplied bounds. Any properties that are not supplied will default to their current values. -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() @@ -1035,7 +1035,7 @@ Changes the attachment point for sheets on macOS. By default, sheets are attached just below the window frame, but you may want to display them beneath a HTML-rendered toolbar. For example: -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() @@ -1178,7 +1178,7 @@ To ensure that file URLs are properly formatted, it is recommended to use Node's [`url.format`](https://nodejs.org/api/url.html#url_url_format_urlobject) method: -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() @@ -1194,7 +1194,7 @@ win.loadURL(url) You can load a URL using a `POST` request with URL-encoded data by doing the following: -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() diff --git a/docs/api/client-request.md b/docs/api/client-request.md index 3bbbe6ced404..300fafa3edf0 100644 --- a/docs/api/client-request.md +++ b/docs/api/client-request.md @@ -65,7 +65,7 @@ strictly follow the Node.js model as described in the For instance, we could have created the same request to 'github.com' as follows: -```javascript +```js const request = net.request({ method: 'GET', protocol: 'https:', @@ -104,7 +104,7 @@ The `callback` function is expected to be called back with user credentials: * `username` string * `password` string -```javascript @ts-type={request:Electron.ClientRequest} +```js @ts-type={request:Electron.ClientRequest} request.on('login', (authInfo, callback) => { callback('username', 'password') }) @@ -113,7 +113,7 @@ request.on('login', (authInfo, callback) => { Providing empty credentials will cancel the request and report an authentication error on the response object: -```javascript @ts-type={request:Electron.ClientRequest} +```js @ts-type={request:Electron.ClientRequest} request.on('response', (response) => { console.log(`STATUS: ${response.statusCode}`) response.on('error', (error) => { diff --git a/docs/api/clipboard.md b/docs/api/clipboard.md index ea5345f0719f..c4328e287172 100644 --- a/docs/api/clipboard.md +++ b/docs/api/clipboard.md @@ -7,7 +7,7 @@ Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer On Linux, there is also a `selection` clipboard. To manipulate it you need to pass `selection` to each method: -```javascript +```js const { clipboard } = require('electron') clipboard.writeText('Example string', 'selection') diff --git a/docs/api/command-line-switches.md b/docs/api/command-line-switches.md index 8237cf53f20f..d497028d22e2 100644 --- a/docs/api/command-line-switches.md +++ b/docs/api/command-line-switches.md @@ -6,7 +6,7 @@ You can use [app.commandLine.appendSwitch][append-switch] to append them in your app's main script before the [ready][ready] event of the [app][app] module is emitted: -```javascript +```js const { app } = require('electron') app.commandLine.appendSwitch('remote-debugging-port', '8315') app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1') @@ -185,7 +185,7 @@ list of hosts. This flag has an effect only if used in tandem with For example: -```javascript +```js const { app } = require('electron') app.commandLine.appendSwitch('proxy-bypass-list', ';*.google.com;*foo.com;1.2.3.4:5678') ``` diff --git a/docs/api/command-line.md b/docs/api/command-line.md index 9d36a03161b3..63046d734624 100644 --- a/docs/api/command-line.md +++ b/docs/api/command-line.md @@ -7,7 +7,7 @@ _This class is not exported from the `'electron'` module. It is only available a The following example shows how to check if the `--disable-gpu` flag is set. -```javascript +```js const { app } = require('electron') app.commandLine.hasSwitch('disable-gpu') ``` diff --git a/docs/api/content-tracing.md b/docs/api/content-tracing.md index ec8e3f797970..435cfe343c94 100644 --- a/docs/api/content-tracing.md +++ b/docs/api/content-tracing.md @@ -10,7 +10,7 @@ This module does not include a web interface. To view recorded traces, use **Note:** You should not use this module until the `ready` event of the app module is emitted. -```javascript +```js const { app, contentTracing } = require('electron') app.whenReady().then(() => { diff --git a/docs/api/context-bridge.md b/docs/api/context-bridge.md index 1b5bc021e9a9..5002874994e3 100644 --- a/docs/api/context-bridge.md +++ b/docs/api/context-bridge.md @@ -6,7 +6,7 @@ Process: [Renderer](../glossary.md#renderer-process) An example of exposing an API to a renderer from an isolated preload script is given below: -```javascript +```js // Preload (Isolated World) const { contextBridge, ipcRenderer } = require('electron') @@ -18,7 +18,7 @@ contextBridge.exposeInMainWorld( ) ``` -```javascript @ts-nocheck +```js @ts-nocheck // Renderer (Main World) window.electron.doThing() @@ -64,7 +64,7 @@ the API become immutable and updates on either side of the bridge do not result An example of a complex API is shown below: -```javascript +```js const { contextBridge, ipcRenderer } = require('electron') contextBridge.exposeInMainWorld( @@ -92,7 +92,7 @@ contextBridge.exposeInMainWorld( An example of `exposeInIsolatedWorld` is shown below: -```javascript +```js const { contextBridge, ipcRenderer } = require('electron') contextBridge.exposeInIsolatedWorld( @@ -104,7 +104,7 @@ contextBridge.exposeInIsolatedWorld( ) ``` -```javascript @ts-nocheck +```js @ts-nocheck // Renderer (In isolated world id1004) window.electron.doThing() @@ -145,7 +145,7 @@ The table of supported types described above also applies to Node APIs that you Please note that many Node APIs grant access to local system resources. Be very cautious about which globals and APIs you expose to untrusted remote content. -```javascript +```js const { contextBridge } = require('electron') const crypto = require('node:crypto') contextBridge.exposeInMainWorld('nodeCrypto', { diff --git a/docs/api/cookies.md b/docs/api/cookies.md index 5665c8587c36..8a3576d3fc60 100644 --- a/docs/api/cookies.md +++ b/docs/api/cookies.md @@ -10,7 +10,7 @@ a `Session`. For example: -```javascript +```js const { session } = require('electron') // Query all cookies. diff --git a/docs/api/crash-reporter.md b/docs/api/crash-reporter.md index 8b8b2e2b2ff0..8883699db7a1 100644 --- a/docs/api/crash-reporter.md +++ b/docs/api/crash-reporter.md @@ -7,7 +7,7 @@ Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer The following is an example of setting up Electron to automatically submit crash reports to a remote server: -```javascript +```js const { crashReporter } = require('electron') crashReporter.start({ submitURL: 'https://your-domain.com/url-to-submit' }) diff --git a/docs/api/debugger.md b/docs/api/debugger.md index fd0abe096dd6..26a4b7c6a9f9 100644 --- a/docs/api/debugger.md +++ b/docs/api/debugger.md @@ -8,7 +8,7 @@ _This class is not exported from the `'electron'` module. It is only available a Chrome Developer Tools has a [special binding][rdp] available at JavaScript runtime that allows interacting with pages and instrumenting them. -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() diff --git a/docs/api/desktop-capturer.md b/docs/api/desktop-capturer.md index 078d4f980b44..5eba3efaa157 100644 --- a/docs/api/desktop-capturer.md +++ b/docs/api/desktop-capturer.md @@ -8,7 +8,7 @@ Process: [Main](../glossary.md#main-process) The following example shows how to capture video from a desktop window whose title is `Electron`: -```javascript +```js // In the main process. const { BrowserWindow, desktopCapturer } = require('electron') @@ -24,7 +24,7 @@ desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources = }) ``` -```javascript @ts-nocheck +```js @ts-nocheck // In the preload script. const { ipcRenderer } = require('electron') @@ -68,7 +68,7 @@ To capture both audio and video from the entire desktop the constraints passed to [`navigator.mediaDevices.getUserMedia`][] must include `chromeMediaSource: 'desktop'`, for both `audio` and `video`, but should not include a `chromeMediaSourceId` constraint. -```javascript +```js const constraints = { audio: { mandatory: { diff --git a/docs/api/dialog.md b/docs/api/dialog.md index 7d1879c340db..414ed39b64d0 100644 --- a/docs/api/dialog.md +++ b/docs/api/dialog.md @@ -6,7 +6,7 @@ Process: [Main](../glossary.md#main-process) An example of showing a dialog to select multiple files: -```javascript +```js const { dialog } = require('electron') console.log(dialog.showOpenDialog({ properties: ['openFile', 'multiSelections'] })) ``` @@ -52,7 +52,7 @@ The `browserWindow` argument allows the dialog to attach itself to a parent wind The `filters` specifies an array of file types that can be displayed or selected when you want to limit the user to a specific type. For example: -```javascript +```js { filters: [ { name: 'Images', extensions: ['jpg', 'png', 'gif'] }, @@ -119,7 +119,7 @@ The `browserWindow` argument allows the dialog to attach itself to a parent wind The `filters` specifies an array of file types that can be displayed or selected when you want to limit the user to a specific type. For example: -```javascript +```js { filters: [ { name: 'Images', extensions: ['jpg', 'png', 'gif'] }, diff --git a/docs/api/dock.md b/docs/api/dock.md index d93f72afe438..efe77aa536c2 100644 --- a/docs/api/dock.md +++ b/docs/api/dock.md @@ -7,7 +7,7 @@ _This class is not exported from the `'electron'` module. It is only available a The following example shows how to bounce your icon on the dock. -```javascript +```js const { app } = require('electron') app.dock.bounce() ``` diff --git a/docs/api/download-item.md b/docs/api/download-item.md index 0a362d522f91..d07293a8887d 100644 --- a/docs/api/download-item.md +++ b/docs/api/download-item.md @@ -9,7 +9,7 @@ _This class is not exported from the `'electron'` module. It is only available a It is used in `will-download` event of `Session` class, and allows users to control the download item. -```javascript +```js // In the main process. const { BrowserWindow } = require('electron') const win = new BrowserWindow() diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index fa6d1e13fcc4..0150fa5e282c 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -59,7 +59,7 @@ geolocation webservice. To enable this feature, acquire a and place the following code in your main process file, before opening any browser windows that will make geolocation requests: -```javascript +```js process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE' ``` diff --git a/docs/api/global-shortcut.md b/docs/api/global-shortcut.md index 66d9c6943a84..9fe98c0415f0 100644 --- a/docs/api/global-shortcut.md +++ b/docs/api/global-shortcut.md @@ -12,7 +12,7 @@ shortcuts. not have the keyboard focus. This module cannot be used before the `ready` event of the app module is emitted. -```javascript +```js const { app, globalShortcut } = require('electron') app.whenReady().then(() => { diff --git a/docs/api/incoming-message.md b/docs/api/incoming-message.md index 904b4bfe123b..02243fb7e919 100644 --- a/docs/api/incoming-message.md +++ b/docs/api/incoming-message.md @@ -89,7 +89,7 @@ tuples. So, the even-numbered offsets are key values, and the odd-numbered offsets are the associated values. Header names are not lowercased, and duplicates are not merged. -```javascript @ts-type={response:Electron.IncomingMessage} +```js @ts-type={response:Electron.IncomingMessage} // Prints something like: // // [ 'user-agent', diff --git a/docs/api/ipc-renderer.md b/docs/api/ipc-renderer.md index cf9b219c8d3b..6dc62b323d2a 100644 --- a/docs/api/ipc-renderer.md +++ b/docs/api/ipc-renderer.md @@ -120,7 +120,7 @@ The main process should listen for `channel` with For example: -```javascript @ts-type={someArgument:unknown} @ts-type={doSomeWork:(arg:unknown)=>Promise} +```js @ts-type={someArgument:unknown} @ts-type={doSomeWork:(arg:unknown)=>Promise} // Renderer process ipcRenderer.invoke('some-name', someArgument).then((result) => { // ... diff --git a/docs/api/menu.md b/docs/api/menu.md index ad253bf95dc1..860eb7e8d6fb 100644 --- a/docs/api/menu.md +++ b/docs/api/menu.md @@ -151,7 +151,7 @@ can have a submenu. An example of creating the application menu with the simple template API: -```javascript @ts-expect-error=[107] +```js @ts-expect-error=[107] const { app, Menu } = require('electron') const isMac = process.platform === 'darwin' @@ -353,7 +353,7 @@ By default, items will be inserted in the order they exist in the template unles Template: -```javascript +```js [ { id: '1', label: 'one' }, { id: '2', label: 'two' }, @@ -373,7 +373,7 @@ Menu: Template: -```javascript +```js [ { id: '1', label: 'one' }, { type: 'separator' }, @@ -397,7 +397,7 @@ Menu: Template: -```javascript +```js [ { id: '1', label: 'one', after: ['3'] }, { id: '2', label: 'two', before: ['1'] }, diff --git a/docs/api/native-image.md b/docs/api/native-image.md index d31b39f1c778..6563b67653a5 100644 --- a/docs/api/native-image.md +++ b/docs/api/native-image.md @@ -10,7 +10,7 @@ In Electron, for the APIs that take images, you can pass either file paths or For example, when creating a tray or setting a window's icon, you can pass an image file path as a `string`: -```javascript +```js const { BrowserWindow, Tray } = require('electron') const appIcon = new Tray('/Users/somebody/images/icon.png') @@ -20,7 +20,7 @@ console.log(appIcon, win) Or read the image from the clipboard, which returns a `NativeImage`: -```javascript +```js const { clipboard, Tray } = require('electron') const image = clipboard.readImage() const appIcon = new Tray(image) @@ -71,7 +71,7 @@ images/ └── icon@3x.png ``` -```javascript +```js const { Tray } = require('electron') const appIcon = new Tray('/Users/somebody/images/icon.png') console.log(appIcon) @@ -138,7 +138,7 @@ Creates a new `NativeImage` instance from a file located at `path`. This method returns an empty image if the `path` does not exist, cannot be read, or is not a valid image. -```javascript +```js const nativeImage = require('electron').nativeImage const image = nativeImage.createFromPath('/Users/somebody/images/icon.png') diff --git a/docs/api/net-log.md b/docs/api/net-log.md index db16c11193f8..f9b04212c043 100644 --- a/docs/api/net-log.md +++ b/docs/api/net-log.md @@ -4,7 +4,7 @@ Process: [Main](../glossary.md#main-process) -```javascript +```js const { app, netLog } = require('electron') app.whenReady().then(async () => { diff --git a/docs/api/net.md b/docs/api/net.md index 858ba19ea4b4..d67012614658 100644 --- a/docs/api/net.md +++ b/docs/api/net.md @@ -26,7 +26,7 @@ Node.js. Example usage: -```javascript +```js const { app } = require('electron') app.whenReady().then(() => { const { net } = require('electron') diff --git a/docs/api/power-save-blocker.md b/docs/api/power-save-blocker.md index 1ba95f0aab07..ee57287258df 100644 --- a/docs/api/power-save-blocker.md +++ b/docs/api/power-save-blocker.md @@ -6,7 +6,7 @@ Process: [Main](../glossary.md#main-process) For example: -```javascript +```js const { powerSaveBlocker } = require('electron') const id = powerSaveBlocker.start('prevent-display-sleep') diff --git a/docs/api/protocol.md b/docs/api/protocol.md index 93dc050287bd..9b093fc89078 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -7,7 +7,7 @@ Process: [Main](../glossary.md#main-process) An example of implementing a protocol that has the same effect as the `file://` protocol: -```javascript +```js const { app, protocol, net } = require('electron') app.whenReady().then(() => { @@ -31,7 +31,7 @@ a different session and your custom protocol will not work if you just use To have your custom protocol work in combination with a custom session, you need to register it to that session explicitly. -```javascript +```js const { app, BrowserWindow, net, protocol, session } = require('electron') const path = require('node:path') const url = require('url') @@ -67,7 +67,7 @@ video/audio. Specify a privilege with the value of `true` to enable the capabili An example of registering a privileged scheme, that bypasses Content Security Policy: -```javascript +```js const { protocol } = require('electron') protocol.registerSchemesAsPrivileged([ { scheme: 'foo', privileges: { bypassCSP: true } } @@ -222,7 +222,7 @@ property. Example: -```javascript +```js protocol.registerBufferProtocol('atom', (request, callback) => { callback({ mimeType: 'text/html', data: Buffer.from('
Response
') }) }) @@ -277,7 +277,7 @@ has the `data` property. Example: -```javascript +```js const { protocol } = require('electron') const { PassThrough } = require('stream') @@ -302,7 +302,7 @@ protocol.registerStreamProtocol('atom', (request, callback) => { It is possible to pass any object that implements the readable stream API (emits `data`/`end`/`error` events). For example, here's how a file could be returned: -```javascript +```js protocol.registerStreamProtocol('atom', (request, callback) => { callback(fs.createReadStream('index.html')) }) diff --git a/docs/api/push-notifications.md b/docs/api/push-notifications.md index 1cf8d88eb021..4a9d19f09ec7 100644 --- a/docs/api/push-notifications.md +++ b/docs/api/push-notifications.md @@ -6,7 +6,7 @@ Process: [Main](../glossary.md#main-process) For example, when registering for push notifications via Apple push notification services (APNS): -```javascript +```js const { pushNotifications, Notification } = require('electron') pushNotifications.registerForAPNSNotifications().then((token) => { diff --git a/docs/api/screen.md b/docs/api/screen.md index a576a5902c52..4f68a2018b06 100644 --- a/docs/api/screen.md +++ b/docs/api/screen.md @@ -14,20 +14,29 @@ property, so writing `let { screen } = require('electron')` will not work. An example of creating a window that fills the whole screen: -```javascript fiddle='docs/fiddles/screen/fit-screen' -const { app, BrowserWindow, screen } = require('electron') +```fiddle docs/fiddles/screen/fit-screen +// Retrieve information about screen size, displays, cursor position, etc. +// +// For more info, see: +// https://www.electronjs.org/docs/latest/api/screen + +const { app, BrowserWindow, screen } = require('electron/main') + +let mainWindow = null -let win app.whenReady().then(() => { - const { width, height } = screen.getPrimaryDisplay().workAreaSize - win = new BrowserWindow({ width, height }) - win.loadURL('https://github.com') + // Create a window that fills the screen's available work area. + const primaryDisplay = screen.getPrimaryDisplay() + const { width, height } = primaryDisplay.workAreaSize + + mainWindow = new BrowserWindow({ width, height }) + mainWindow.loadURL('https://electronjs.org') }) ``` Another example of creating a window in the external display: -```javascript +```js const { app, BrowserWindow, screen } = require('electron') let win diff --git a/docs/api/service-workers.md b/docs/api/service-workers.md index ec9f33d3e18a..f1fa64f62560 100644 --- a/docs/api/service-workers.md +++ b/docs/api/service-workers.md @@ -10,7 +10,7 @@ a `Session`. For example: -```javascript +```js const { session } = require('electron') // Get all service workers. diff --git a/docs/api/session.md b/docs/api/session.md index 01b330a87511..f8f4367d6e0b 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -9,7 +9,7 @@ The `session` module can be used to create new `Session` objects. You can also access the `session` of existing pages by using the `session` property of [`WebContents`](web-contents.md), or from the `session` module. -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow({ width: 800, height: 600 }) @@ -75,7 +75,7 @@ _This class is not exported from the `'electron'` module. It is only available a You can create a `Session` object in the `session` module: -```javascript +```js const { session } = require('electron') const ses = session.fromPartition('persist:name') console.log(ses.getUserAgent()) @@ -98,7 +98,7 @@ Emitted when Electron is about to download `item` in `webContents`. Calling `event.preventDefault()` will cancel the download and `item` will not be available from next tick of the process. -```javascript @ts-expect-error=[4] +```js @ts-expect-error=[4] const { session } = require('electron') session.defaultSession.on('will-download', (event, item, webContents) => { event.preventDefault() @@ -214,7 +214,7 @@ cancel the request. Additionally, permissioning on `navigator.hid` can be further managed by using [`ses.setPermissionCheckHandler(handler)`](#sessetpermissioncheckhandlerhandler) and [`ses.setDevicePermissionHandler(handler)`](#sessetdevicepermissionhandlerhandler). -```javascript @ts-type={fetchGrantedDevices:()=>(Array)} +```js @ts-type={fetchGrantedDevices:()=>(Array)} const { app, BrowserWindow } = require('electron') let win = null @@ -320,7 +320,7 @@ cancel the request. Additionally, permissioning on `navigator.serial` can be managed by using [ses.setPermissionCheckHandler(handler)](#sessetpermissioncheckhandlerhandler) with the `serial` permission. -```javascript @ts-type={fetchGrantedDevices:()=>(Array)} +```js @ts-type={fetchGrantedDevices:()=>(Array)} const { app, BrowserWindow } = require('electron') let win = null @@ -463,7 +463,7 @@ cancel the request. Additionally, permissioning on `navigator.usb` can be further managed by using [`ses.setPermissionCheckHandler(handler)`](#sessetpermissioncheckhandlerhandler) and [`ses.setDevicePermissionHandler(handler)`](#sessetdevicepermissionhandlerhandler). -```javascript @ts-type={fetchGrantedDevices:()=>(Array)} @ts-type={updateGrantedDevices:(devices:Array)=>void} +```js @ts-type={fetchGrantedDevices:()=>(Array)} @ts-type={updateGrantedDevices:(devices:Array)=>void} const { app, BrowserWindow } = require('electron') let win = null @@ -754,7 +754,7 @@ Sets download saving directory. By default, the download directory will be the Emulates network with the given configuration for the `session`. -```javascript +```js const win = new BrowserWindow() // To emulate a GPRS connection with 50kbps throughput and 500 ms latency. @@ -868,7 +868,7 @@ calling `callback(-2)` rejects it. Calling `setCertificateVerifyProc(null)` will revert back to default certificate verify proc. -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow() @@ -921,7 +921,7 @@ To clear the handler, call `setPermissionRequestHandler(null)`. Please note tha you must also implement `setPermissionCheckHandler` to get complete permission handling. Most web APIs do a permission check and then make a permission request if the check is denied. -```javascript +```js const { session } = require('electron') session.fromPartition('some-partition').setPermissionRequestHandler((webContents, permission, callback) => { if (webContents.getURL() === 'some-host' && permission === 'notifications') { @@ -967,7 +967,7 @@ you must also implement `setPermissionRequestHandler` to get complete permission Most web APIs do a permission check and then make a permission request if the check is denied. To clear the handler, call `setPermissionCheckHandler(null)`. -```javascript +```js const { session } = require('electron') const url = require('url') session.fromPartition('some-partition').setPermissionCheckHandler((webContents, permission, requestingOrigin) => { @@ -1012,7 +1012,7 @@ via the `navigator.mediaDevices.getDisplayMedia` API. Use the [desktopCapturer](desktop-capturer.md) API to choose which stream(s) to grant access to. -```javascript +```js const { session, desktopCapturer } = require('electron') session.defaultSession.setDisplayMediaRequestHandler((request, callback) => { @@ -1026,7 +1026,7 @@ session.defaultSession.setDisplayMediaRequestHandler((request, callback) => { Passing a [WebFrameMain](web-frame-main.md) object as a video or audio stream will capture the video or audio stream from that frame. -```javascript +```js const { session } = require('electron') session.defaultSession.setDisplayMediaRequestHandler((request, callback) => { @@ -1055,7 +1055,7 @@ Additionally, the default behavior of Electron is to store granted device permis If longer term storage is needed, a developer can store granted device permissions (eg when handling the `select-hid-device` event) and then read from that storage with `setDevicePermissionHandler`. -```javascript @ts-type={fetchGrantedDevices:()=>(Array)} +```js @ts-type={fetchGrantedDevices:()=>(Array)} const { app, BrowserWindow } = require('electron') let win = null @@ -1137,7 +1137,7 @@ The return value for the handler is a string array of USB classes which should b Returning an empty string array from the handler will allow all USB classes; returning the passed in array will maintain the default list of protected USB classes (this is also the default behavior if a handler is not defined). To clear the handler, call `setUSBProtectedClassesHandler(null)`. -```javascript +```js const { app, BrowserWindow } = require('electron') let win = null @@ -1192,7 +1192,7 @@ that requires additional validation will be automatically cancelled. macOS does not require a handler because macOS handles the pairing automatically. To clear the handler, call `setBluetoothPairingHandler(null)`. -```javascript +```js const { app, BrowserWindow, session } = require('electron') const path = require('node:path') @@ -1238,7 +1238,7 @@ Clears the host resolver cache. Dynamically sets whether to always send credentials for HTTP NTLM or Negotiate authentication. -```javascript +```js const { session } = require('electron') // consider any url ending with `example.com`, `foobar.com`, `baz` // for integrated authentication. @@ -1543,7 +1543,7 @@ A [`WebRequest`](web-request.md) object for this session. A [`Protocol`](protocol.md) object for this session. -```javascript +```js const { app, session } = require('electron') const path = require('node:path') @@ -1562,7 +1562,7 @@ app.whenReady().then(() => { A [`NetLog`](net-log.md) object for this session. -```javascript +```js const { app, session } = require('electron') app.whenReady().then(async () => { diff --git a/docs/api/shell.md b/docs/api/shell.md index f278c4bbddaf..afbb966cc002 100644 --- a/docs/api/shell.md +++ b/docs/api/shell.md @@ -8,7 +8,7 @@ The `shell` module provides functions related to desktop integration. An example of opening a URL in the user's default browser: -```javascript +```js const { shell } = require('electron') shell.openExternal('https://github.com') diff --git a/docs/api/structures/printer-info.md b/docs/api/structures/printer-info.md index 61b637c7cef9..5910dca8fcfb 100644 --- a/docs/api/structures/printer-info.md +++ b/docs/api/structures/printer-info.md @@ -14,7 +14,7 @@ The number represented by `status` means different things on different platforms Below is an example of some of the additional options that may be set which may be different on each platform. -```javascript +```js { name: 'Austin_4th_Floor_Printer___C02XK13BJHD4', displayName: 'Austin 4th Floor Printer @ C02XK13BJHD4', diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 1fea0e3fbc0b..7d61d79cb0e5 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -4,7 +4,7 @@ Process: [Main](../glossary.md#main-process) -```javascript +```js const { systemPreferences } = require('electron') console.log(systemPreferences.isAeroGlassEnabled()) ``` @@ -189,7 +189,7 @@ enabled, and `false` otherwise. An example of using it to determine if you should create a transparent window or not (transparent windows won't work correctly when DWM composition is disabled): -```javascript +```js const { BrowserWindow, systemPreferences } = require('electron') const browserOptions = { width: 1000, height: 800 } @@ -348,7 +348,7 @@ Returns `boolean` - whether or not this device has the ability to use Touch ID. Returns `Promise` - resolves if the user has successfully authenticated with Touch ID. -```javascript +```js const { systemPreferences } = require('electron') systemPreferences.promptTouchID('To get consent for a Security-Gated Thing').then(success => { diff --git a/docs/api/touch-bar.md b/docs/api/touch-bar.md index 7cbcc83ddd23..c22943032643 100644 --- a/docs/api/touch-bar.md +++ b/docs/api/touch-bar.md @@ -79,7 +79,7 @@ immediately updates the escape item in the touch bar. Below is an example of a simple slot machine touch bar game with a button and some labels. -```javascript +```js const { app, BrowserWindow, TouchBar } = require('electron') const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar diff --git a/docs/api/tray.md b/docs/api/tray.md index 84e1b6bd30f0..285065b2ae12 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -8,7 +8,7 @@ Process: [Main](../glossary.md#main-process) `Tray` is an [EventEmitter][event-emitter]. -```javascript +```js const { app, Menu, Tray } = require('electron') let tray = null @@ -39,7 +39,7 @@ app.whenReady().then(() => { * In order for changes made to individual `MenuItem`s to take effect, you have to call `setContextMenu` again. For example: -```javascript +```js const { app, Menu, Tray } = require('electron') let appIcon = null diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 07965252b85f..9896d7a1aac8 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -9,7 +9,7 @@ It is responsible for rendering and controlling a web page and is a property of the [`BrowserWindow`](browser-window.md) object. An example of accessing the `webContents` object: -```javascript +```js const { BrowserWindow } = require('electron') const win = new BrowserWindow({ width: 800, height: 1500 }) @@ -53,7 +53,7 @@ If you want to also observe navigations in `