Add did-attach-webview event

This commit is contained in:
Yuya Ochiai 2017-10-07 01:31:41 +09:00
parent 61e606bedc
commit 6326c6727e
4 changed files with 43 additions and 1 deletions

View file

@ -581,6 +581,16 @@ that can't be set via `<webview>` attributes.
**Note:** The specified `preload` script option will be appear as `preloadURL`
(not `preload`) in the `webPreferences` object emitted with this event.
#### Event: 'did-attach-webview'
Returns:
* `event` Event
* `webContents` WebContents - The guest web contents that is used by the
`<webview>`.
Emitted when a `<webview>` has been attached to this web contents.
### Instance Methods
#### `contents.loadURL(url[, options])`

View file

@ -76,7 +76,7 @@ const createGuest = function (embedder, params) {
watchEmbedder(embedder)
// Init guest web view after attached.
guest.on('did-attach', function () {
guest.on('did-attach', function (event) {
params = this.attachParams
delete this.attachParams
@ -114,6 +114,7 @@ const createGuest = function (embedder, params) {
this.loadURL(params.src, opts)
}
guest.allowPopups = params.allowpopups
embedder.emit('did-attach-webview', event, guest)
})
const sendToEmbedder = (channel, ...args) => {

View file

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<webview src="./a.html"/>
<script>
var {ipcRenderer} = require('electron')
var wv = document.querySelector('webview')
wv.addEventListener('dom-ready', () => {
ipcRenderer.send('webview-dom-ready', wv.getWebContents().id)
})
</script>
</body>
</html>

View file

@ -1183,6 +1183,21 @@ describe('<webview> tag', function () {
})
})
describe('did-attach-webview event', () => {
it('is emitted when a webview has been attached', (done) => {
w = new BrowserWindow({
show: false
})
w.webContents.on('did-attach-webview', (event, webContents) => {
ipcMain.once('webview-dom-ready', (event, id) => {
assert.equal(webContents.id, id)
done()
})
})
w.loadURL('file://' + fixtures + '/pages/webview-did-attach-event.html')
})
})
it('loads devtools extensions registered on the parent window', function (done) {
w = new BrowserWindow({
show: false