例子
<webview id="foo" src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" style="display:inline-block; width:640px; height:480px"></webview>
<script>
onload = function() {
var webview = document.getElementById("foo");
var indicator = document.querySelector(".indicator");
var loadstart = function() {
indicator.innerText = "loading...";
}
var loadstop = function() {
indicator.innerText = "";
}
webview.addEventListener("did-start-loading", loadstart);
webview.addEventListener("did-stop-loading", loadstop);
}
</script>
标签属性
src
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></webview>
autosize
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" autosize="on" minwidth="576" minheight="432"></webview>
nodeintegration
<webview src="http://www.google.com/" rel="external nofollow" nodeintegration></webview>
plugins
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" plugins></webview>
preload
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" preload="./test.js"></webview>
httpreferrer
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" httpreferrer="http://cheng.guru"></webview>
useragent
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" useragent="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"></webview>
disablewebsecurity
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" disablewebsecurity></webview>
partition
<webview src="https://github.com" rel="external nofollow" partition="persist:github"></webview>
<webview src="http://electron.atom.io" rel="external nofollow" partition="electron"></webview>
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" allowpopups></webview>
blinkfeatures
<webview src="https://www.github.com/" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" blinkfeatures="PreciseMemoryInfo, CSSVariables"></webview>
方法
例如
webview.addEventListener("dom-ready", function() {
webview.openDevTools();
});
<webview>.loadURL(url[, options])
url
URLoptions
Object (可选)httpReferrer
String - 一个http类型的url.userAgent
String -用于发起请求的用户代理.extraHeaders
String - 额外的headers,用 "\n"分隔.
<webview>.getURL()
<webview>.getTitle()
<webview>.isLoading()
<webview>.isWaitingForResponse()
<webview>.stop()
<webview>.reload()
<webview>.reloadIgnoringCache()
<webview>.canGoBack()
<webview>.canGoForward()
<webview>.canGoToOffset(offset)
<webview>.clearHistory()
<webview>.goBack()
<webview>.goForward()
<webview>.goToIndex(index)
<webview>.goToOffset(offset)
<webview>.isCrashed()
<webview>.setUserAgent(userAgent)
<webview>.getUserAgent()
<webview>.insertCSS(css)
<webview>.executeJavaScript(code, userGesture, callback)
code
StringuserGesture
Boolean - 默认 false
.callback
Function (可选) - 回调函数.
<webview>.inspectElement(x, y)
<webview>.inspectServiceWorker()
<webview>.setAudioMuted(muted)
muted
Boolean设置 guest page 流畅(muted).
<webview>.isAudioMuted()
<webview>.undo()
<webview>.redo()
<webview>.cut()
<webview>.copy()
<webview>.paste()
<webview>.pasteAndMatchStyle()
<webview>.delete()
<webview>.selectAll()
<webview>.unselect()
<webview>.replace(text)
<webview>.replaceMisspelling(text)
<webview>.insertText(text)
<webview>.findInPage(text[, options])
text
String - 搜索内容,不能为空.options
Object (可选)forward
Boolean - 向前或向后, 默认为 true
.findNext
Boolean - 是否查找的第一个结果,默认为 false
.matchCase
Boolean - 是否区分大小写,默认为 false
.wordStart
Boolean - 是否只查找首字母.默认为 false
.medialCapitalAsWordStart
Boolean - 当配合 wordStart
的时候,接受一个文字中的匹配项,要求匹配项是以大写字母开头后面跟小写字母或者没有字母。可以接受一些其他单词内部匹配, 默认为 false
.
<webview>.stopFindInPage(action)
action
String - 指定一个行为来接替停止<webview>.findInPage
请求.clearSelection
- 转变为一个普通的 selection.keepSelection
- 清除 selection.activateSelection
- 聚焦并点击 selection node.
<webview>.print([options])
<webview>.printToPDF(options, callback)
<webview>.send(channel[, arg1][, arg2][, ...])
<webview>.getWebContents()
DOM 事件
Event: 'load-commit'
返回:
url
StringisMainFrame
Boolean
Event: 'did-finish-load'
Event: 'did-fail-load'
errorCode
IntegererrorDescription
StringvalidatedURL
String
Event: 'did-frame-finish-load'
返回:
Event: 'did-start-loading'
Event: 'did-stop-loading'
Event: 'did-get-response-details'
返回:
status
BooleannewURL
StringoriginalURL
StringhttpResponseCode
IntegerrequestMethod
Stringreferrer
Stringheaders
Object
Event: 'did-get-redirect-request'
返回:
oldURL
StringnewURL
StringisMainFrame
Boolean
Event: 'dom-ready'
Event: 'page-title-updated'
返回:
title
StringexplicitSet
Boolean
Event: 'page-favicon-updated'
返回:
favicons
Array - Array of URLs.
Event: 'enter-html-full-screen'
Event: 'leave-html-full-screen'
Event: 'console-message'
返回:
level
Integermessage
Stringline
IntegersourceId
String
webview.addEventListener('console-message', function(e) {
console.log('Guest page logged a message:', e.message);
});
Event: 'found-in-page'
返回:
result
ObjectrequestId
IntegerfinalUpdate
Boolean - 指明下面是否还有更多的回应.activeMatchOrdinal
Integer (可选) - 活动匹配位置matches
Integer (optional) - 匹配数量.selectionArea
Object (optional) - 整合第一个匹配域.
webview.addEventListener('found-in-page', function(e) {
if (e.result.finalUpdate)
webview.stopFindInPage("keepSelection");
});
const rquestId = webview.findInPage("test");
Event: 'new-window'
返回:
url
StringframeName
Stringdisposition
String - 可以为 default
, foreground-tab
, background-tab
,new-window
和 other
.options
Object - 参数应该被用作创建新的BrowserWindow
.
webview.addEventListener('new-window', function(e) {
require('electron').shell.openExternal(e.url);
});
Event: 'will-navigate'
返回:
Event: 'did-navigate'
返回:
Event: 'did-navigate-in-page'
返回:
Event: 'close'
webview.addEventListener('close', function() {
webview.src = 'about:blank';
});
Event: 'ipc-message'
返回:
// In embedder page.
webview.addEventListener('ipc-message', function(event) {
console.log(event.channel);
// Prints "pong"
});
webview.send('ping');
// In guest page.
var ipcRenderer = require('electron').ipcRenderer;
ipcRenderer.on('ping', function() {
ipcRenderer.sendToHost('pong');
});
Event: 'crashed'
Event: 'gpu-crashed'
Event: 'plugin-crashed'
返回:
name
Stringversion
String
Event: 'destroyed'
Event: 'did-change-theme-color'
<meta name='theme-color' content='#ff0000'>
在开发者工具获取焦点的时候触发.
请发表评论