webFrameMain

控制页面和内联框架(iframes)。

进程:主进程

webFramework 模块可以用来查找现有的 WebContents 实例。 通常在导航事件中使用。

  1. const { BrowserWindow, webFrameMain } = require('electron')
  2. const win = new BrowserWindow({ width: 800, height: 1500 })
  3. win.loadURL('https://twitter.com')
  4. win.webContents.on(
  5. 'did-frame-navigate',
  6. (event, url, isMainFrame, frameProcessId, frameRoutingId) => {
  7. const frame = webFrameMain.fromId(frameProcessId, frameRoutingId)
  8. if (frame) {
  9. const code = 'document.body.innerHTML = document.body.innerHTML.replaceAll("heck", "h*ck")'
  10. frame.executeJavaScript(code)
  11. }
  12. }
  13. )

您还可以通过使用 WebContentsmainFrame 属性 访问现有页面的框架。

  1. const { BrowserWindow } = require('electron')
  2. async function main () {
  3. const win = new BrowserWindow({ width: 800, height: 600 })
  4. await win.loadURL('https://reddit.com')
  5. const youtubeEmbeds = win.webContents.mainFrame.frames.filter((frame) => {
  6. try {
  7. const url = new URL(frame.url)
  8. return url.host === 'www.youtube.com'
  9. } catch {
  10. return false
  11. }
  12. })
  13. console.log(youtubeEmbeds)
  14. }
  15. main()

方法

通过webFrameMain模块可以访问以下方法:

webFrameMain.fromId(processId, routingId)

  • processId Integer - 表示拥有此框架的进程的内部 ID。
  • routingId Integer - 表示当前渲染器进程中唯一框架的 ID 。 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).

Returns WebFrameMain | undefined - A frame with the given process and routing IDs, or undefined if there is no WebFrameMain associated with the given IDs.

Class: WebFrameMain

进程:主进程

实例方法

frame.executeJavaScript(code[, userGesture])

  • code String
  • userGesture Boolean (optional) - Default is false.

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.

在页面中执行 code

在浏览器窗口中,一些HTML API(如requestFullScreen)只能是 由来自用户的手势调用。 将 userGesture 设置为 true 将删除此限制。

frame.reload()

Returns boolean - Whether the reload was initiated successfully. Only results in false when the frame has no history.

frame.send(channel, ...args)

  • channel String
  • ...args any[]

Send an asynchronous message to the renderer process via channel, along with 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.

The renderer process can handle the message by listening to channel with the ipcRenderer module.

frame.postMessage(channel, message, [transfer])

  • channel String
  • message any
  • transfer MessagePortMain

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.

例如:

  1. // Main process
  2. const { port1, port2 } = new MessageChannelMain()
  3. webContents.mainFrame.postMessage('port', { message: 'hello' }, [port1])
  4. // Renderer process
  5. ipcRenderer.on('port', (e, msg) => {
  6. const [port] = e.ports
  7. // ...
  8. })

实例属性

frame.url Readonly

A string representing the current URL of the frame.

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.

frame.name Readonly

A String representing the frame name.

frame.osProcessId Readonly

An Integer representing the operating system pid of the process which owns this frame.

frame.processId Readonly

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.

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.

frame.visibilityState 只读

A string representing the visibility state of the frame.

See also how the Page Visibility API is affected by other Electron APIs.