chore: type check JS in docs (#38423)
* build(deps): update @electron/lint-roller * chore: type check JS in docs * docs: add @ts-check and @ts-expect-error to code blocks * chore: fix type check errors in docs * chore: add ts-type to blocks
This commit is contained in:
parent
4c89061e0e
commit
905aad9cb6
49 changed files with 257 additions and 182 deletions
|
@ -55,7 +55,7 @@ fs.readdirSync('/path/to/example.asar')
|
|||
|
||||
Use a module from the archive:
|
||||
|
||||
```javascript
|
||||
```javascript @ts-nocheck
|
||||
require('./path/to/example.asar/dir/module.js')
|
||||
```
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ Valid `algorithm` values are currently `SHA256` only. The `hash` is a hash of t
|
|||
|
||||
ASAR integrity checking is currently disabled by default and can be enabled by toggling a fuse. See [Electron Fuses](fuses.md) for more information on what Electron Fuses are and how they work. When enabling this fuse you typically also want to enable the `onlyLoadAppFromAsar` fuse otherwise the validity checking can be bypassed via the Electron app code search path.
|
||||
|
||||
```js
|
||||
```js @ts-nocheck
|
||||
require('@electron/fuses').flipFuses(
|
||||
// E.g. /a/b/Foo.app
|
||||
pathToPackagedApp,
|
||||
|
|
|
@ -90,7 +90,7 @@ Usage of `selenium-webdriver` with Electron is the same as with
|
|||
normal websites, except that you have to manually specify how to connect
|
||||
ChromeDriver and where to find the binary of your Electron app:
|
||||
|
||||
```js title='test.js'
|
||||
```js title='test.js' @ts-expect-error=[1]
|
||||
const webdriver = require('selenium-webdriver')
|
||||
const driver = new webdriver.Builder()
|
||||
// The "9515" is the port opened by ChromeDriver.
|
||||
|
@ -155,7 +155,7 @@ Playwright launches your app in development mode through the `_electron.launch`
|
|||
To point this API to your Electron app, you can pass the path to your main process
|
||||
entry point (here, it is `main.js`).
|
||||
|
||||
```js {5}
|
||||
```js {5} @ts-nocheck
|
||||
const { _electron: electron } = require('playwright')
|
||||
const { test } = require('@playwright/test')
|
||||
|
||||
|
@ -169,7 +169,7 @@ test('launch app', async () => {
|
|||
After that, you will access to an instance of Playwright's `ElectronApp` class. This
|
||||
is a powerful class that has access to main process modules for example:
|
||||
|
||||
```js {6-11}
|
||||
```js {6-11} @ts-nocheck
|
||||
const { _electron: electron } = require('playwright')
|
||||
const { test } = require('@playwright/test')
|
||||
|
||||
|
@ -189,7 +189,7 @@ test('get isPackaged', async () => {
|
|||
It can also create individual [Page][playwright-page] objects from Electron BrowserWindow instances.
|
||||
For example, to grab the first BrowserWindow and save a screenshot:
|
||||
|
||||
```js {6-7}
|
||||
```js {6-7} @ts-nocheck
|
||||
const { _electron: electron } = require('playwright')
|
||||
const { test } = require('@playwright/test')
|
||||
|
||||
|
@ -205,7 +205,7 @@ test('save screenshot', async () => {
|
|||
Putting all this together using the PlayWright Test runner, let's create a `example.spec.js`
|
||||
test file with a single test and assertion:
|
||||
|
||||
```js title='example.spec.js'
|
||||
```js title='example.spec.js' @ts-nocheck
|
||||
const { _electron: electron } = require('playwright')
|
||||
const { test, expect } = require('@playwright/test')
|
||||
|
||||
|
@ -259,7 +259,7 @@ expose custom methods to your test suite.
|
|||
To create a custom driver, we'll use Node.js' [`child_process`](https://nodejs.org/api/child_process.html) API.
|
||||
The test suite will spawn the Electron process, then establish a simple messaging protocol:
|
||||
|
||||
```js title='testDriver.js'
|
||||
```js title='testDriver.js' @ts-nocheck
|
||||
const childProcess = require('child_process')
|
||||
const electronPath = require('electron')
|
||||
|
||||
|
@ -296,7 +296,7 @@ For convenience, you may want to wrap `appProcess` in a driver object that provi
|
|||
high-level functions. Here is an example of how you can do this. Let's start by creating
|
||||
a `TestDriver` class:
|
||||
|
||||
```js title='testDriver.js'
|
||||
```js title='testDriver.js' @ts-nocheck
|
||||
class TestDriver {
|
||||
constructor ({ path, args, env }) {
|
||||
this.rpcCalls = []
|
||||
|
@ -378,7 +378,7 @@ framework of your choosing. The following example uses
|
|||
[`ava`](https://www.npmjs.com/package/ava), but other popular choices like Jest
|
||||
or Mocha would work as well:
|
||||
|
||||
```js title='test.js'
|
||||
```js title='test.js' @ts-nocheck
|
||||
const test = require('ava')
|
||||
const electronPath = require('electron')
|
||||
const { TestDriver } = require('./testDriver')
|
||||
|
|
|
@ -67,7 +67,7 @@ are likely using [`electron-packager`][], which includes [`@electron/osx-sign`][
|
|||
If you're using Packager's API, you can pass [in configuration that both signs
|
||||
and notarizes your application](https://electron.github.io/electron-packager/main/interfaces/electronpackager.options.html).
|
||||
|
||||
```js
|
||||
```js @ts-nocheck
|
||||
const packager = require('electron-packager')
|
||||
|
||||
packager({
|
||||
|
@ -116,7 +116,7 @@ Electron app. This is the tool used under the hood by Electron Forge's
|
|||
`electron-winstaller` directly, use the `certificateFile` and `certificatePassword` configuration
|
||||
options when creating your installer.
|
||||
|
||||
```js {10-11}
|
||||
```js {10-11} @ts-nocheck
|
||||
const electronInstaller = require('electron-winstaller')
|
||||
// NB: Use this syntax within an async function, Node does not have support for
|
||||
// top-level await as of Node 12.
|
||||
|
@ -146,7 +146,7 @@ If you're not using Electron Forge and want to use `electron-wix-msi` directly,
|
|||
`certificateFile` and `certificatePassword` configuration options
|
||||
or pass in parameters directly to [SignTool.exe][] with the `signWithParams` option.
|
||||
|
||||
```js {12-13}
|
||||
```js {12-13} @ts-nocheck
|
||||
import { MSICreator } from 'electron-wix-msi'
|
||||
|
||||
// Step 1: Instantiate the MSICreator
|
||||
|
|
|
@ -16,7 +16,7 @@ Context isolation has been enabled by default since Electron 12, and it is a rec
|
|||
|
||||
Exposing APIs from your preload script to a loaded website in the renderer process is a common use-case. With context isolation disabled, your preload script would share a common global `window` object with the renderer. You could then attach arbitrary properties to a preload script:
|
||||
|
||||
```javascript title='preload.js'
|
||||
```javascript title='preload.js' @ts-nocheck
|
||||
// preload with contextIsolation disabled
|
||||
window.myAPI = {
|
||||
doAThing: () => {}
|
||||
|
@ -25,7 +25,7 @@ window.myAPI = {
|
|||
|
||||
The `doAThing()` function could then be used directly in the renderer process:
|
||||
|
||||
```javascript title='renderer.js'
|
||||
```javascript title='renderer.js' @ts-nocheck
|
||||
// use the exposed API in the renderer
|
||||
window.myAPI.doAThing()
|
||||
```
|
||||
|
@ -43,7 +43,7 @@ contextBridge.exposeInMainWorld('myAPI', {
|
|||
})
|
||||
```
|
||||
|
||||
```javascript title='renderer.js'
|
||||
```javascript title='renderer.js' @ts-nocheck
|
||||
// use the exposed API in the renderer
|
||||
window.myAPI.doAThing()
|
||||
```
|
||||
|
@ -98,7 +98,7 @@ declare global {
|
|||
|
||||
Doing so will ensure that the TypeScript compiler will know about the `electronAPI` property on your global `window` object when writing scripts in your renderer process:
|
||||
|
||||
```typescript title='renderer.ts'
|
||||
```typescript title='renderer.ts' @ts-nocheck
|
||||
window.electronAPI.loadPreferences()
|
||||
```
|
||||
|
||||
|
|
|
@ -116,7 +116,7 @@ Now the renderer process can communicate with the main process securely and perf
|
|||
|
||||
The `renderer.js` file is responsible for controlling the `<button>` functionality.
|
||||
|
||||
```js title='renderer.js'
|
||||
```js title='renderer.js' @ts-expect-error=[2,7]
|
||||
document.getElementById('toggle-dark-mode').addEventListener('click', async () => {
|
||||
const isDarkMode = await window.darkMode.toggle()
|
||||
document.getElementById('theme-source').innerHTML = isDarkMode ? 'Dark' : 'Light'
|
||||
|
|
|
@ -67,7 +67,7 @@ The loadBrowserProcessSpecificV8Snapshot fuse changes which V8 snapshot file is
|
|||
|
||||
We've made a handy module, [`@electron/fuses`](https://npmjs.com/package/@electron/fuses), to make flipping these fuses easy. Check out the README of that module for more details on usage and potential error cases.
|
||||
|
||||
```js
|
||||
```js @ts-nocheck
|
||||
require('@electron/fuses').flipFuses(
|
||||
// Path to electron
|
||||
require('electron'),
|
||||
|
|
|
@ -66,7 +66,7 @@ You can use environment variables to override the base URL, the path at which to
|
|||
look for Electron binaries, and the binary filename. The URL used by `@electron/get`
|
||||
is composed as follows:
|
||||
|
||||
```javascript
|
||||
```javascript @ts-nocheck
|
||||
url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME
|
||||
```
|
||||
|
||||
|
|
|
@ -138,7 +138,7 @@ To make these elements interactive, we'll be adding a few lines of code in the i
|
|||
`renderer.js` file that leverages the `window.electronAPI` functionality exposed from the preload
|
||||
script:
|
||||
|
||||
```javascript title='renderer.js (Renderer Process)'
|
||||
```javascript title='renderer.js (Renderer Process)' @ts-expect-error=[4,5]
|
||||
const setButton = document.getElementById('btn')
|
||||
const titleInput = document.getElementById('title')
|
||||
setButton.addEventListener('click', () => {
|
||||
|
@ -182,13 +182,13 @@ provided to the renderer process. Please refer to
|
|||
:::
|
||||
|
||||
```javascript {6-13,25} title='main.js (Main Process)'
|
||||
const { BrowserWindow, dialog, ipcMain } = require('electron')
|
||||
const { app, BrowserWindow, dialog, ipcMain } = require('electron')
|
||||
const path = require('path')
|
||||
|
||||
// ...
|
||||
|
||||
async function handleFileOpen () {
|
||||
const { canceled, filePaths } = await dialog.showOpenDialog()
|
||||
const { canceled, filePaths } = await dialog.showOpenDialog({})
|
||||
if (!canceled) {
|
||||
return filePaths[0]
|
||||
}
|
||||
|
@ -203,7 +203,7 @@ function createWindow () {
|
|||
mainWindow.loadFile('index.html')
|
||||
}
|
||||
|
||||
app.whenReady(() => {
|
||||
app.whenReady().then(() => {
|
||||
ipcMain.handle('dialog:openFile', handleFileOpen)
|
||||
createWindow()
|
||||
})
|
||||
|
@ -263,7 +263,7 @@ The UI consists of a single `#btn` button element that will be used to trigger o
|
|||
a `#filePath` element that will be used to display the path of the selected file. Making these
|
||||
pieces work will take a few lines of code in the renderer process script:
|
||||
|
||||
```javascript title='renderer.js (Renderer Process)'
|
||||
```javascript title='renderer.js (Renderer Process)' @ts-expect-error=[5]
|
||||
const btn = document.getElementById('btn')
|
||||
const filePathElement = document.getElementById('filePath')
|
||||
|
||||
|
@ -412,7 +412,7 @@ function createWindow () {
|
|||
For the purposes of the tutorial, it's important to note that the `click` handler
|
||||
sends a message (either `1` or `-1`) to the renderer process through the `update-counter` channel.
|
||||
|
||||
```javascript
|
||||
```javascript @ts-nocheck
|
||||
click: () => mainWindow.webContents.send('update-counter', -1)
|
||||
```
|
||||
|
||||
|
@ -486,7 +486,7 @@ To tie it all together, we'll create an interface in the loaded HTML file that c
|
|||
Finally, to make the values update in the HTML document, we'll add a few lines of DOM manipulation
|
||||
so that the value of the `#counter` element is updated whenever we fire an `update-counter` event.
|
||||
|
||||
```javascript title='renderer.js (Renderer Process)'
|
||||
```javascript title='renderer.js (Renderer Process)' @ts-nocheck
|
||||
const counter = document.getElementById('counter')
|
||||
|
||||
window.electronAPI.onUpdateCounter((_event, value) => {
|
||||
|
@ -509,7 +509,7 @@ We can demonstrate this with slight modifications to the code from the previous
|
|||
renderer process, use the `event` parameter to send a reply back to the main process through the
|
||||
`counter-value` channel.
|
||||
|
||||
```javascript title='renderer.js (Renderer Process)'
|
||||
```javascript title='renderer.js (Renderer Process)' @ts-nocheck
|
||||
const counter = document.getElementById('counter')
|
||||
|
||||
window.electronAPI.onUpdateCounter((event, value) => {
|
||||
|
|
|
@ -56,7 +56,7 @@ Starting with a working application from the
|
|||
[Quick Start Guide](quick-start.md), update the `main.js` file with the
|
||||
following lines:
|
||||
|
||||
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/global'
|
||||
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/global' @ts-type={createWindow:()=>void}
|
||||
const { app, globalShortcut } = require('electron')
|
||||
|
||||
app.whenReady().then(() => {
|
||||
|
@ -131,7 +131,7 @@ If you don't want to do manual shortcut parsing, there are libraries that do
|
|||
advanced key detection, such as [mousetrap][]. Below are examples of usage of the
|
||||
`mousetrap` running in the Renderer process:
|
||||
|
||||
```js
|
||||
```js @ts-nocheck
|
||||
Mousetrap.bind('4', () => { console.log('4') })
|
||||
Mousetrap.bind('?', () => { console.log('show shortcuts!') })
|
||||
Mousetrap.bind('esc', () => { console.log('escape') }, 'keyup')
|
||||
|
|
|
@ -45,6 +45,8 @@ if (process.defaultApp) {
|
|||
We will now define the function in charge of creating our browser window and load our application's `index.html` file.
|
||||
|
||||
```javascript
|
||||
let mainWindow
|
||||
|
||||
const createWindow = () => {
|
||||
// Create the browser window.
|
||||
mainWindow = new BrowserWindow({
|
||||
|
@ -65,7 +67,7 @@ This code will be different in Windows compared to MacOS and Linux. This is due
|
|||
|
||||
#### Windows code:
|
||||
|
||||
```javascript
|
||||
```javascript @ts-type={mainWindow:Electron.BrowserWindow} @ts-type={createWindow:()=>void}
|
||||
const gotTheLock = app.requestSingleInstanceLock()
|
||||
|
||||
if (!gotTheLock) {
|
||||
|
@ -91,7 +93,7 @@ if (!gotTheLock) {
|
|||
|
||||
#### MacOS and Linux code:
|
||||
|
||||
```javascript
|
||||
```javascript @ts-type={createWindow:()=>void}
|
||||
// This method will be called when Electron has finished
|
||||
// initialization and is ready to create browser windows.
|
||||
// Some APIs can only be used after this event occurs.
|
||||
|
@ -166,7 +168,7 @@ If you're using Electron Packager's API, adding support for protocol handlers is
|
|||
Electron Forge is handled, except
|
||||
`protocols` is part of the Packager options passed to the `packager` function.
|
||||
|
||||
```javascript
|
||||
```javascript @ts-nocheck
|
||||
const packager = require('electron-packager')
|
||||
|
||||
packager({
|
||||
|
|
|
@ -126,7 +126,7 @@ app.whenReady().then(async () => {
|
|||
Then, in your preload scripts you receive the port through IPC and set up the
|
||||
listeners.
|
||||
|
||||
```js title='preloadMain.js and preloadSecondary.js (Preload scripts)'
|
||||
```js title='preloadMain.js and preloadSecondary.js (Preload scripts)' @ts-nocheck
|
||||
const { ipcRenderer } = require('electron')
|
||||
|
||||
ipcRenderer.on('port', e => {
|
||||
|
@ -148,7 +148,7 @@ That means window.electronMessagePort is globally available and you can call
|
|||
`postMessage` on it from anywhere in your app to send a message to the other
|
||||
renderer.
|
||||
|
||||
```js title='renderer.js (Renderer Process)'
|
||||
```js title='renderer.js (Renderer Process)' @ts-nocheck
|
||||
// elsewhere in your code to send a message to the other renderers message handler
|
||||
window.electronMessagePort.postmessage('ping')
|
||||
```
|
||||
|
@ -181,7 +181,7 @@ app.whenReady().then(async () => {
|
|||
// We can't use ipcMain.handle() here, because the reply needs to transfer a
|
||||
// MessagePort.
|
||||
// Listen for message sent from the top-level frame
|
||||
mainWindow.webContents.mainFrame.on('request-worker-channel', (event) => {
|
||||
mainWindow.webContents.mainFrame.ipc.on('request-worker-channel', (event) => {
|
||||
// Create a new channel ...
|
||||
const { port1, port2 } = new MessageChannelMain()
|
||||
// ... send one end to the worker ...
|
||||
|
@ -245,7 +245,7 @@ Electron's built-in IPC methods only support two modes: fire-and-forget
|
|||
can implement a "response stream", where a single request responds with a
|
||||
stream of data.
|
||||
|
||||
```js title='renderer.js (Renderer Process)'
|
||||
```js title='renderer.js (Renderer Process)' @ts-expect-error=[18]
|
||||
const makeStreamingRequest = (element, callback) => {
|
||||
// MessageChannels are lightweight--it's cheap to create a new one for each
|
||||
// request.
|
||||
|
|
|
@ -42,7 +42,7 @@ safe.
|
|||
The only way to load a native module safely for now, is to make sure the app
|
||||
loads no native modules after the Web Workers get started.
|
||||
|
||||
```javascript
|
||||
```javascript @ts-expect-error=[1]
|
||||
process.dlopen = () => {
|
||||
throw new Error('Load native module is not safe')
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ In `preload.js` use the [`contextBridge`][] to inject a method `window.electron.
|
|||
|
||||
```js
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
const path = require('path')
|
||||
|
||||
contextBridge.exposeInMainWorld('electron', {
|
||||
startDrag: (fileName) => {
|
||||
|
@ -43,7 +44,7 @@ Add a draggable element to `index.html`, and reference your renderer script:
|
|||
|
||||
In `renderer.js` set up the renderer process to handle drag events by calling the method you added via the [`contextBridge`][] above.
|
||||
|
||||
```javascript
|
||||
```javascript @ts-expect-error=[3]
|
||||
document.getElementById('drag').ondragstart = (event) => {
|
||||
event.preventDefault()
|
||||
window.electron.startDrag('drag-and-drop.md')
|
||||
|
|
|
@ -173,7 +173,7 @@ in the fictitious `.foo` format. In order to do that, it relies on the
|
|||
equally fictitious `foo-parser` module. In traditional Node.js development,
|
||||
you might write code that eagerly loads dependencies:
|
||||
|
||||
```js title='parser.js'
|
||||
```js title='parser.js' @ts-expect-error=[2]
|
||||
const fs = require('fs')
|
||||
const fooParser = require('foo-parser')
|
||||
|
||||
|
@ -196,7 +196,7 @@ In the above example, we're doing a lot of work that's being executed as soon
|
|||
as the file is loaded. Do we need to get parsed files right away? Could we
|
||||
do this work a little later, when `getParsedFiles()` is actually called?
|
||||
|
||||
```js title='parser.js'
|
||||
```js title='parser.js' @ts-expect-error=[20]
|
||||
// "fs" is likely already being loaded, so the `require()` call is cheap
|
||||
const fs = require('fs')
|
||||
|
||||
|
@ -205,7 +205,7 @@ class Parser {
|
|||
// Touch the disk as soon as `getFiles` is called, not sooner.
|
||||
// Also, ensure that we're not blocking other operations by using
|
||||
// the asynchronous version.
|
||||
this.files = this.files || await fs.readdir('.')
|
||||
this.files = this.files || await fs.promises.readdir('.')
|
||||
|
||||
return this.files
|
||||
}
|
||||
|
|
|
@ -175,13 +175,13 @@ Although preload scripts share a `window` global with the renderer they're attac
|
|||
you cannot directly attach any variables from the preload script to `window` because of
|
||||
the [`contextIsolation`][context-isolation] default.
|
||||
|
||||
```js title='preload.js'
|
||||
```js title='preload.js' @ts-nocheck
|
||||
window.myAPI = {
|
||||
desktop: true
|
||||
}
|
||||
```
|
||||
|
||||
```js title='renderer.js'
|
||||
```js title='renderer.js' @ts-nocheck
|
||||
console.log(window.myAPI)
|
||||
// => undefined
|
||||
```
|
||||
|
@ -200,7 +200,7 @@ contextBridge.exposeInMainWorld('myAPI', {
|
|||
})
|
||||
```
|
||||
|
||||
```js title='renderer.js'
|
||||
```js title='renderer.js' @ts-nocheck
|
||||
console.log(window.myAPI)
|
||||
// => { desktop: true }
|
||||
```
|
||||
|
|
|
@ -182,7 +182,7 @@ In Electron, browser windows can only be created after the `app` module's
|
|||
[`app.whenReady()`][app-when-ready] API. Call `createWindow()` after `whenReady()`
|
||||
resolves its Promise.
|
||||
|
||||
```js
|
||||
```js @ts-type={createWindow:()=>void}
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
})
|
||||
|
@ -239,7 +239,7 @@ from within your existing `whenReady()` callback.
|
|||
|
||||
[activate]: ../api/app.md#event-activate-macos
|
||||
|
||||
```js
|
||||
```js @ts-type={createWindow:()=>void}
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
|
||||
|
@ -290,6 +290,7 @@ To attach this script to your renderer process, pass in the path to your preload
|
|||
to the `webPreferences.preload` option in your existing `BrowserWindow` constructor.
|
||||
|
||||
```js
|
||||
const { app, BrowserWindow } = require('electron')
|
||||
// include the Node.js 'path' module at the top of your file
|
||||
const path = require('path')
|
||||
|
||||
|
|
|
@ -141,7 +141,7 @@ like `HTTP`. Similarly, we recommend the use of `WSS` over `WS`, `FTPS` over
|
|||
|
||||
#### How?
|
||||
|
||||
```js title='main.js (Main Process)'
|
||||
```js title='main.js (Main Process)' @ts-type={browserWindow:Electron.BrowserWindow}
|
||||
// Bad
|
||||
browserWindow.loadURL('http://example.com')
|
||||
|
||||
|
@ -278,7 +278,7 @@ security-conscious developers might want to assume the very opposite.
|
|||
|
||||
```js title='main.js (Main Process)'
|
||||
const { session } = require('electron')
|
||||
const URL = require('url').URL
|
||||
const { URL } = require('url')
|
||||
|
||||
session
|
||||
.fromPartition('some-partition')
|
||||
|
@ -608,7 +608,8 @@ sometimes be fooled - a `startsWith('https://example.com')` test would let
|
|||
`https://example.com.attacker.com` through.
|
||||
|
||||
```js title='main.js (Main Process)'
|
||||
const URL = require('url').URL
|
||||
const { URL } = require('url')
|
||||
const { app } = require('electron')
|
||||
|
||||
app.on('web-contents-created', (event, contents) => {
|
||||
contents.on('will-navigate', (event, navigationUrl) => {
|
||||
|
@ -647,8 +648,8 @@ receive, amongst other parameters, the `url` the window was requested to open
|
|||
and the options used to create it. We recommend that you register a handler to
|
||||
monitor the creation of windows, and deny any unexpected window creation.
|
||||
|
||||
```js title='main.js (Main Process)'
|
||||
const { shell } = require('electron')
|
||||
```js title='main.js (Main Process)' @ts-type={isSafeForExternalOpen:(url:string)=>boolean}
|
||||
const { app, shell } = require('electron')
|
||||
|
||||
app.on('web-contents-created', (event, contents) => {
|
||||
contents.setWindowOpenHandler(({ url }) => {
|
||||
|
@ -683,7 +684,7 @@ leveraged to execute arbitrary commands.
|
|||
|
||||
#### How?
|
||||
|
||||
```js title='main.js (Main Process)'
|
||||
```js title='main.js (Main Process)' @ts-type={USER_CONTROLLED_DATA_HERE:string}
|
||||
// Bad
|
||||
const { shell } = require('electron')
|
||||
shell.openExternal(USER_CONTROLLED_DATA_HERE)
|
||||
|
@ -739,7 +740,7 @@ You should be validating the `sender` of **all** IPC messages by default.
|
|||
|
||||
#### How?
|
||||
|
||||
```js title='main.js (Main Process)'
|
||||
```js title='main.js (Main Process)' @ts-type={getSecrets:()=>unknown}
|
||||
// Bad
|
||||
ipcMain.handle('get-secrets', () => {
|
||||
return getSecrets()
|
||||
|
|
|
@ -72,7 +72,7 @@ npx electron-installer-snap --src=out/myappname-linux-x64
|
|||
If you have an existing build pipeline, you can use `electron-installer-snap`
|
||||
programmatically. For more information, see the [Snapcraft API docs][snapcraft-syntax].
|
||||
|
||||
```js
|
||||
```js @ts-nocheck
|
||||
const snap = require('electron-installer-snap')
|
||||
|
||||
snap(options)
|
||||
|
|
|
@ -20,12 +20,12 @@ On macOS as we use the native APIs there is no way to set the language that the
|
|||
|
||||
For Windows and Linux there are a few Electron APIs you should use to set the languages for the spellchecker.
|
||||
|
||||
```js
|
||||
```js @ts-type={myWindow:Electron.BrowserWindow}
|
||||
// Sets the spellchecker to check English US and French
|
||||
myWindow.session.setSpellCheckerLanguages(['en-US', 'fr'])
|
||||
myWindow.webContents.session.setSpellCheckerLanguages(['en-US', 'fr'])
|
||||
|
||||
// An array of all available language codes
|
||||
const possibleLanguages = myWindow.session.availableSpellCheckerLanguages
|
||||
const possibleLanguages = myWindow.webContents.session.availableSpellCheckerLanguages
|
||||
```
|
||||
|
||||
By default the spellchecker will enable the language matching the current OS locale.
|
||||
|
@ -35,7 +35,7 @@ By default the spellchecker will enable the language matching the current OS loc
|
|||
All the required information to generate a context menu is provided in the [`context-menu`](../api/web-contents.md#event-context-menu) event on each `webContents` instance. A small example
|
||||
of how to make a context menu with this information is provided below.
|
||||
|
||||
```js
|
||||
```js @ts-type={myWindow:Electron.BrowserWindow}
|
||||
const { Menu, MenuItem } = require('electron')
|
||||
|
||||
myWindow.webContents.on('context-menu', (event, params) => {
|
||||
|
@ -45,7 +45,7 @@ myWindow.webContents.on('context-menu', (event, params) => {
|
|||
for (const suggestion of params.dictionarySuggestions) {
|
||||
menu.append(new MenuItem({
|
||||
label: suggestion,
|
||||
click: () => mainWindow.webContents.replaceMisspelling(suggestion)
|
||||
click: () => myWindow.webContents.replaceMisspelling(suggestion)
|
||||
}))
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ myWindow.webContents.on('context-menu', (event, params) => {
|
|||
menu.append(
|
||||
new MenuItem({
|
||||
label: 'Add to dictionary',
|
||||
click: () => mainWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
|
||||
click: () => myWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
@ -67,8 +67,8 @@ myWindow.webContents.on('context-menu', (event, params) => {
|
|||
|
||||
Although the spellchecker itself does not send any typings, words or user input to Google services the hunspell dictionary files are downloaded from a Google CDN by default. If you want to avoid this you can provide an alternative URL to download the dictionaries from.
|
||||
|
||||
```js
|
||||
myWindow.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')
|
||||
```js @ts-type={myWindow:Electron.BrowserWindow}
|
||||
myWindow.webContents.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')
|
||||
```
|
||||
|
||||
Check out the docs for [`session.setSpellCheckerDictionaryDownloadURL`](../api/session.md#sessetspellcheckerdictionarydownloadurlurl) for more information on where to get the dictionary files from and how you need to host them.
|
||||
|
|
|
@ -51,7 +51,7 @@ app.whenReady().then(() => {
|
|||
|
||||
Great! Now we can start attaching a context menu to our Tray, like so:
|
||||
|
||||
```js
|
||||
```js @ts-expect-error=[8]
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{ label: 'Item1', type: 'radio' },
|
||||
{ label: 'Item2', type: 'radio' },
|
||||
|
@ -68,7 +68,7 @@ To read more about constructing native menus, click
|
|||
|
||||
Finally, let's give our tray a tooltip and a title.
|
||||
|
||||
```js
|
||||
```js @ts-type={tray:Electron.Tray}
|
||||
tray.setToolTip('This is my application')
|
||||
tray.setTitle('This is my title')
|
||||
```
|
||||
|
|
|
@ -256,7 +256,7 @@ const createWindow = () => {
|
|||
|
||||
### Calling your function when the app is ready
|
||||
|
||||
```js title='main.js (Lines 12-14)'
|
||||
```js title='main.js (Lines 12-14)' @ts-type={createWindow:()=>void}
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
})
|
||||
|
@ -336,7 +336,7 @@ Because windows cannot be created before the `ready` event, you should only list
|
|||
`activate` events after your app is initialized. Do this by only listening for activate
|
||||
events inside your existing `whenReady()` callback.
|
||||
|
||||
```js
|
||||
```js @ts-type={createWindow:()=>void}
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
|
||||
|
|
|
@ -118,7 +118,7 @@ information in the window. This variable can be accessed via `window.versions` o
|
|||
`versions`. Create a `renderer.js` script that uses the [`document.getElementById`][]
|
||||
DOM API to replace the displayed text for the HTML element with `info` as its `id` property.
|
||||
|
||||
```js title="renderer.js"
|
||||
```js title="renderer.js" @ts-nocheck
|
||||
const information = document.getElementById('info')
|
||||
information.innerText = `This app is using Chrome (v${versions.chrome()}), Node.js (v${versions.node()}), and Electron (v${versions.electron()})`
|
||||
```
|
||||
|
@ -225,7 +225,7 @@ app.whenReady().then(() => {
|
|||
Once you have the sender and receiver set up, you can now send messages from the renderer
|
||||
to the main process through the `'ping'` channel you just defined.
|
||||
|
||||
```js title='renderer.js'
|
||||
```js title='renderer.js' @ts-expect-error=[2]
|
||||
const func = async () => {
|
||||
const response = await window.versions.ping()
|
||||
console.log(response) // prints out 'pong'
|
||||
|
|
|
@ -188,7 +188,7 @@ npm install update-electron-app
|
|||
|
||||
Then, import the module and call it immediately in the main process.
|
||||
|
||||
```js title='main.js'
|
||||
```js title='main.js' @ts-nocheck
|
||||
require('update-electron-app')()
|
||||
```
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ npm install update-electron-app
|
|||
|
||||
Then, invoke the updater from your app's main process file:
|
||||
|
||||
```js title="main.js"
|
||||
```js title="main.js" @ts-nocheck
|
||||
require('update-electron-app')()
|
||||
```
|
||||
|
||||
|
@ -113,7 +113,7 @@ Now that you've configured the basic update mechanism for your application, you
|
|||
need to ensure that the user will get notified when there's an update. This
|
||||
can be achieved using the [autoUpdater API events](../api/auto-updater.md#events):
|
||||
|
||||
```javascript title="main.js"
|
||||
```javascript title="main.js" @ts-expect-error=[11]
|
||||
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
|
||||
const dialogOpts = {
|
||||
type: 'info',
|
||||
|
|
|
@ -196,9 +196,9 @@ const win = new BrowserWindow({
|
|||
}
|
||||
})
|
||||
|
||||
ipcMain.on('set-ignore-mouse-events', (event, ...args) => {
|
||||
ipcMain.on('set-ignore-mouse-events', (event, ignore, options) => {
|
||||
const win = BrowserWindow.fromWebContents(event.sender)
|
||||
win.setIgnoreMouseEvents(...args)
|
||||
win.setIgnoreMouseEvents(ignore, options)
|
||||
})
|
||||
```
|
||||
|
||||
|
|
|
@ -125,7 +125,7 @@ Starting with a working application from the
|
|||
following lines:
|
||||
|
||||
```javascript
|
||||
const { BrowserWindow } = require('electron')
|
||||
const { BrowserWindow, nativeImage } = require('electron')
|
||||
const path = require('path')
|
||||
|
||||
const win = new BrowserWindow()
|
||||
|
@ -133,11 +133,11 @@ const win = new BrowserWindow()
|
|||
win.setThumbarButtons([
|
||||
{
|
||||
tooltip: 'button1',
|
||||
icon: path.join(__dirname, 'button1.png'),
|
||||
icon: nativeImage.createFromPath(path.join(__dirname, 'button1.png')),
|
||||
click () { console.log('button1 clicked') }
|
||||
}, {
|
||||
tooltip: 'button2',
|
||||
icon: path.join(__dirname, 'button2.png'),
|
||||
icon: nativeImage.createFromPath(path.join(__dirname, 'button2.png')),
|
||||
flags: ['enabled', 'dismissonclick'],
|
||||
click () { console.log('button2 clicked.') }
|
||||
}
|
||||
|
@ -189,11 +189,11 @@ Starting with a working application from the
|
|||
following lines:
|
||||
|
||||
```javascript
|
||||
const { BrowserWindow } = require('electron')
|
||||
const { BrowserWindow, nativeImage } = require('electron')
|
||||
|
||||
const win = new BrowserWindow()
|
||||
|
||||
win.setOverlayIcon('path/to/overlay.png', 'Description for overlay')
|
||||
win.setOverlayIcon(nativeImage.createFromPath('path/to/overlay.png'), 'Description for overlay')
|
||||
```
|
||||
|
||||
[msdn-icon-overlay]: https://learn.microsoft.com/en-us/windows/win32/shell/taskbar-extensions#icon-overlays
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue