options
Object (可选)
httpReferrer (String | Referrer) (可选) - 一个 HTTP Referrer url。
userAgent String (可选) - 发起请求的 userAgent.
extraHeaders String (可选) - 用 "\n" 分割的额外标题
postData ([UploadRawData] | [UploadFile] | [UploadBlob]) (可选)
baseURLForDataURL String (可选) - 要加载的数据文件的根 url(带有路径分隔符). 只有当指定的 url是一个数据 url 并需要加载其他文件时,才需要这样做。
Returns Promise<void> - The promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load).
webview 中加载目标 url,url 地址必须包含协议前缀,例如:http:// 或 file://。
url String
Initiates a download of the resource at url without navigating.
返回 String - 访客页的URL。
返回 String - 访客页的标题。
返回 Boolean - 访客页是否仍然在加载资源。
Returns Boolean - Whether the main frame (and not just iframes or frames within it) is still loading.
返回 Boolean - 访客页面是否正在等待页面主资源的第一响应。
Stops any pending navigation.
刷新访客页。
刷新访客页并忽略缓存。
返回 Boolean - 访客页能否后退。
返回 Boolean - 访客页能否前进。
offset Integer
返回 Boolean - 访客页能否前进到 offset。
Clears the navigation history.
使访客页后退。
使访客页前进。
index Integer
定位到指定的绝对索引。
offset Integer
定位到相对于“当前入口”的指定的偏移。
Returns Boolean - Whether the renderer process has crashed.
userAgent String
覆盖访客页的用户代理。
返回 String - 访客页的用户代理。
css String
Returns Promise<String> - A promise that resolves with a key for the inserted CSS that can later be used to remove the CSS via <webview>.removeInsertedCSS(key).
Injects CSS into the current web page and returns a unique key for the inserted stylesheet.
key String
Returns Promise<void> - Resolves if the removal was successful.
Removes the inserted CSS from the current web page. The stylesheet is identified by its key, which is returned from <webview>.insertCSS(css).
code String
userGesture Boolean (可选) - 默认为 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.
在页面中执行 code。 如果设置了userGesture,它将在页面中创建用户手势上下文。 像 requestFullScreen 这样的需要用户操作的HTML API可以利用这个选项来实现自动化。
Opens a DevTools window for guest page.
Closes the DevTools window of guest page.
Returns Boolean - Whether guest page has a DevTools window attached.
Returns Boolean - Whether DevTools window of guest page is focused.
x Integer
y Integer
Starts inspecting element at position (x, y) of guest page.
Opens the DevTools for the shared worker context present in the guest page.
Opens the DevTools for the service worker context present in the guest page.
muted Boolean
设置访客页是否静音。
返回 Boolean - 访客页是否被静音。
Returns Boolean - Whether audio is currently playing.
在页面中执行编辑命令 undo。
在页面中执行编辑命令 redo。
在页面中执行编辑命令 cut。
在页面中执行编辑命令 copy。
在页面中执行编辑命令 paste。
在页面中执行编辑命令 pasteAndMatchStyle。
在页面中执行编辑命令 delete。
在页面中执行编辑命令 selectAll。
在页面中执行编辑命令 unselect。
text String
在页面中执行编辑命令 replace。
text String
在页面中执行编辑命令 replaceMisspelling。
text String
Returns Promise<void>
插入text 到焦点元素
text String - 要搜索的内容,必须非空。
options
Object (可选)
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.
action
String - Specifies the action to take place when ending
<webview>.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 webview with the provided action.
options
Object (可选)
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
Object[] (optional) - The page range to print.
from Number - Index of the first page to print (0-based).
to Number - Index of the last page to print (inclusive) (0-based).
duplexMode String (optional) - Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.
Record<string, number> (optional)
horizontal Number (optional) - The horizontal dpi.
vertical Number (optional) - The vertical dpi.
header String (optional) - String to be printed as page header.
footer String (optional) - String to be printed as page footer.
pageSize String | Size (optional) - Specify page size of the printed document. Can be A3, A4, A5, Legal, Letter, Tabloid or an Object containing height.
Record<string, string> (optional) - the header and footer for the PDF.
title String - The title for the PDF header.
url String - the url for the PDF footer.
landscape Boolean (optional) - true for landscape, false for portrait.
marginsType Integer (optional) - Specifies the type of margins to use. Uses 0 for default margin, 1 for no margin, and 2 for minimum margin. and width in microns.
scaleFactor Number (optional) - The scale factor of the web page. Can range from 0 to 100.
pageRanges
Record<string, number> (optional) - The page range to print.
from Number - Index of the first page to print (0-based).
to Number - Index of the last page to print (inclusive) (0-based).
pageSize String | Size (optional) - Specify page size of the generated PDF. Can be A3, A4, A5, Legal, Letter, Tabloid or an Object containing height
printBackground Boolean (optional) - Whether to print CSS backgrounds.
printSelectionOnly Boolean (optional) - Whether to print selection only.
Returns Promise<Uint8Array> - Resolves with the generated PDF data.
Prints webview's web page as PDF, Same as webContents.printToPDF(options).
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.
channel String
...args any[]
Returns Promise<void>
通过channel向渲染器进程发送异步消息,可以发送任意参数。 The renderer process can handle the message by listening to the channel event with the ipcRenderer module.
示例请进传送门: webContents.send
event MouseInputEvent | MouseWheelInputEvent | KeyboardInputEvent
Returns Promise<void>
Sends an input event to the page.
See webContents.sendInputEvent for detailed description of event object.
factor Number - 缩放比例
Changes the zoom factor to the specified factor. Zoom factor is zoom percent divided by 100, so 300% = 3.0.
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.
Returns Number - the current zoom factor.
Returns Number - the current zoom level.
minimumLevel Number
maximumLevel Number
Returns Promise<void>
设置最大和最小缩放级别。
Shows pop-up dictionary that searches the selected word on the page.
Returns Number - The WebContents ID of this webview.
webview 标签具有以下有效的 DOM 事件:
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.
导航完成时触发,即选项卡的旋转器将停止旋转,并指派onload事件后。(译者:网页加载的过程中,并不是所有的浏览器都是转圈圈,而且浏览器版本不同,加载效果也有可能不一样。其中 ie 和 chrome 是转圈圈,safari 是进度条,firefox是一个小点左右来回移动--20180103)
errorCode Integer
errorDescription String
validatedURL String
isMainFrame Boolean
这个事件类似于 did-finish-load, 不过是在加载失败或取消后触发,例如调用了 window.stop()
isMainFrame Boolean
Fired when a frame has done navigation.
Corresponds to the points in time when the spinner of the tab starts spinning.
Corresponds to the points in time when the spinner of the tab stops spinning.
Fired when document in the given frame is loaded.
title String
explicitSet Boolean
Fired when page title is set during navigation. explicitSet is false when title is synthesized from file url.
favicons String[] - 由连接组成的数组。
Fired when page receives favicon urls.
Fired when page enters fullscreen triggered by HTML API.
Fired when page leaves fullscreen triggered by HTML API.
level Integer - The log level, from 0 to 3. In order it matches verbose, info, warning and error.
message String - The actual console message
line Integer - The line number of the source that triggered this console message
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)
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)
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 = (new URL(e.url)).protocol
if (protocol === 'http:' || protocol === 'https:') {
await shell.openExternal(e.url)
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.
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.
isMainFrame Boolean
url String
当发生页内导航时,触发该事件。
当发生页内导航时,虽然页面地址发生变化,但它并没有导航到其它页面。 例如,点击锚点链接,或者DOM的 hashchange事件被触发时,都会触发该事件。
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'
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')
Fired when the renderer process is crashed.
name 字符串
version String
Fired when a plugin process is crashed.
Fired when the WebContents is destroyed.
多媒体开始播放时,触发该事件。
当媒体文件暂停或播放完成的时候触发
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'>
url String
当鼠标滑到,或者键盘切换到a连接时,触发该事件。
当开发者工具被打开时,触发该事件。
当开发者工具被关闭时,触发该事件。
当开发者工具被选中/打开时,触发该事件。