| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | # webFrameMain
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | > Control web pages and iframes.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Process: [Main](../glossary.md#main-process) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The `webFrameMain` module can be used to lookup frames across existing | 
					
						
							|  |  |  | [`WebContents`](web-contents.md) instances. Navigation events are the common | 
					
						
							|  |  |  | use case. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:50:08 -08:00
										 |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | const { BrowserWindow, webFrameMain } = require('electron') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const win = new BrowserWindow({ width: 800, height: 1500 }) | 
					
						
							|  |  |  | win.loadURL('https://twitter.com') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | win.webContents.on( | 
					
						
							|  |  |  |   'did-frame-navigate', | 
					
						
							| 
									
										
										
										
											2022-06-05 22:49:14 -07:00
										 |  |  |   (event, url, httpResponseCode, httpStatusText, isMainFrame, frameProcessId, frameRoutingId) => { | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  |     const frame = webFrameMain.fromId(frameProcessId, frameRoutingId) | 
					
						
							|  |  |  |     if (frame) { | 
					
						
							|  |  |  |       const code = 'document.body.innerHTML = document.body.innerHTML.replaceAll("heck", "h*ck")' | 
					
						
							|  |  |  |       frame.executeJavaScript(code) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 00:34:06 +01:00
										 |  |  | You can also access frames of existing pages by using the `mainFrame` property | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | of [`WebContents`](web-contents.md). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-20 23:50:08 -08:00
										 |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | const { BrowserWindow } = require('electron') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | async function main () { | 
					
						
							|  |  |  |   const win = new BrowserWindow({ width: 800, height: 600 }) | 
					
						
							|  |  |  |   await win.loadURL('https://reddit.com') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const youtubeEmbeds = win.webContents.mainFrame.frames.filter((frame) => { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       const url = new URL(frame.url) | 
					
						
							|  |  |  |       return url.host === 'www.youtube.com' | 
					
						
							|  |  |  |     } catch { | 
					
						
							|  |  |  |       return false | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   console.log(youtubeEmbeds) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main() | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## Methods
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | These methods can be accessed from the `webFrameMain` module: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### `webFrameMain.fromId(processId, routingId)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 00:34:06 +01:00
										 |  |  | * `processId` Integer - An `Integer` representing the internal ID of the process which owns the frame. | 
					
						
							|  |  |  | * `routingId` Integer - An `Integer` representing the unique frame ID in the | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  |   current renderer process. Routing IDs can be retrieved from `WebFrameMain` | 
					
						
							|  |  |  |   instances (`frame.routingId`) and are also passed by frame | 
					
						
							|  |  |  |   specific `WebContents` navigation events (e.g. `did-frame-navigate`). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 03:31:21 +01:00
										 |  |  | Returns `WebFrameMain | undefined` - A frame with the given process and routing IDs, | 
					
						
							|  |  |  | or `undefined` if there is no WebFrameMain associated with the given IDs. | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Class: WebFrameMain
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-15 13:50:31 -07:00
										 |  |  | Process: [Main](../glossary.md#main-process)<br /> | 
					
						
							|  |  |  | _This class is not exported from the `'electron'` module. It is only available as a return value of other methods in the Electron API._ | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-01 18:21:15 -04:00
										 |  |  | ### Instance Events
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### Event: 'dom-ready'
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Emitted when the document is loaded. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | ### Instance Methods
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.executeJavaScript(code[, userGesture])`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 05:13:18 +01:00
										 |  |  | * `code` string | 
					
						
							|  |  |  | * `userGesture` boolean (optional) - Default is `false`. | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | Returns `Promise<unknown>` - A promise that resolves with the result of the executed | 
					
						
							|  |  |  | code or is rejected if execution throws or results in a rejected promise. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Evaluates `code` in page. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In the browser window some HTML APIs like `requestFullScreen` can only be | 
					
						
							|  |  |  | invoked by a gesture from the user. Setting `userGesture` to `true` will remove | 
					
						
							|  |  |  | this limitation. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.reload()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns `boolean` - Whether the reload was initiated successfully. Only results in `false` when the frame has no history. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-11 18:33:53 -04:00
										 |  |  | #### `frame.isDestroyed()`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns `boolean` - Whether the frame is destroyed. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 01:00:37 +01:00
										 |  |  | #### `frame.send(channel, ...args)`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 05:13:18 +01:00
										 |  |  | * `channel` string | 
					
						
							| 
									
										
										
										
											2021-01-15 01:00:37 +01:00
										 |  |  | * `...args` any[] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Send an asynchronous message to the renderer process via `channel`, along with | 
					
						
							| 
									
										
										
										
											2024-05-28 11:15:18 -07:00
										 |  |  | arguments. Arguments will be serialized with the [Structured Clone Algorithm][SCA], | 
					
						
							|  |  |  | just like [`postMessage`][], so prototype chains will not be included. | 
					
						
							|  |  |  | Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception. | 
					
						
							| 
									
										
										
										
											2021-01-15 01:00:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The renderer process can handle the message by listening to `channel` with the | 
					
						
							|  |  |  | [`ipcRenderer`](ipc-renderer.md) module. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.postMessage(channel, message, [transfer])`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 05:13:18 +01:00
										 |  |  | * `channel` string | 
					
						
							| 
									
										
										
										
											2021-01-15 01:00:37 +01:00
										 |  |  | * `message` any | 
					
						
							|  |  |  | * `transfer` MessagePortMain[] (optional) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Send a message to the renderer process, optionally transferring ownership of | 
					
						
							|  |  |  | zero or more [`MessagePortMain`][] objects. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The transferred `MessagePortMain` objects will be available in the renderer | 
					
						
							|  |  |  | process by accessing the `ports` property of the emitted event. When they | 
					
						
							|  |  |  | arrive in the renderer, they will be native DOM `MessagePort` objects. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | For example: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | // Main process | 
					
						
							| 
									
										
										
										
											2023-06-05 15:26:26 +08:00
										 |  |  | const win = new BrowserWindow() | 
					
						
							| 
									
										
										
										
											2021-01-15 01:00:37 +01:00
										 |  |  | const { port1, port2 } = new MessageChannelMain() | 
					
						
							| 
									
										
										
										
											2023-06-05 15:26:26 +08:00
										 |  |  | win.webContents.mainFrame.postMessage('port', { message: 'hello' }, [port1]) | 
					
						
							| 
									
										
										
										
											2021-01-15 01:00:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Renderer process | 
					
						
							|  |  |  | ipcRenderer.on('port', (e, msg) => { | 
					
						
							|  |  |  |   const [port] = e.ports | 
					
						
							|  |  |  |   // ... | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-02 23:32:24 -05:00
										 |  |  | #### `frame.collectJavaScriptCallStack()` _Experimental_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Returns `Promise<string> | Promise<void>` - A promise that resolves with the currently running JavaScript call | 
					
						
							|  |  |  | stack. If no JavaScript runs in the frame, the promise will never resolve. In cases where the call stack is | 
					
						
							|  |  |  | otherwise unable to be collected, it will return `undefined`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This can be useful to determine why the frame is unresponsive in cases where there's long-running JavaScript. | 
					
						
							|  |  |  | For more information, see the [proposed Crash Reporting API.](https://wicg.github.io/crash-reporting/) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | const { app } = require('electron') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.commandLine.appendSwitch('enable-features', 'DocumentPolicyIncludeJSCallStacksInCrashReports') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | app.on('web-contents-created', (_, webContents) => { | 
					
						
							|  |  |  |   webContents.on('unresponsive', async () => { | 
					
						
							|  |  |  |     // Interrupt execution and collect call stack from unresponsive renderer | 
					
						
							|  |  |  |     const callStack = await webContents.mainFrame.collectJavaScriptCallStack() | 
					
						
							|  |  |  |     console.log('Renderer unresponsive\n', callStack) | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | ### Instance Properties
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-03 16:55:12 -07:00
										 |  |  | #### `frame.ipc` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An [`IpcMain`](ipc-main.md) instance scoped to the frame. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IPC messages sent with `ipcRenderer.send`, `ipcRenderer.sendSync` or | 
					
						
							|  |  |  | `ipcRenderer.postMessage` will be delivered in the following order: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 1. `contents.on('ipc-message')` | 
					
						
							|  |  |  | 2. `contents.mainFrame.on(channel)` | 
					
						
							|  |  |  | 3. `contents.ipc.on(channel)` | 
					
						
							|  |  |  | 4. `ipcMain.on(channel)` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Handlers registered with `invoke` will be checked in the following order. The | 
					
						
							|  |  |  | first one that is defined will be called, the rest will be ignored. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 1. `contents.mainFrame.handle(channel)` | 
					
						
							|  |  |  | 2. `contents.handle(channel)` | 
					
						
							|  |  |  | 3. `ipcMain.handle(channel)` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In most cases, only the main frame of a WebContents can send or receive IPC | 
					
						
							|  |  |  | messages. However, if the `nodeIntegrationInSubFrames` option is enabled, it is | 
					
						
							|  |  |  | possible for child frames to send and receive IPC messages also. The | 
					
						
							|  |  |  | [`WebContents.ipc`](web-contents.md#contentsipc-readonly) interface may be more | 
					
						
							|  |  |  | convenient when `nodeIntegrationInSubFrames` is not enabled. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | #### `frame.url` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `string` representing the current URL of the frame. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-31 17:40:57 -07:00
										 |  |  | #### `frame.origin` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `string` representing the current origin of the frame, serialized according | 
					
						
							|  |  |  | to [RFC 6454](https://www.rfc-editor.org/rfc/rfc6454). This may be different | 
					
						
							|  |  |  | from the URL. For instance, if the frame is a child window opened to | 
					
						
							|  |  |  | `about:blank`, then `frame.origin` will return the parent frame's origin, while | 
					
						
							|  |  |  | `frame.url` will return the empty string. Pages without a scheme/host/port | 
					
						
							|  |  |  | triple origin will have the serialized origin of `"null"` (that is, the string | 
					
						
							|  |  |  | containing the letters n, u, l, l). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | #### `frame.top` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `WebFrameMain | null` representing top frame in the frame hierarchy to which `frame` | 
					
						
							|  |  |  | belongs. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.parent` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `WebFrameMain | null` representing parent frame of `frame`, the property would be | 
					
						
							|  |  |  | `null` if `frame` is the top frame in the frame hierarchy. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.frames` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `WebFrameMain[]` collection containing the direct descendents of `frame`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.framesInSubtree` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `WebFrameMain[]` collection containing every frame in the subtree of `frame`, | 
					
						
							|  |  |  | including itself. This can be useful when traversing through all frames. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #### `frame.frameTreeNodeId` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An `Integer` representing the id of the frame's internal FrameTreeNode | 
					
						
							|  |  |  | instance. This id is browser-global and uniquely identifies a frame that hosts | 
					
						
							|  |  |  | content. The identifier is fixed at the creation of the frame and stays | 
					
						
							|  |  |  | constant for the lifetime of the frame. When the frame is removed, the id is | 
					
						
							|  |  |  | not used again. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-26 04:03:34 +01:00
										 |  |  | #### `frame.name` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-16 05:13:18 +01:00
										 |  |  | A `string` representing the frame name. | 
					
						
							| 
									
										
										
										
											2020-10-26 04:03:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-14 13:42:37 -07:00
										 |  |  | #### `frame.frameToken` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `string` which uniquely identifies the frame within its associated renderer | 
					
						
							|  |  |  | process. This is equivalent to [`webFrame.frameToken`](web-frame.md#webframeframetoken-readonly). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-26 04:03:34 +01:00
										 |  |  | #### `frame.osProcessId` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An `Integer` representing the operating system `pid` of the process which owns this frame. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | #### `frame.processId` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-26 04:03:34 +01:00
										 |  |  | An `Integer` representing the Chromium internal `pid` of the process which owns this frame. | 
					
						
							|  |  |  | This is not the same as the OS process ID; to read that use `frame.osProcessId`. | 
					
						
							| 
									
										
										
										
											2020-10-09 12:50:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #### `frame.routingId` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An `Integer` representing the unique frame id in the current renderer process. | 
					
						
							|  |  |  | Distinct `WebFrameMain` instances that refer to the same underlying frame will | 
					
						
							|  |  |  | have the same `routingId`. | 
					
						
							| 
									
										
										
										
											2021-04-22 12:00:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #### `frame.visibilityState` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `string` representing the [visibility state](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState) of the frame. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | See also how the [Page Visibility API](browser-window.md#page-visibility) is affected by other Electron APIs. | 
					
						
							| 
									
										
										
										
											2022-03-24 10:12:47 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-11 18:33:53 -04:00
										 |  |  | #### `frame.detached` _Readonly_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A `Boolean` representing whether the frame is detached from the frame tree. If a frame is accessed | 
					
						
							|  |  |  | while the corresponding page is running any [unload][] listeners, it may become detached as the | 
					
						
							|  |  |  | newly navigated page replaced it in the frame tree. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-24 10:12:47 -07:00
										 |  |  | [SCA]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm | 
					
						
							|  |  |  | [`postMessage`]: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage | 
					
						
							| 
									
										
										
										
											2022-12-05 10:18:57 -08:00
										 |  |  | [`MessagePortMain`]: message-port-main.md | 
					
						
							| 
									
										
										
										
											2024-10-11 18:33:53 -04:00
										 |  |  | [unload]: https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event |