mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
This commit is contained in:
@@ -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
|
||||
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)`
|
||||
|
||||
* `event` [MouseInputEvent](structures/mouse-input-event.md) | [MouseWheelInputEvent](structures/mouse-wheel-input-event.md) | [KeyboardInputEvent](structures/keyboard-input-event.md)
|
||||
@@ -907,6 +922,7 @@ webview.addEventListener('close', () => {
|
||||
|
||||
Returns:
|
||||
|
||||
* `frameId` [number, number] - pair of `[processId, frameId]`.
|
||||
* `channel` String
|
||||
* `args` any[]
|
||||
|
||||
|
||||
@@ -100,8 +100,12 @@ const createGuest = function (embedder: Electron.WebContents, params: Record<str
|
||||
});
|
||||
|
||||
// Dispatch guest's IPC messages to embedder.
|
||||
guest.on('ipc-message-host' as any, function (_: Electron.Event, channel: string, args: any[]) {
|
||||
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE, channel, ...args);
|
||||
guest.on('ipc-message-host' as any, function (event: Electron.IpcMainEvent, channel: string, args: any[]) {
|
||||
sendToEmbedder(IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE, {
|
||||
frameId: [event.processId, event.frameId],
|
||||
channel,
|
||||
args
|
||||
});
|
||||
});
|
||||
|
||||
// Notify guest of embedder window visibility when it is ready
|
||||
|
||||
@@ -65,6 +65,7 @@ export const asyncMethods = new Set([
|
||||
'insertText',
|
||||
'removeInsertedCSS',
|
||||
'send',
|
||||
'sendToFrame',
|
||||
'sendInputEvent',
|
||||
'setLayoutZoomLevelLimits',
|
||||
'setVisualZoomLevelLimits',
|
||||
|
||||
@@ -37,8 +37,8 @@ export function registerEvents (viewInstanceId: number, delegate: GuestViewDeleg
|
||||
dispatchEvent(delegate, eventName, eventName, ...args);
|
||||
});
|
||||
|
||||
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`, function (event, channel, ...args) {
|
||||
delegate.dispatchEvent('ipc-message', { channel, args });
|
||||
ipcRendererInternal.on(`${IPC_MESSAGES.GUEST_VIEW_INTERNAL_IPC_MESSAGE}-${viewInstanceId}`, function (event, data) {
|
||||
delegate.dispatchEvent('ipc-message', data);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -267,6 +267,24 @@ describe('<webview> tag', function () {
|
||||
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 () => {
|
||||
const message = await startLoadingWebViewAndWaitForMessage(webview, {
|
||||
preload: `${fixtures}/module/preload.js`,
|
||||
@@ -529,8 +547,9 @@ describe('<webview> tag', function () {
|
||||
webpreferences: 'contextIsolation=no',
|
||||
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(args).to.deep.equal(['arg1', 'arg2']);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user