<​webview>标签·续2

<​webview>.getWebContentsId()

Returns Number - The WebContents ID of this webview.

DOM Events

webview 标签具有以下有效的 DOM 事件:

Event: ‘load-commit’

返回:

  • url String

  • isMainFrame Boolean

Fired when a load has committed. This includes navigation within the current document as well as subframe document-level loads, but does not include asynchronous resource loads.

Event: ‘did-finish-load’

导航完成时触发,即选项卡的旋转器将停止旋转,并指派onload事件后。(译者:网页加载的过程中,并不是所有的浏览器都是转圈圈,而且浏览器版本不同,加载效果也有可能不一样。其中 ie 和 chrome 是转圈圈,safari 是进度条,firefox是一个小点左右来回移动—20180103)

Event: ‘did-fail-load’

返回:

  • errorCode Integer

  • errorDescription String

  • validatedURL String

  • isMainFrame Boolean

这个事件类似于 did-finish-load, 不过是在加载失败或取消后触发,例如调用了 window.stop()

Event: ‘did-frame-finish-load’

返回:

  • isMainFrame Boolean

Fired when a frame has done navigation.

Event: ‘did-start-loading’

Corresponds to the points in time when the spinner of the tab starts spinning.

Event: ‘did-stop-loading’

Corresponds to the points in time when the spinner of the tab stops spinning.

事件: ‘dom-ready’

Fired when document in the given frame is loaded.

事件: ‘page-title-updated’

返回:

  • 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’

返回:

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

Fired when page receives favicon urls.

事件: ‘enter-html-full-screen’

Fired when page enters fullscreen triggered by HTML API.

事件: ‘leave-html-full-screen’

Fired when page leaves fullscreen triggered by HTML API.

Event: ‘console-message’

返回:

  • level Integer

  • message String

  • line Integer

  • sourceId String

Fired when the guest window logs a console message.

The following example code forwards all log messages to the embedder’s console without regard for log level or other properties.

const webview = document.querySelector('webview')webview.addEventListener('console-message', (e) => {  console.log('Guest page logged a message:', e.message)})

Event: ‘found-in-page’

返回:

  • result Object

  • requestId Integer

  • activeMatchOrdinal Integer - 当前匹配位置。

  • matches Integer - 符合匹配条件的元素个数。

  • selectionArea Rectangle - Coordinates of first match region.

  • finalUpdate Boolean

Fired when a result is available for webview.findInPage request.

const webview = document.querySelector('webview')webview.addEventListener('found-in-page', (e) => {  webview.stopFindInPage('keepSelection')})const requestId = webview.findInPage('test')console.log(requestId)

Event: ‘new-window’

返回:

  • url String

  • frameName String

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

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

Fired when the guest page attempts to open a new browser window.

The following example code opens the new url in system’s default browser.

const { shell } = require('electron')const webview = document.querySelector('webview')webview.addEventListener('new-window', async (e) => {  const protocol = require('url').parse(e.url).protocol  if (protocol === 'http:' || protocol === 'https:') {    await shell.openExternal(e.url)  }})

Event: ‘will-navigate’

返回:

  • 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 <​webview>.loadURL and <​webview>.back.

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

Calling event.preventDefault() does NOT have any effect.

Event: ‘did-navigate’

返回:

  • url String

Emitted when a 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’

返回:

  • isMainFrame Boolean

  • url String

当发生页内导航时,触发该事件。

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

事件: ‘close’

Fired when the guest page attempts to close itself.

The following example code navigates the webview to about:blank when the guest attempts to close itself.

const webview = document.querySelector('webview')webview.addEventListener('close', () => {  webview.src = 'about:blank'})

Event: ‘ipc-message’

返回:

  • channel String

  • args any[]

Fired when the guest page has sent an asynchronous message to embedder page.

With sendToHost method and ipc-message event you can communicate between guest page and embedder page:

// In embedder page.const webview = document.querySelector('webview')webview.addEventListener('ipc-message', (event) => {  console.log(event.channel)  // Prints "pong"})webview.send('ping')
// In guest page.const { ipcRenderer } = require('electron')ipcRenderer.on('ping', () => {  ipcRenderer.sendToHost('pong')})

Event: ‘crashed’

Fired when the renderer process is crashed.

Event: ‘plugin-crashed’

返回:

  • name 字符串

  • version String

Fired when a plugin process is crashed.

Event: ‘destroyed’

Fired when the WebContents is destroyed.

Event: ‘media-started-playing’

多媒体开始播放时,触发该事件。

Event: ‘media-paused’

当媒体文件暂停或播放完成的时候触发

Event: ‘did-change-theme-color’

返回:

  • themeColor String

Emitted when a page’s theme color changes. This is usually due to encountering a meta tag:

<​meta name='theme-color' content='#ff0000'>

Event: ‘update-target-url’

返回:

  • url String

当鼠标滑到,或者键盘切换到a连接时,触发该事件。

Event: ‘devtools-opened’

当开发者工具被打开时,触发该事件。

Event: ‘devtools-closed’

当开发者工具被关闭时,触发该事件。

Event: ‘devtools-focused’

当开发者工具被选中/打开时,触发该事件。