webContents

webContents

渲染以及控制 web 页面

进程:主进程

webContents is an EventEmitter. 负责渲染和控制网页, 是 BrowserWindow 对象的一个属性。 这是一个访问 webContents 对象的例子:

const { BrowserWindow } = require('electron')let win = new BrowserWindow({ width: 800, height: 1500 })win.loadURL('http://github.com')let contents = win.webContentsconsole.log(contents)

方法

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

const { webContents } = require('electron')console.log(webContents)

webContents.getAllWebContents()

返回 WebContents[] - 所有 WebContents 实例的数组。 包含所有Windows,webviews,opened devtools 和 devtools 扩展背景页的 web 内容

webContents.getFocusedWebContents()

Returns WebContents - 此 app 中焦点的 web 内容,否则返回 null。

webContents.fromId(id)

  • id Integer

Returns WebContents - 给定 id 的 WebContents 实例。

类: WebContents

渲染和控制 BrowserWindow 实例的内容。

进程:主进程

实例事件

Event: ‘did-finish-load’

导航完成时触发,即选项卡的旋转器将停止旋转,并指派onload事件后。

Event: ‘did-fail-load’

返回:

  • event Event

  • errorCode Integer

  • errorDescription String

  • validatedURL String

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

This event is like did-finish-load but emitted when the load failed. 完整的错误码列表以及含义,请看这

Event: ‘did-fail-provisional-load’

返回:

  • event Event

  • errorCode Integer

  • errorDescription String

  • validatedURL String

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

This event is like did-fail-load but emitted when the load was cancelled (e.g. window.stop() was invoked).

Event: ‘did-frame-finish-load’

返回:

  • event Event

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

当框架完成导航(navigation)时触发

Event: ‘did-start-loading’

当tab中的旋转指针(spinner)开始旋转时,就会触发该事件。

Event: ‘did-stop-loading’

当tab中的旋转指针(spinner)结束旋转时,就会触发该事件。

事件: ‘dom-ready’

返回:

  • event Event

一个框架中的文本加载完成后触发该事件。

事件: ‘page-title-updated’

返回:

  • event Event

  • title String

  • explicitSet Boolean

Fired when page title is set during navigation. explicitSet is false when title is synthesized from file url.

事件: ‘page-favicon-updated’

返回:

  • event Event

  • favicons String[] - 由连接组成的数组。

当页面获取到favicon的连接时,触发该事件。

Event: ‘new-window’

返回:

  • event Event

  • url String

  • frameName String

  • disposition String - 可以被设置为 default, foreground-tab, background-tab, new-window, save-to-disk 及 other.

  • options BrowserWindowConstructorOptions - The options which will be used for creating the new BrowserWindow.

  • additionalFeatures String[] - 非标准功能(非标准功能是指这些功能不是由Chromium或Electron处理的功能),这些功能默认指向window.open().

  • referrer Referrer - The referrer that will be passed to the new window. May or may not result in the Referer header being sent, depending on the referrer policy.

Emitted when the page requests to open a new window for a url. It could be requested by window.open or an external link like <​a target='_blank'>.

默认情况下, 将为 url 创建新的 BrowserWindow。

调用event.preventDefault()事件,可以阻止Electron自动创建新的BrowserWindow实例。 调用event.preventDefault() 事件后,你还可以手动创建新的BrowserWindow实例,不过接下来你必须用event.newGuest方法来引用BrowserWindow实例,如果你不这样做,则可能会产生异常。 例如:

myBrowserWindow.webContents.on('new-window', (event, url, frameName, disposition, options) => {  event.preventDefault()  const win = new BrowserWindow({    webContents: options.webContents, // use existing webContents if provided    show: false  })  win.once('ready-to-show', () => win.show())  if (!options.webContents) {    win.loadURL(url) // existing webContents will be navigated automatically  }  event.newGuest = win})

Event: ‘will-navigate’

返回:

  • event Event

  • url String

Emitted when a user or the page wants to start navigation. It can happen when the window.location object is changed or a user clicks a link in the page.

This event will not emit when the navigation is started programmatically with APIs like webContents.loadURL and webContents.back.

It is also not emitted for in-page navigations, such as clicking anchor links or updating the window.location.hash. Use did-navigate-in-page event for this purpose.

调用event.preventDefault()将阻止导航。

Event: ‘did-start-navigation’

返回:

  • event Event

  • url String

  • isInPlace Boolean

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

Emitted when any frame (including main) starts navigating. isInplace will be true for in-page navigations.

Event: ‘will-redirect’

返回:

  • event Event

  • url String

  • isInPlace Boolean

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

Emitted as a server side redirect occurs during navigation. For example a 302 redirect.

This event will be emitted after did-start-navigation and always before the did-redirect-navigation event for the same navigation.

Calling event.preventDefault() will prevent the navigation (not just the redirect).

Event: ‘did-redirect-navigation’

返回:

  • event Event

  • url String

  • isInPlace Boolean

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

Emitted after a server side redirect occurs during navigation. For example a 302 redirect.

This event can not be prevented, if you want to prevent redirects you should checkout out the will-redirect event above.

Event: ‘did-navigate’

返回:

  • event Event

  • url String

  • httpResponseCode Integer - -1 for non HTTP navigations

  • httpStatusText String - empty for non HTTP navigations

Emitted when a main frame navigation is done.

This event is not emitted for in-page navigations, such as clicking anchor links or updating the window.location.hash. Use did-navigate-in-page event for this purpose.

Event: ‘did-frame-navigate’

返回:

  • event Event

  • url String

  • httpResponseCode Integer - -1 for non HTTP navigations

  • httpStatusText String - empty for non HTTP navigations,

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

Emitted when any frame navigation is done.

This event is not emitted for in-page navigations, such as clicking anchor links or updating the window.location.hash. Use did-navigate-in-page event for this purpose.

Event: ‘did-navigate-in-page’

返回:

  • event Event

  • url String

  • isMainFrame Boolean

  • frameProcessId Integer

  • frameRoutingId Integer

Emitted when an in-page navigation happened in any frame.

当发生页内导航时,虽然页面地址发生变化,但它并没有导航到其它页面。 例如,点击锚点链接,或者DOM的 hashchange事件被触发时,都会触发该事件。

Event: ‘will-prevent-unload’

返回:

  • event Event

Emitted when a beforeunload event handler is attempting to cancel a page unload.

Calling event.preventDefault() will ignore the beforeunload event handler and allow the page to be unloaded.

const { BrowserWindow, dialog } = require('electron')const win = new BrowserWindow({ width: 800, height: 600 })win.webContents.on('will-prevent-unload', (event) => {  const choice = dialog.showMessageBox(win, {    type: 'question',    buttons: ['Leave', 'Stay'],    title: 'Do you want to leave this site?',    message: 'Changes you made may not be saved.',    defaultId: 0,    cancelId: 1  })  const leave = (choice === 0)  if (leave) {    event.preventDefault()  }})

Event: ‘crashed’

返回:

  • event Event

  • killed Boolean

当渲染进程崩溃或被结束时触发

事件: ‘unresponsive’

网页变得未响应时触发

事件: ‘responsive’

未响应的页面变成响应时触发

Event: ‘plugin-crashed’

返回:

  • event Event

  • name 字符串

  • version String

当有插件进程崩溃时触发

Event: ‘destroyed’

当webContents被销毁时,触发该事件。

Event: ‘before-input-event’

返回:

  • event Event

  • input Object - Input properties.

  • type String - 可以是 keyUp ,或者 keyDown.

  • key String - 等同于 KeyboardEvent.key.

  • code String - 等同于 KeyboardEvent. code .

  • isAutoRepeat String - 等同于 KeyboardEvent. repeat .

  • shift String - 等同于 KeyboardEvent.shiftKey .

  • control String - 等同于 KeyboardEvent. controlKey .

  • alt String - 等同于 KeyboardEvent. altKey .

  • meta String - 等同于 KeyboardEvent. metaKey .

Emitted before dispatching the keydown and keyup events in the page. Calling event.preventDefault will prevent the page keydown/keyup events and the menu shortcuts.