fix: promise resolved to early when browser initiated in-page navigation v2 (#39597)

This commit is contained in:
Tomasz 2023-08-28 18:37:28 +02:00 committed by GitHub
parent f0ad357af2
commit f30fbebb98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 2 deletions

View file

@ -454,6 +454,7 @@ WebContents.prototype.loadURL = function (url, options) {
};
let navigationStarted = false;
let browserInitiatedInPageNavigation = false;
const navigationListener = (event: Electron.Event, url: string, isSameDocument: boolean, isMainFrame: boolean) => {
if (isMainFrame) {
if (navigationStarted && !isSameDocument) {
@ -468,6 +469,7 @@ WebContents.prototype.loadURL = function (url, options) {
// as the routing does not leave the document
return rejectAndCleanup(-3, 'ERR_ABORTED', url);
}
browserInitiatedInPageNavigation = navigationStarted && isSameDocument;
navigationStarted = true;
}
};
@ -482,17 +484,22 @@ WebContents.prototype.loadURL = function (url, options) {
// would be more appropriate.
rejectAndCleanup(-2, 'ERR_FAILED', url);
};
const finishListenerWhenUserInitiatedNavigation = () => {
if (!browserInitiatedInPageNavigation) {
finishListener();
}
};
const removeListeners = () => {
this.removeListener('did-finish-load', finishListener);
this.removeListener('did-fail-load', failListener);
this.removeListener('did-navigate-in-page', finishListener);
this.removeListener('did-navigate-in-page', finishListenerWhenUserInitiatedNavigation);
this.removeListener('did-start-navigation', navigationListener);
this.removeListener('did-stop-loading', stopLoadingListener);
this.removeListener('destroyed', stopLoadingListener);
};
this.on('did-finish-load', finishListener);
this.on('did-fail-load', failListener);
this.on('did-navigate-in-page', finishListener);
this.on('did-navigate-in-page', finishListenerWhenUserInitiatedNavigation);
this.on('did-start-navigation', navigationListener);
this.on('did-stop-loading', stopLoadingListener);
this.on('destroyed', stopLoadingListener);