Skip to main content

webFrameMain

Control web pages and iframes.控制网页和iframe。

Process:进程:Main

The webFrameMain module can be used to lookup frames across existing WebContents instances. webFrameMain模块可用于在现有WebContents实例中查找帧。Navigation events are the common use case.导航事件是常见的用例。

const { BrowserWindow, webFrameMain } = require('electron')

const win = new BrowserWindow({ width: 800, height: 1500 })
win.loadURL('https://twitter.com')

win.webContents.on(
'did-frame-navigate',
(event, url, httpResponseCode, httpStatusText, isMainFrame, frameProcessId, frameRoutingId) => {
const frame = webFrameMain.fromId(frameProcessId, frameRoutingId)
if (frame) {
const code = 'document.body.innerHTML = document.body.innerHTML.replaceAll("heck", "h*ck")'
frame.executeJavaScript(code)
}
}
)

You can also access frames of existing pages by using the mainFrame property of WebContents.您还可以使用WebContentsmainFrame属性访问现有页面的框架。

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模块访问这些方法:

webFrameMain.fromId(processId, routingId)

  • processId Integer - An Integer representing the internal ID of the process which owns the frame.一个Integer,表示拥有该框架的进程的内部ID。
  • routingId Integer - An Integer representing the unique frame ID in the current renderer process. 一个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).路由ID可以从WebFrameMain实例(frame.routingId)中检索,也可以由帧特定的WebContents导航事件传递(例如,帧导航)。

Returns返回WebFrameMain | undefined - A frame with the given process and routing IDs, or undefined if there is no WebFrameMain associated with the given IDs.具有给定进程和路由ID的帧,如果没有与给定ID关联的WebFrameMain,则为undefined

Class: WebFrameMain

Process:进程:Main
This class is not exported from the 'electron' module. 此类不是从'electron'模块导出的。It is only available as a return value of other methods in the Electron API.它只能作为Electron API中其他方法的返回值使用。

Instance Events实例事件

Event: 'dom-ready'

Emitted when the document is loaded.加载文档时发出。

Instance Methods实例方法

frame.executeJavaScript(code[, userGesture])

  • code string
  • userGesture boolean (optional) - Default is false.默认值为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.一种承诺,通过执行代码的结果来解析,或者如果执行抛出或导致被拒绝的承诺,则被拒绝。

Evaluates code in page.评估页面中的code

In the browser window some HTML APIs like requestFullScreen can only be invoked by a gesture from the user. 在浏览器窗口中,一些HTML API(如requestFullScreen)只能通过用户的手势调用。Setting userGesture to true will remove this limitation.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. 通过channel向渲染器进程发送异步消息以及参数。Arguments will be serialized with the Structured Clone Algorithm, just like postMessage, so prototype chains will not be included. 参数将使用结构化克隆算法进行序列化,就像postMessage一样,所以原型链将不包括在内。Sending Functions, Promises, Symbols, WeakMaps, or WeakSets will throw an exception.发送函数、承诺、符号、WeakMaps或WeakSets将引发异常。

The renderer process can handle the message by listening to channel with the ipcRenderer module.渲染器进程可以通过ipcRenderer模块监听channel来处理消息。

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

  • channel string
  • message any
  • transfer MessagePortMain[] (optional)

Send a message to the renderer process, optionally transferring ownership of zero or more [MessagePortMain][] objects.向呈现器进程发送消息,可以选择传输零个或多个[MessagePortMain][]对象的所有权。

The transferred MessagePortMain objects will be available in the renderer process by accessing the ports property of the emitted event. 通过访问发出事件的ports属性,传输的MessagePortMain对象将在渲染器进程中可用。When they arrive in the renderer, they will be native DOM MessagePort objects.当它们到达呈现器时,它们将是本机DOMMessagePort对象。

For example:例如:

// Main process
const { port1, port2 } = new MessageChannelMain()
webContents.mainFrame.postMessage('port', { message: 'hello' }, [port1])

// Renderer process
ipcRenderer.on('port', (e, msg) => {
const [port] = e.ports
// ...
})

Instance Properties实例属性

frame.url Readonly

A string representing the current URL of the frame.表示帧的当前URL的string

frame.top Readonly

A WebFrameMain | null representing top frame in the frame hierarchy to which frame belongs.WebFrameMain|null一个WebFrameMain | null,表示frame所属的框架层次结构中的顶部框架。

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.一个WebFrameMain | null,表示frame的父框架,如果frame是框架层次结构中的顶部框架,则该属性将为null

frame.frames Readonly

A WebFrameMain[] collection containing the direct descendents of frame.WebFrameMain[]集合,包含框架的直接子代。

frame.framesInSubtree Readonly

A WebFrameMain[] collection containing every frame in the subtree of frame, including itself. WebFrameMain[]集合,包含frame子树中的每个框架,包括其本身。This can be useful when traversing through all frames.当遍历所有帧时,这可能很有用。

frame.frameTreeNodeId Readonly

An Integer representing the id of the frame's internal FrameTreeNode instance. 一个Integer,表示帧的内部FrameTreeNode实例的id。This id is browser-global and uniquely identifies a frame that hosts content. 此id是浏览器全局id,唯一标识承载内容的框架。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.当框架被移除时,id将不再使用。

frame.name Readonly

A string representing the frame name.表示帧名称的string

frame.osProcessId Readonly

An Integer representing the operating system pid of the process which owns this frame.一个Integer,表示拥有此帧的进程的操作系统pid

frame.processId Readonly

An Integer representing the Chromium internal pid of the process which owns this frame. 一个Integer,表示拥有此帧的进程的Chromium内部pidThis is not the same as the OS process ID; to read that use frame.osProcessId.这与操作系统进程ID不同;要读取,请使用frame.osProcessId

frame.routingId Readonly

An Integer representing the unique frame id in the current renderer process. 一个Integer,表示当前渲染器进程中唯一的帧id。Distinct WebFrameMain instances that refer to the same underlying frame will have the same routingId.引用相同基础框架的不同WebFrameMain实例将具有相同的routingId

frame.visibilityState Readonly

A string representing the visibility state of the frame.表示框架可见性状态的string

See also how the Page Visibility API is affected by other Electron APIs.另请参阅页面可见性API如何受到其他Electron API的影响。