feat: make async webContents / <webview> methods return a Promise (#18792)
This commit is contained in:
parent
632bbf948d
commit
deebde66f9
6 changed files with 32 additions and 35 deletions
|
@ -987,6 +987,8 @@ Returns `String` - The user agent for this web page.
|
||||||
|
|
||||||
* `css` String
|
* `css` String
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Injects CSS into the current web page.
|
Injects CSS into the current web page.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
@ -1065,6 +1067,8 @@ Returns `Number` - the current zoom level.
|
||||||
* `minimumLevel` Number
|
* `minimumLevel` Number
|
||||||
* `maximumLevel` Number
|
* `maximumLevel` Number
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Sets the maximum and minimum pinch-to-zoom level.
|
Sets the maximum and minimum pinch-to-zoom level.
|
||||||
|
|
||||||
> **NOTE**: Visual zoom is disabled by default in Electron. To re-enable it, call:
|
> **NOTE**: Visual zoom is disabled by default in Electron. To re-enable it, call:
|
||||||
|
@ -1078,6 +1082,8 @@ Sets the maximum and minimum pinch-to-zoom level.
|
||||||
* `minimumLevel` Number
|
* `minimumLevel` Number
|
||||||
* `maximumLevel` Number
|
* `maximumLevel` Number
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Sets the maximum and minimum layout-based (i.e. non-visual) zoom level.
|
Sets the maximum and minimum layout-based (i.e. non-visual) zoom level.
|
||||||
|
|
||||||
#### `contents.undo()`
|
#### `contents.undo()`
|
||||||
|
@ -1139,6 +1145,8 @@ Executes the editing command `replaceMisspelling` in web page.
|
||||||
|
|
||||||
* `text` String
|
* `text` String
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Inserts `text` to the focused element.
|
Inserts `text` to the focused element.
|
||||||
|
|
||||||
#### `contents.findInPage(text[, options])`
|
#### `contents.findInPage(text[, options])`
|
||||||
|
|
|
@ -379,6 +379,8 @@ Returns `String` - The user agent for guest page.
|
||||||
|
|
||||||
* `css` String
|
* `css` String
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Injects CSS into the guest page.
|
Injects CSS into the guest page.
|
||||||
|
|
||||||
### `<webview>.executeJavaScript(code[, userGesture])`
|
### `<webview>.executeJavaScript(code[, userGesture])`
|
||||||
|
@ -490,6 +492,8 @@ Executes editing command `replaceMisspelling` in page.
|
||||||
|
|
||||||
* `text` String
|
* `text` String
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Inserts `text` to the focused element.
|
Inserts `text` to the focused element.
|
||||||
|
|
||||||
### `<webview>.findInPage(text[, options])`
|
### `<webview>.findInPage(text[, options])`
|
||||||
|
@ -531,6 +535,8 @@ Stops any `findInPage` request for the `webview` with the provided `action`.
|
||||||
the web page. Default is `false`.
|
the web page. Default is `false`.
|
||||||
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
|
* `deviceName` String (optional) - Set the printer device name to use. Default is `''`.
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Prints `webview`'s web page. Same as `webContents.print([options])`.
|
Prints `webview`'s web page. Same as `webContents.print([options])`.
|
||||||
|
|
||||||
### `<webview>.printToPDF(options)`
|
### `<webview>.printToPDF(options)`
|
||||||
|
@ -562,6 +568,8 @@ Captures a snapshot of the page within `rect`. Omitting `rect` will capture the
|
||||||
* `channel` String
|
* `channel` String
|
||||||
* `...args` any[]
|
* `...args` any[]
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Send an asynchronous message to renderer process via `channel`, you can also
|
Send an asynchronous message to renderer process via `channel`, you can also
|
||||||
send arbitrary arguments. The renderer process can handle the message by
|
send arbitrary arguments. The renderer process can handle the message by
|
||||||
listening to the `channel` event with the [`ipcRenderer`](ipc-renderer.md) module.
|
listening to the `channel` event with the [`ipcRenderer`](ipc-renderer.md) module.
|
||||||
|
@ -573,6 +581,8 @@ examples.
|
||||||
|
|
||||||
* `event` Object
|
* `event` Object
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Sends an input `event` to the page.
|
Sends an input `event` to the page.
|
||||||
|
|
||||||
See [webContents.sendInputEvent](web-contents.md#contentssendinputeventevent)
|
See [webContents.sendInputEvent](web-contents.md#contentssendinputeventevent)
|
||||||
|
@ -607,6 +617,8 @@ Returns `Number` - the current zoom level.
|
||||||
* `minimumLevel` Number
|
* `minimumLevel` Number
|
||||||
* `maximumLevel` Number
|
* `maximumLevel` Number
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Sets the maximum and minimum pinch-to-zoom level.
|
Sets the maximum and minimum pinch-to-zoom level.
|
||||||
|
|
||||||
### `<webview>.setLayoutZoomLevelLimits(minimumLevel, maximumLevel)`
|
### `<webview>.setLayoutZoomLevelLimits(minimumLevel, maximumLevel)`
|
||||||
|
@ -614,6 +626,8 @@ Sets the maximum and minimum pinch-to-zoom level.
|
||||||
* `minimumLevel` Number
|
* `minimumLevel` Number
|
||||||
* `maximumLevel` Number
|
* `maximumLevel` Number
|
||||||
|
|
||||||
|
Returns `Promise<void>`
|
||||||
|
|
||||||
Sets the maximum and minimum layout-based (i.e. non-visual) zoom level.
|
Sets the maximum and minimum layout-based (i.e. non-visual) zoom level.
|
||||||
|
|
||||||
### `<webview>.showDefinitionForSelection()` _macOS_
|
### `<webview>.showDefinitionForSelection()` _macOS_
|
||||||
|
|
|
@ -180,7 +180,7 @@ const webFrameMethods = [
|
||||||
|
|
||||||
for (const method of webFrameMethods) {
|
for (const method of webFrameMethods) {
|
||||||
WebContents.prototype[method] = function (...args) {
|
WebContents.prototype[method] = function (...args) {
|
||||||
ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
|
return ipcMainUtils.invokeInWebContents(this, false, 'ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', method, ...args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,7 @@ const { webContents } = require('electron')
|
||||||
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
||||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
||||||
const parseFeaturesString = require('@electron/internal/common/parse-features-string')
|
const parseFeaturesString = require('@electron/internal/common/parse-features-string')
|
||||||
const {
|
const { syncMethods, asyncMethods } = require('@electron/internal/common/web-view-methods')
|
||||||
syncMethods,
|
|
||||||
asyncCallbackMethods,
|
|
||||||
asyncPromiseMethods
|
|
||||||
} = require('@electron/internal/common/web-view-methods')
|
|
||||||
|
|
||||||
// Doesn't exist in early initialization.
|
// Doesn't exist in early initialization.
|
||||||
let webViewManager = null
|
let webViewManager = null
|
||||||
|
@ -371,11 +367,7 @@ ipcMainInternal.on('ELECTRON_GUEST_VIEW_MANAGER_FOCUS_CHANGE', function (event,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const allMethods = new Set([
|
const allMethods = new Set([ ...syncMethods, ...asyncMethods ])
|
||||||
...syncMethods,
|
|
||||||
...asyncCallbackMethods,
|
|
||||||
...asyncPromiseMethods
|
|
||||||
])
|
|
||||||
|
|
||||||
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId, method, args) {
|
handleMessage('ELECTRON_GUEST_VIEW_MANAGER_CALL', function (event, guestInstanceId, method, args) {
|
||||||
const guest = getGuestForWebContents(guestInstanceId, event.sender)
|
const guest = getGuestForWebContents(guestInstanceId, event.sender)
|
||||||
|
|
|
@ -53,18 +53,15 @@ exports.syncMethods = new Set([
|
||||||
'setZoomLevel'
|
'setZoomLevel'
|
||||||
])
|
])
|
||||||
|
|
||||||
exports.asyncCallbackMethods = new Set([
|
exports.asyncMethods = new Set([
|
||||||
|
'capturePage',
|
||||||
|
'executeJavaScript',
|
||||||
'insertCSS',
|
'insertCSS',
|
||||||
'insertText',
|
'insertText',
|
||||||
'send',
|
'send',
|
||||||
'sendInputEvent',
|
'sendInputEvent',
|
||||||
'setLayoutZoomLevelLimits',
|
'setLayoutZoomLevelLimits',
|
||||||
'setVisualZoomLevelLimits',
|
'setVisualZoomLevelLimits',
|
||||||
'print'
|
'print',
|
||||||
])
|
|
||||||
|
|
||||||
exports.asyncPromiseMethods = new Set([
|
|
||||||
'capturePage',
|
|
||||||
'executeJavaScript',
|
|
||||||
'printToPDF'
|
'printToPDF'
|
||||||
])
|
])
|
||||||
|
|
|
@ -3,11 +3,7 @@ import { remote, webFrame } from 'electron'
|
||||||
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
import * as ipcRendererUtils from '@electron/internal/renderer/ipc-renderer-internal-utils'
|
||||||
import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal'
|
import * as guestViewInternal from '@electron/internal/renderer/web-view/guest-view-internal'
|
||||||
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
|
import { WEB_VIEW_CONSTANTS } from '@electron/internal/renderer/web-view/web-view-constants'
|
||||||
import {
|
import { syncMethods, asyncMethods } from '@electron/internal/common/web-view-methods'
|
||||||
syncMethods,
|
|
||||||
asyncCallbackMethods,
|
|
||||||
asyncPromiseMethods
|
|
||||||
} from '@electron/internal/common/web-view-methods'
|
|
||||||
|
|
||||||
const v8Util = process.electronBinding('v8_util')
|
const v8Util = process.electronBinding('v8_util')
|
||||||
|
|
||||||
|
@ -257,23 +253,13 @@ export const setupMethods = (WebViewElement: typeof ElectronInternal.WebViewElem
|
||||||
}
|
}
|
||||||
|
|
||||||
const createNonBlockHandler = function (method: string) {
|
const createNonBlockHandler = function (method: string) {
|
||||||
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
|
||||||
ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const method of asyncCallbackMethods) {
|
|
||||||
(WebViewElement.prototype as Record<string, any>)[method] = createNonBlockHandler(method)
|
|
||||||
}
|
|
||||||
|
|
||||||
const createPromiseHandler = function (method: string) {
|
|
||||||
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
return function (this: ElectronInternal.WebViewElement, ...args: Array<any>) {
|
||||||
return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
return ipcRendererUtils.invoke('ELECTRON_GUEST_VIEW_MANAGER_CALL', this.getWebContentsId(), method, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const method of asyncPromiseMethods) {
|
for (const method of asyncMethods) {
|
||||||
(WebViewElement.prototype as Record<string, any>)[method] = createPromiseHandler(method)
|
(WebViewElement.prototype as Record<string, any>)[method] = createNonBlockHandler(method)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue