webContents·续3

Returns Promise<​any> - A promise that resolves with the result of the executed code or is rejected if the result of the code is a rejected promise.

在页面中执行 code。

在浏览器窗口中,一些HTML API(如requestFullScreen)只能是 由来自用户的手势调用。 将 userGesture 设置为 true 将删除此限制。

Code execution will be suspended until web page stop loading.

contents.executeJavaScript('fetch("https://jsonplaceholder.typicode.com/users/1").then(resp => resp.json())', true)  .then((result) => {    console.log(result) // Will be the JSON object from the fetch call  })

contents.executeJavaScriptInIsolatedWorld(worldId, scripts[, userGesture])

  • worldId Integer - The ID of the world to run the javascript in, 0 is the default world, 999 is the world used by Electron’s contextIsolation feature. You can provide any integer here.

  • scripts WebSource[]

  • userGesture Boolean (optional) - Default is false.

Returns Promise<​any> - A promise that resolves with the result of the executed code or is rejected if the result of the code is a rejected promise.

Works like executeJavaScript but evaluates scripts in an isolated context.

contents.setIgnoreMenuShortcuts(ignore) 实验功能

  • ignore Boolean

Ignore application menu shortcuts while this web contents is focused.

contents.setAudioMuted(muted)

  • muted Boolean

使当前页面音频静音

过时的

contents.isAudioMuted()

返回 Boolean -判断页面是否被静音

过时的

contents.isCurrentlyAudible()

Returns Boolean - Whether audio is currently playing.

contents.setZoomFactor(factor)

  • factor Double - Zoom factor; default is 1.0.

Changes the zoom factor to the specified factor. Zoom factor is zoom percent divided by 100, so 300% = 3.0.

The factor must be greater than 0.0.

过时的

contents.getZoomFactor()

Returns Number - the current zoom factor.

过时的

contents.setZoomLevel(level)

  • level Number - 缩放等级。

更改缩放等级。 The original size is 0 and each increment above or below represents zooming 20% larger or smaller to default limits of 300% and 50% of original size, respectively. The formula for this is scale := 1.2 ^ level.

过时的

contents.getZoomLevel()

Returns Number - the current zoom level.

过时的

contents.setVisualZoomLevelLimits(minimumLevel, maximumLevel)

  • minimumLevel Number

  • maximumLevel Number

Returns Promise<​void>

设置最大和最小缩放级别。

NOTE: Visual zoom is disabled by default in Electron. To re-enable it, call:

js contents.setVisualZoomLevelLimits(1, 3)

contents.setLayoutZoomLevelLimits(minimumLevel, maximumLevel) Deprecated

  • minimumLevel Number

  • maximumLevel Number

Returns Promise<​void>

设置最大和最小基于布局(例如非图像)的缩放级别。

Deprecated: This API is no longer supported by Chromium.

contents.undo()

在页面中执行undo编辑命令。

contents.redo()

在页面中执行redo编辑命令。

contents.cut()

在页面中执行cut编辑命令。

contents.copy()

在页面中执行copy编辑命令。

contents.copyImageAt(x, y)

  • x Integer

  • y Integer

Copy the image at the given position to the clipboard.

contents.paste()

在页面中执行paste编辑命令。

contents.pasteAndMatchStyle()

在页面中执行pasteAndMatchStyle编辑命令。

contents.delete()

在页面中执行delete编辑命令。

contents.selectAll()

在页面中执行selectAll编辑命令。

contents.unselect()

在页面中执行unselect编辑命令。

contents.replace(text)

  • text String

在页面中执行replace编辑命令。

contents.replaceMisspelling(text)

  • text String

在页面中执行replaceMisspelling编辑命令。

contents.insertText(text)

  • text String

Returns Promise<​void>

插入text 到焦点元素

contents.findInPage(text[, options])

  • text String - 要搜索的内容,必须非空。

  • options Object (optional)

  • forward Boolean (可选) -向前或向后搜索,默认为 true。

  • findNext Boolean (optional) - Whether the operation is first request or a follow up, defaults to false.

  • matchCase Boolean (optional) - Whether search should be case-sensitive, defaults to false.

  • wordStart Boolean (optional) - Whether to look only at the start of words. 默认值为 false.

  • medialCapitalAsWordStart Boolean (optional) - When combined with wordStart, accepts a match in the middle of a word if the match begins with an uppercase letter followed by a lowercase or non-letter. Accepts several other intra-word matches, defaults to false.

Returns Integer - The request id used for the request.

Starts a request to find all matches for the text in the web page. The result of the request can be obtained by subscribing to found-in-page event.

contents.stopFindInPage(action)

  • action String - Specifies the action to take place when ending [webContents.findInPage] request.

  • clearSelection - Clear the selection.

  • keepSelection - Translate the selection into a normal selection.

  • activateSelection - Focus and click the selection node.

Stops any findInPage request for the webContents with the provided action.

const { webContents } = require('electron')webContents.on('found-in-page', (event, result) => {  if (result.finalUpdate) webContents.stopFindInPage('clearSelection')})const requestId = webContents.findInPage('api')console.log(requestId)

contents.capturePage([rect])

  • rect Rectangle (optional) - The area of the page to be captured.

Returns Promise<​NativeImage> - Resolves with a NativeImage

Captures a snapshot of the page within rect. Omitting rect will capture the whole visible page.

contents.isBeingCaptured()

Returns Boolean - Whether this page is being captured. It returns true when the capturer count is large then 0.

contents.incrementCapturerCount([size, stayHidden])

  • size Size (optional) - The perferred size for the capturer.

  • stayHidden Boolean (optional) - Keep the page hidden instead of visible.

Increase the capturer count by one. The page is considered visible when its browser window is hidden and the capturer count is non-zero. If you would like the page to stay hidden, you should ensure that stayHidden is set to true.

This also affects the Page Visibility API.

contents.decrementCapturerCount([stayHidden])

  • stayHidden Boolean (optional) - Keep the page in hidden state instead of visible.

Decrease the capturer count by one. The page will be set to hidden or occluded state when its browser window is hidden or occluded and the capturer count reaches zero. If you want to decrease the hidden capturer count instead you should set stayHidden to true.

contents.getPrinters()

获取系统打印机列表

返回 PrinterInfo[]

contents.print([options], [callback])

  • options Object (optional)

  • silent Boolean (optional) - Don’t ask user for print settings. 默认值为 false.

  • printBackground Boolean (optional) - Prints the background color and image of the web page. 默认值为 false.

  • deviceName String (optional) - Set the printer device name to use. Must be the system-defined name and not the ‘friendly’ name, e.g ‘Brother_QL_820NWB’ and not ‘Brother QL-820NWB’.

  • color Boolean (optional) - Set whether the printed web page will be in color or grayscale. 默认值为 true。

  • margins Object (optional)

  • marginType String (optional) - Can be default, none, printableArea, or custom. If custom is chosen, you will also need to specify top, bottom, left, and right.

  • top Number (optional) - The top margin of the printed web page, in pixels.

  • bottom Number (optional) - The bottom margin of the printed web page, in pixels.

  • left Number (optional) - The left margin of the printed web page, in pixels.

  • right Number (optional) - The right margin of the printed web page, in pixels.

  • landscape Boolean (optional) - Whether the web page should be printed in landscape mode. 默认值为 false.

  • scaleFactor Number (optional) - The scale factor of the web page.

  • pagesPerSheet Number (optional) - The number of pages to print per page sheet.

  • collate Boolean (optional) - Whether the web page should be collated.

  • copies Number (optional) - The number of copies of the web page to print.

  • pageRanges Record (optional) - The page range to print. Should have two keys: from and to.

  • duplexMode String (optional) - Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.

  • dpi Object (optional)