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 您还可以使用WebContents的mainFrame
property of WebContents.mainFrame
属性访问现有页面的框架。
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路由ID可以从WebFrameMain
instances (frame.routingId
) and are also passed by frame specificWebContents
navigation events (e.g.did-frame-navigate
).WebFrameMain
实例(frame.routingId
)中检索,也可以由帧特定的WebContents
导航事件传递(例如,帧导航)。
Returns返回WebFrameMain | undefined
- A frame with the given process and routing IDs, or 具有给定进程和路由ID的帧,如果没有与给定ID关联的undefined
if there is no WebFrameMain associated with the given IDs.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
stringuserGesture
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 在浏览器窗口中,一些HTML API(如requestFullScreen
can only be invoked by a gesture from the user. 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 渲染器进程可以通过ipcRenderer模块监听channel
with the ipcRenderer module.channel
来处理消息。
frame.postMessage(channel, message, [transfer])
channel
stringmessage
anytransfer
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 表示帧的当前URL的string
representing the current URL of the frame.string
。
frame.top
Readonly
A WebFrameMain|WebFrameMain | null
representing top frame in the frame hierarchy to which frame
belongs.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内部pid
。This is not the same as the OS process ID; to read that use 这与操作系统进程ID不同;要读取,请使用frame.osProcessId
.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的影响。