webContents·续1

To only prevent the menu shortcuts, use setIgnoreMenuShortcuts:

const { BrowserWindow } = require('electron')let win = new BrowserWindow({ width: 800, height: 600 })win.webContents.on('before-input-event', (event, input) => {  // For example, only enable application menu keyboard shortcuts when  // Ctrl/Cmd are down.  win.webContents.setIgnoreMenuShortcuts(!input.control && !input.meta)})

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

窗口进入由HTML API 触发的全屏状态时触发

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

窗口离开由HTML API触发的全屏状态时触发

Event: ‘zoom-changed’

返回:

  • event Event

  • zoomDirection String - Can be in or out.

Emitted when the user is requesting to change the zoom level using the mouse wheel.

Event: ‘devtools-opened’

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

Event: ‘devtools-closed’

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

Event: ‘devtools-focused’

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

事件: ‘certificate-error’

返回:

  • event Event

  • url String

  • error String - 错误码.

  • certificate 证书

  • callback Function

  • isTrusted Boolean - 用于显示证书是否可信。

证书的链接验证失败时,触发该事件。

使用方式与app的certificate-error的事件相同。

事件: ‘sel​ect-client-certificate’

返回:

  • event Event

  • url URL

  • certificateList 证书[]

  • callback Function

  • certificate Certificate - Must be a certificate from the given list.

当一个客户证书被请求的时候发出。

使用方式与app的select-client-certificate的事件相同。

事件: “login”

返回:

  • event Event

  • authenticationResponseDetails Object

  • url URL

  • authInfo Object

  • isProxy Boolean

  • scheme String

  • host String

  • port Integer

  • realm String

  • callback Function

  • username String (optional)

  • password String (optional)

当 webContents 要进行基本身份验证时触发。

使用方式与app的login的事件相同。

Event: ‘found-in-page’

返回:

  • event Event

  • result Object

  • requestId Integer

  • activeMatchOrdinal Integer - 当前匹配位置。

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

  • selectionArea Rectangle - Coordinates of first match region.

  • finalUpdate Boolean

如果调用[webContents.findInPage]有返回时,会触发这一事件。

Event: ‘media-started-playing’

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

Event: ‘media-paused’

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

Event: ‘did-change-theme-color’

返回:

  • event Event

  • color (String | null) - Theme color is in format of ‘#rrggbb’. It is null when no theme color is set.

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’

返回:

  • event Event

  • url String

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

Event: ‘cursor-changed’

返回:

  • event Event

  • type String

  • image NativeImage (可选)

  • scale Float (optional) - scaling factor for the custom cursor.

  • size Size (可选) - image大小。

  • hotspot Point (optional) - coordinates of the custom cursor’s hotspot.

当鼠标指针改变的时候触发。 Type参数值包含:default, crosshair, pointer, text, wait, help, e-resize, n-resize, ne-resize, nw-resize, s-resize, se-resize, sw-resize, w-resize, ns-resize, ew-resize, nesw-resize, nwse-resize, col-resize, row-resize, m-panning, e-panning, n-panning, ne-panning, nw-panning, s-panning, se-panning, sw-panning, w-panning, move, vertical-text, cell, context-menu, alias, progress, nodrop, copy, none, not-allowed, zoom-in, zoom-out, grab, grabbing 或 custom.

If the type parameter is custom, the image parameter will hold the custom cursor image in a NativeImage, and scale, size and hotspot will hold additional information about the custom cursor.

Event: ‘context-menu’

返回:

  • event Event

  • params Object

  • x Integer - x 坐标。

  • y Integer - y 坐标。

  • linkURL String - URL of the link that encloses the node the context menu was invoked on.

  • linkText String - Text associated with the link. May be an empty string if the contents of the link are an image.

  • pageURL String - URL of the top level page that the context menu was invoked on.

  • frameURL String - URL of the subframe that the context menu was invoked on.

  • srcURL String - Source URL for the element that the context menu was invoked on. Elements with source URLs are images, audio and video.

  • mediaType String - Type of the node the context menu was invoked on. Can be none, image, audio, video, canvas, file or plugin.

  • hasImageContents Boolean - Whether the context menu was invoked on an image which has non-empty contents.

  • isEditable Boolean - Whether the context is editable.

  • selectionText String - Text of the selection that the context menu was invoked on.

  • titleText String - Title or alt text of the selection that the context was invoked on.

  • misspelledWord String - The misspelled word under the cursor, if any.

  • dictionarySuggestions String[] - An array of suggested words to show the user to replace the misspelledWord. Only available if there is a misspelled word and spellchecker is enabled.

  • frameCharset String - The character encoding of the frame on which the menu was invoked.

  • inputFieldType String - If the context menu was invoked on an input field, the type of that field. Possible values are none, plainText, password, other.

  • menuSourceType String - Input source that invoked the context menu. Can be none, mouse, keyboard, touch or touchMenu.

  • mediaFlags Object - The flags for the media element the context menu was invoked on.

  • inError Boolean - Whether the media element has crashed.

  • isPaused Boolean - Whether the media element is paused.

  • isMuted Boolean - Whether the media element is muted.

  • hasAudio Boolean - Whether the media element has audio.

  • isLooping Boolean - Whether the media element is looping.

  • isControlsVisible Boolean - Whether the media element’s controls are visible.

  • canToggleControls Boolean - Whether the media element’s controls are toggleable.

  • canRotate Boolean - Whether the media element can be rotated.

  • editFlags Object - These flags indicate whether the renderer believes it is able to perform the corresponding action.

  • canUndo Boolean - Whether the renderer believes it can undo.

  • canRedo Boolean - Whether the renderer believes it can redo.

  • canCut Boolean - Whether the renderer believes it can cut.

  • canCopy Boolean - Whether the renderer believes it can copy

  • canPaste Boolean - Whether the renderer believes it can paste.

  • canDelete Boolean - Whether the renderer believes it can delete.

  • canSelectAll Boolean - Whether the renderer believes it can sel​ect all.

Emitted when there is a new context menu that needs to be handled.

事件: ‘sel​ect-bluetooth-device’

返回:

  • event Event

  • devices BluetoothDevice[]

  • callback Function

  • deviceId String 设备Id

Emitted when bluetooth device needs to be selected on call to navigator.bluetooth.requestDevice. To use navigator.bluetooth api webBluetooth should be enabled. If event.preventDefault is not called, first available device will be selected. callback should be called with deviceId to be selected, passing empty string to callback will cancel the request.

const { app, BrowserWindow } = require('electron')let win = nullapp.commandLine.appendSwitch('enable-experimental-web-platform-features')app.on('ready', () => {  win = new BrowserWindow({ width: 800, height: 600 })  win.webContents.on('sel​ect-bluetooth-device', (event, deviceList, callback) => {    event.preventDefault()    let result = deviceList.find((device) => {      return device.deviceName === 'test'    })    if (!result) {      callback('')    } else {      callback(result.deviceId)    }  })})

Event: ‘paint’

返回:

  • event Event

  • dirtyRect Rectangle

  • image NativeImage - The image data of the whole frame.

Emitted when a new frame is generated. Only the dirty area is passed in the buffer.

const { BrowserWindow } = require('electron')let win = new BrowserWindow({ webPreferences: { offscreen: true } })win.webContents.on('paint', (event, dirty, image) => {  // updateBitmap(dirty, image.getBitmap())})win.loadURL('http://github.com')

Event: ‘devtools-reload-page’

当在开发者工具中命令webContents重新加载时,触发该事件。

Event: ‘will-attach-webview’

返回:

  • event Event