<webview>标签

1,735 阅读2分钟

使用:

<webview src={MALL_LOGIN_URL}
         disablewebsecurity="true"
         allowpopups="true"
         useragent={USER_AGENT}
         preload={preloadJsFile}/>

标签属性

src

为src指定自己的值将重新加载当前页。
src 属性还可以接受数据 url, 如 data:text/plain, Hellp,world!。

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

nodeintegration

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

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

nodeintegrationinsubframes

是否允许在子页面(iframe)或子窗口(child window)中集成Node.js; 预先加载的脚本会被注入到每一个iframe,你可以用 process.isMainFrame 来判断当前是否处于主框架(main frame)中。

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

enableremotemodule

当此属性为false时,webview中的访客页面将无法访问远程模块。默认情况下,远程模块可用。

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

plugins

当这个属性出现时,webview中的访客页面将能够使用浏览器插件。默认情况下,插件被禁用。

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

preload

在页面运行其他脚本之前预先加载指定的脚本 无论页面是否集成Node, 此脚本都可以访问所有Node API 脚本路径为文件的绝对路径。 当 node integration 关闭时, 预加载的脚本将从全局范围重新引入node的全局引用标志

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

httpreferrer

HTTP引用URL

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

useragent

它在导航到访客页面之前为该页面设置用户代理。加载页面后,使用setUserAgent方法更改用户代理。

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

disablewebsecurity

当此属性存在时,访客页面将禁用web安全性。默认情况下启用Web安全。

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

partition

通过 session 的 partition 字符串来设置界面session. 如果 partition 以 persist:开头, 该页面将使用持续的 session,并在所有页面生效,且使用同一个partition. 如果没有 persist: 前缀, 页面将使用 in-memory session. 通过分配相同的 partition, 多个页可以共享同一会话。 默认使用默认的 session.

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

allowpopups

当此属性存在时,将允许访客页面打开新窗口。默认情况下,弹出窗口处于禁用状态。

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

webpreferences

支持的首选项字符串的完整列表,请查看BrowserWindow

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

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

enableblinkfeatures

以逗号分隔的需要启用的特性列表,譬如CSSVariables,KeyboardEventKey 在 RuntimeEnabledFeatures.json5文件中查看被支持的所有特性.

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

disableblinkfeatures

以 ,分隔的禁用特性列表, 如 CSSVariables,KeyboardEventKey. 在RuntimeEnabledFeatures.json5 文件中查看被支持的所有特性.

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

方法

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

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

更多方法请查看来源链接

来源链接:www.electronjs.org/docs/api/we…

来源链接:www.electronjs.org/docs/api/br…