fix: don't error on navigating to a download link (#328)

This commit is contained in:
Simon Knott
2025-05-07 12:47:45 +02:00
committed by GitHub
parent 950d0d1d34
commit c2255246a3
2 changed files with 38 additions and 1 deletions

View File

@@ -66,7 +66,26 @@ export class Tab {
}
async navigate(url: string) {
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
const downloadEvent = this.page.waitForEvent('download').catch(() => {});
try {
await this.page.goto(url, { waitUntil: 'domcontentloaded' });
} catch (_e: unknown) {
const e = _e as Error;
const mightBeDownload =
e.message.includes('net::ERR_ABORTED') // chromium
|| e.message.includes('Download is starting'); // firefox + webkit
if (!mightBeDownload)
throw e;
// on chromium, the download event is fired *after* page.goto rejects, so we wait a lil bit
const download = await Promise.race([
downloadEvent,
new Promise(resolve => setTimeout(resolve, 500)),
]);
if (!download)
throw e;
}
// Cap load event to 5 seconds, the page is operational at this point.
await this.page.waitForLoadState('load', { timeout: 5000 }).catch(() => {});
}