webFrameMain
控制页面和内联框架(iframes)。
进程:主进程
webFramework
模块可以用来查找现有的 WebContents
实例。 通常在导航事件中使用。
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)
}
}
)
您还可以通过使用 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()
方法
通过webFrameMain
模块可以访问以下方法:
webFrameMain.fromId(processId, routingId)
processId
Integer -一个Integer
表示拥有此框架的进程的内部 ID。routingId
Integer - 一个Integer
表示当前渲染器进程中唯一框架的 ID 。 Routing IDs 可以从WebFrameMain
instances (frame.routingId
) 获取到,也可以从 frame 指定的WebContents
的导航事件 (例如did-frame-navigate
) 传入。
返回 WebFrameMain | undefined
- 一个带有指定进程和 routing IDs 的 frame,如果指定的 IDs 没有关联的 WebFrameMain 则为 undefined
。
Class: WebFrameMain
Process: Main
此类不从 'electron'
模块导出. 它只能作为 Electron API 中其他方法的返回值。