webContents
Render and control web pages.
Процесс: Главный
webContents
является EventEmitter'ом. Он ответственен за рендер и управление веб-страницы и является свойством объекта BrowserWindow
. Пример доступа к объекту webContents
:
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ width: 800, height: 1500 })
win.loadURL('https://github.com')
const contents = win.webContents
console.log(contents)
Navigation Events
Several events can be used to monitor navigations as they occur within a webContents
.
Document Navigations
When a webContents
navigates to another page (as opposed to an in-page navigation), the following events will be fired.
did-start-navigation
will-frame-navigate
will-navigate
(only fired when main frame navigates)will-redirect
(only fired when a redirect happens during navigation)did-redirect-navigation
(only fired when a redirect happens during navigation)did-frame-navigate
did-navigate
(only fired when main frame navigates)
Subsequent events will not fire if event.preventDefault()
is called on any of the cancellable events.
In-page Navigation
In-page navigations don't cause the page to reload, but instead navigate to a location within the current page. These events are not cancellable. For an in-page navigations, the following events will fire in this order:
Frame Navigation
The will-navigate
and did-navigate
events only fire when the mainFrame navigates. If you want to also observe navigations in <iframe>
s, use will-frame-navigate
and did-frame-navigate
events.
Методы
Эти методы доступны из модуля webContents
:
const { webContents } = require('electron')
console.log(webContents)
webContents.getAllWebContents()
Возвращает WebContents
- массив всех экземпляров WebContents
. Этот массив содержит веб-контент всех окон, webviews, открытых инструментов разработчика и расширений инструментов разработчика на фоновых страницах.
webContents.getFocusedWebContents()
Returns WebContents | null
- The web contents that is focused in this application, otherwise returns null
.
webContents.fromId(id)
id
Integer
Returns WebContents | undefined
- A WebContents instance with the given ID, or undefined
if there is no WebContents associated with the given ID.
webContents.fromFrame(frame)
frame
WebFrameMain
Returns WebContents | undefined
- A WebContents instance with the given WebFrameMain, or undefined
if there is no WebContents associated with the given WebFrameMain.
webContents.fromDevToolsTargetId(targetId)
targetId
string - The Chrome DevTools Protocol TargetID associated with the WebContents instance.
Returns WebContents | undefined
- A WebContents instance with the given TargetID, or undefined
if there is no WebContents associated with the given TargetID.
When communicating with the Chrome DevTools Protocol, it can be useful to lookup a WebContents instance based on its assigned TargetID.
async function lookupTargetId (browserWindow) {
const wc = browserWindow.webContents
await wc.debugger.attach('1.3')
const { targetInfo } = await wc.debugger.sendCommand('Target.getTargetInfo')
const { targetId } = targetInfo
const targetWebContents = await wc.fromDevToolsTargetId(targetId)
}