feat: add <webview>.sendToFrame() / frameId to 'ipc-message' event (#30451)

This commit is contained in:
Milan Burda 2021-08-25 09:46:46 +02:00 committed by GitHub
parent be43996d35
commit 501ac15b1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 3 deletions

View file

@ -606,6 +606,21 @@ listening to the `channel` event with the [`ipcRenderer`](ipc-renderer.md) modul
See [webContents.send](web-contents.md#contentssendchannel-args) for See [webContents.send](web-contents.md#contentssendchannel-args) for
examples. examples.
### `<webview>.sendToFrame(frameId, channel, ...args)`
* `frameId` [number, number] - `[processId, frameId]`
* `channel` String
* `...args` any[]
Returns `Promise<void>`
Send an asynchronous message to renderer process via `channel`, you can also
send arbitrary arguments. The renderer process can handle the message by
listening to the `channel` event with the [`ipcRenderer`](ipc-renderer.md) module.
See [webContents.sendToFrame](web-contents.md#contentssendtoframeframeid-channel-args) for
examples.
### `<webview>.sendInputEvent(event)` ### `<webview>.sendInputEvent(event)`
* `event` [MouseInputEvent](structures/mouse-input-event.md) | [MouseWheelInputEvent](structures/mouse-wheel-input-event.md) | [KeyboardInputEvent](structures/keyboard-input-event.md) * `event` [MouseInputEvent](structures/mouse-input-event.md) | [MouseWheelInputEvent](structures/mouse-wheel-input-event.md) | [KeyboardInputEvent](structures/keyboard-input-event.md)
@ -920,6 +935,7 @@ webview.addEventListener('close', () => {
Returns: Returns:
* `frameId` [number, number] - pair of `[processId, frameId]`.
* `channel` String * `channel` String
* `args` any[] * `args` any[]

View file

@ -161,8 +161,12 @@ const createGuest = function (embedder: Electron.WebContents, embedderFrameId: n
}); });
// Dispatch guest's IPC messages to embedder. // Dispatch guest's IPC messages to embedder.
guest.on('ipc-message-host' as any, function (_: Electron.Event, channel: string, args: any[]) { guest.on('ipc-message-host' as any, function (event: Electron.IpcMainEvent, channel: string, args: any[]) {
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'ipc-message', { channel, args }); sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_DISPATCH_EVENT, 'ipc-message', {
frameId: [event.processId, event.frameId],
channel,
args
});
}); });
// Notify guest of embedder window visibility when it is ready // Notify guest of embedder window visibility when it is ready

View file

@ -65,6 +65,7 @@ export const asyncMethods = new Set([
'insertText', 'insertText',
'removeInsertedCSS', 'removeInsertedCSS',
'send', 'send',
'sendToFrame',
'sendInputEvent', 'sendInputEvent',
'setLayoutZoomLevelLimits', 'setLayoutZoomLevelLimits',
'setVisualZoomLevelLimits', 'setVisualZoomLevelLimits',

View file

@ -267,6 +267,24 @@ describe('<webview> tag', function () {
expect(args).to.deep.equal([message]); expect(args).to.deep.equal([message]);
}); });
it('<webview>.sendToFrame()', async () => {
loadWebView(webview, {
nodeintegration: 'on',
webpreferences: 'contextIsolation=no',
preload: `${fixtures}/module/preload-ipc.js`,
src: `file://${fixtures}/pages/ipc-message.html`
});
const { frameId } = await waitForEvent(webview, 'ipc-message');
const message = 'boom!';
webview.sendToFrame(frameId, 'ping', message);
const { channel, args } = await waitForEvent(webview, 'ipc-message');
expect(channel).to.equal('pong');
expect(args).to.deep.equal([message]);
});
it('works without script tag in page', async () => { it('works without script tag in page', async () => {
const message = await startLoadingWebViewAndWaitForMessage(webview, { const message = await startLoadingWebViewAndWaitForMessage(webview, {
preload: `${fixtures}/module/preload.js`, preload: `${fixtures}/module/preload.js`,
@ -529,8 +547,9 @@ describe('<webview> tag', function () {
webpreferences: 'contextIsolation=no', webpreferences: 'contextIsolation=no',
src: `file://${fixtures}/pages/ipc-message.html` src: `file://${fixtures}/pages/ipc-message.html`
}); });
const { channel, args } = await waitForEvent(webview, 'ipc-message'); const { frameId, channel, args } = await waitForEvent(webview, 'ipc-message');
expect(frameId).to.be.an('array').that.has.lengthOf(2);
expect(channel).to.equal('channel'); expect(channel).to.equal('channel');
expect(args).to.deep.equal(['arg1', 'arg2']); expect(args).to.deep.equal(['arg1', 'arg2']);
}); });