Merge pull request #11225 from electron/get-current-browser-view
feature: BrowserView.getAllViews() & BrowserView.fromWebContents()
This commit is contained in:
commit
3c607394d7
4 changed files with 43 additions and 0 deletions
|
@ -154,6 +154,8 @@ void Initialize(v8::Local<v8::Object> exports,
|
|||
isolate, BrowserView::GetConstructor(isolate)->GetFunction());
|
||||
browser_view.SetMethod("fromId",
|
||||
&mate::TrackableObject<BrowserView>::FromWeakMapID);
|
||||
browser_view.SetMethod("getAllViews",
|
||||
&mate::TrackableObject<BrowserView>::GetAll);
|
||||
mate::Dictionary dict(isolate, exports);
|
||||
dict.Set("BrowserView", browser_view);
|
||||
}
|
||||
|
|
|
@ -40,6 +40,17 @@ view.webContents.loadURL('https://electronjs.org')
|
|||
|
||||
### Static Methods
|
||||
|
||||
#### `BrowserView.getAllViews()`
|
||||
|
||||
Returns `BrowserView[]` - An array of all opened BrowserViews.
|
||||
|
||||
#### `BrowserView.fromWebContents(webContents)`
|
||||
|
||||
* `webContents` [WebContents](web-contents.md)
|
||||
|
||||
Returns `BrowserView | null` - The BrowserView that owns the given `webContents`
|
||||
or `null` if the contents are not owned by a BrowserView.
|
||||
|
||||
#### `BrowserView.fromId(id)`
|
||||
|
||||
* `id` Integer
|
||||
|
|
|
@ -5,4 +5,12 @@ const {BrowserView} = process.atomBinding('browser_view')
|
|||
|
||||
Object.setPrototypeOf(BrowserView.prototype, EventEmitter.prototype)
|
||||
|
||||
BrowserView.fromWebContents = (webContents) => {
|
||||
for (const view of BrowserView.getAllViews()) {
|
||||
if (view.webContents.equal(webContents)) return view
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
module.exports = BrowserView
|
||||
|
|
|
@ -125,4 +125,26 @@ describe('BrowserView module', () => {
|
|||
assert.equal(view2.webContents.id, view.webContents.id)
|
||||
})
|
||||
})
|
||||
|
||||
describe('BrowserView.fromWebContents()', () => {
|
||||
it('returns the view with given id', () => {
|
||||
view = new BrowserView()
|
||||
w.setBrowserView(view)
|
||||
assert.notEqual(view.id, null)
|
||||
let view2 = BrowserView.fromWebContents(view.webContents)
|
||||
assert.equal(view2.webContents.id, view.webContents.id)
|
||||
})
|
||||
})
|
||||
|
||||
describe('BrowserView.getAllViews()', () => {
|
||||
it('returns all views', () => {
|
||||
view = new BrowserView()
|
||||
w.setBrowserView(view)
|
||||
assert.notEqual(view.id, null)
|
||||
|
||||
const views = BrowserView.getAllViews()
|
||||
assert.equal(views.length, 1)
|
||||
assert.equal(views[0].webContents.id, view.webContents.id)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue