<​webview>标签

<​webview>标签

警告

Electron的 webview 标签基于 Chromium webview ,后者正在经历巨大的架构变化。 这将影响 webview 的稳定性,包括呈现、导航和事件路由。 我们目前建议不使用 webview 标签,并考虑其他替代方案,如 iframe 、Electron的 BrowserView 或完全避免嵌入内容的体系结构。

Enabling

By default the webview tag is disabled in Electron >= 5. 在构造 BrowserWindow 时,需要通过设置 webviewTag webPreferences选项来启用标签。 更多信息请参看 BrowserWindows 的构造器文档。

概览

在一个独立的 frame 和进程里显示外部 web 内容。

进程: Renderer

使用 webview 标签在Electron 应用中嵌入 “外来” 内容 (如 网页)。外来”内容包含在 webview 容器中。 应用中的嵌入页面可以控制外来内容的布局和重绘。

与 iframe 不同, webview 在与应用程序不同的进程中运行。它与您的网页没有相同的权限, 应用程序和嵌入内容之间的所有交互都将是异步的。 这将保证你的应用对于嵌入的内容的安全性。 注意: 从宿主页上调用 webview 的方法大多数都需要对主进程进行同步调用。

示例

若要在应用程序中嵌入网页, 请将 webview 标签添加到应用程序的被嵌入页面中 (这是将显示外来内容的应用程序页)。 在最简单的例子中, webview 标签包括网页的 src 和控制 webview 容器外观的 css 样式:

<​webview id="foo" src="https://www.github.com/" style="display:inline-flex; width:640px; height:480px"><​/webview>

如果要以任何方式控制外来内容, 则可以写用于侦听 webview 事件的 JavaScript, 并使用 webview 方法响应这些事件。 下面是包含两个事件侦听器的示例代码: 一个侦听网页开始加载, 另一个用于网页停止加载, 并在加载时显示 “loading…” 消息:

<​script>  onload = () => {    const webview = document.querySelector('webview')    const indicator = document.querySelector('.indicator')    const loadstart = () => {      indicator.innerText = 'loading...'    }    const loadstop = () => {      indicator.innerText = ''    }    webview.addEventListener('did-start-loading', loadstart)    webview.addEventListener('did-stop-loading', loadstop)  }<​/script>

Internal implementation

Under the hood webview is implemented with Out-of-Process iframes (OOPIFs). The webview tag is essentially a custom element using shadow DOM to wrap an iframe element inside it.

So the behavior of webview is very similar to a cross-domain iframe, as examples:

  • When clicking into a webview, the page focus will move from the embedder frame to webview.

  • You can not add keyboard, mouse, and scroll event listeners to webview.

  • All reactions between the embedder frame and webview are asynchronous.

CSS 样式说明

Please note that the webview tag’s style uses display:flex; internally to ensure the child iframe element fills the full height and width of its webview container when used with traditional and flexbox layouts. Please do not overwrite the default display:flex; CSS property, unless specifying display:inline-flex; for inline layout.

标签属性

webview 标签具有以下属性:

src

<​webview src="https://www.github.com/"><​/webview>

A String representing the visible URL. Writing to this attribute initiates top-level navigation.

Assigning src its own value will reload the current page.

src 属性还可以接受数据 url, 如 data:text/plain, Hellp,world!。

nodeintegration

<​webview src="http://www.google.com/" nodeintegration><​/webview>

A Boolean. 当有此属性时, webview 中的访客页(guest page)将具有Node集成, 并且可以使用像 require 和 process 这样的node APIs 去访问低层系统资源。 Node 集成在访客页中默认是禁用的。

nodeintegrationinsubframes

<​webview src="http://www.google.com/" nodeintegrationinsubframes><​/webview>

A Boolean for the experimental option for enabling NodeJS support in sub-frames such as iframes inside the webview. All your preloads will load for every iframe, you can use process.isMainFrame to determine if you are in the main frame or not. This option is disabled by default in the guest page.

enableremotemodule

<​webview src="http://www.google.com/" enableremotemodule="false"><​/webview>

A Boolean. When this attribute is false the guest page in webview will not have access to the remote module. The remote module is available by default.

plugins

<​webview src="https://www.github.com/" plugins><​/webview>

A Boolean. When this attribute is present the guest page in webview will be able to use browser plugins. Plugins are disabled by default.

preload

<​webview src="https://www.github.com/" preload="./test.js"><​/webview>

A String that specifies a script that will be loaded before other scripts run in the guest page. 该脚本的URL的协议必须是 file: asar:二者之一,因为在访客页中,它是通过“内部”的 require 去加载的

当访客页没有 node integration ,这个脚本仍然有能力去访问所有的 Node APIs, 但是当这个脚本执行执行完成之后,通过Node 注入的全局对象(global objects)将会被删除。

Note: This option will appear as preloadURL (not preload) in the webPreferences specified to the will-attach-webview event.

httpreferrer

<​webview src="https://www.github.com/" httpreferrer="http://cheng.guru"><​/webview>

A String that sets the referrer URL for the guest page.

useragent

<​webview src="https://www.github.com/" useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"><​/webview>

A String that sets the user agent for the guest page before the page is navigated to. Once the page is loaded, use the setUserAgent method to change the user agent.

disablewebsecurity

<​webview src="https://www.github.com/" disablewebsecurity><​/webview>

A Boolean. When this attribute is present the guest page will have web security disabled. Web security is enabled by default.

partition

<​webview src="https://github.com" partition="persist:github"><​/webview><​webview src="https://electronjs.org" partition="electron"><​/webview>

A String that sets the session used by the page. 如果 partition 以 persist:开头, 该页面将使用持续的 session,并在所有页面生效,且使用同一个partition. 如果没有 persist: 前缀, 页面将使用 in-memory session. 通过分配相同的 partition, 多个页可以共享同一会话。 如果没有设置partition,app 将会使用默认的session。

This value can only be modified before the first navigation, since the session of an active renderer process cannot change. Subsequent attempts to modify the value will fail with a DOM exception.

allowpopups

<​webview src="https://www.github.com/" allowpopups><​/webview>

A Boolean. When this attribute is present the guest page will be allowed to open new windows. Popups are disabled by default.

webpreferences

<​webview src="https://github.com" webpreferences="allowRunningInsecureContent, javascript=no"><​/webview>

A String which is a comma separated list of strings which specifies the web preferences to be set on the webview. 支持的首选项字符串的完整列表,请查看 BrowserWindow。

该字符串的格式与 window.open 中的功能字符串( the features string )相同。 只有自己名字的将被赋予 true 布尔值。 可以通过 = 来赋予其他值。 yes 和 1 会被解析成 true,而 no 和 0 解析为 false。

enableblinkfeatures

<​webview src="https://www.github.com/" enableblinkfeatures="PreciseMemoryInfo, CSSVariables"><​/webview>

A String which is a list of strings which specifies the blink features to be enabled separated by ,. The full list of supported feature strings can be found in the RuntimeEnabledFeatures.json5 file.

disableblinkfeatures

<​webview src="https://www.github.com/" disableblinkfeatures="PreciseMemoryInfo, CSSVariables"><​/webview>

A String which is a list of strings which specifies the blink features to be disabled separated by ,. The full list of supported feature strings can be found in the RuntimeEnabledFeatures.json5 file.

方法

webview 标签具有以下方法:

注意: 使用方法之前 <0>webview 元素必须已被加载。

示例

const webview = document.querySelector('webview')webview.addEventListener('dom-ready', () => {  webview.openDevTools()})

<​webview>.loadURL(url[, options])

  • url URL

  • options Object (optional)

  • 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://。

<​webview>.downloadURL(url)

  • url String

Initiates a download of the resource at url without navigating.

<​webview>.getURL()

返回 String - 访客页的URL。

<​webview>.getTitle()

返回 String - 访客页的标题。

<​webview>.isLoading()

返回 Boolean - 访客页是否仍然在加载资源。

<​webview>.isLoadingMainFrame()