fix: BrowserWindow add the same BrowserView (#48201)

fix: BrowserWindow add the same BrowserView (#48057)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Zonglong Liu <83216456+mai-121@users.noreply.github.com>
This commit is contained in:
trop[bot] 2025-09-02 16:56:04 -04:00 committed by GitHub
commit 79433861fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View file

@ -199,7 +199,14 @@ BrowserWindow.prototype.setBackgroundThrottling = function (allowed: boolean) {
};
BrowserWindow.prototype.addBrowserView = function (browserView: BrowserView) {
if (browserView.ownerWindow) { browserView.ownerWindow.removeBrowserView(browserView); }
if (this._browserViews.includes(browserView)) {
return;
}
const ownerWindow = browserView.ownerWindow;
if (ownerWindow && ownerWindow !== this) {
ownerWindow.removeBrowserView(browserView);
}
this.contentView.addChildView(browserView.webContentsView);
browserView.ownerWindow = this;
browserView.webContents._setOwnerWindow(this);

View file

@ -471,6 +471,39 @@ describe('BrowserView module', () => {
w.webContents.loadURL('about:blank');
await once(w.webContents, 'did-finish-load');
});
it('document visibilitychange does not change when adding the same BrowserView multiple times', async () => {
w.show();
expect(w.isVisible()).to.be.true('w is visible');
const view = new BrowserView();
const [width, height] = w.getSize();
view.setBounds({ x: 0, y: 0, width, height });
w.addBrowserView(view);
expect(view.ownerWindow).to.equal(w);
await view.webContents.loadURL(`data:text/html,
<html>
<body>
<h1>HELLO BROWSERVIEW</h1>
<script>
document.visibilityChangeCount = 0;
document.addEventListener('visibilitychange', () => {
document.visibilityChangeCount++;
})
</script>
</body>
</html>
`);
const query = 'document.visibilityChangeCount';
const countBefore = await view.webContents.executeJavaScript(query);
expect(countBefore).to.equal(0);
w.addBrowserView(view);
w.addBrowserView(view);
const countAfter = await view.webContents.executeJavaScript(query);
expect(countAfter).to.equal(countBefore);
});
});
describe('BrowserWindow.removeBrowserView()', () => {