fix(booklet): Close pages when no goodie

This commit is contained in:
Tibo De Peuter 2025-01-01 19:22:39 +01:00
parent fe523c91fe
commit 9bcba4caaa
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2

View file

@ -21,7 +21,7 @@ export async function lucida(album: URL, baseTimeout: number, context: BrowserCo
const page: Page = await openAlbum(album, context);
// Check 'Hide my download from recently downloaded' checkbox
await page.locator('input[id="hide-from-recent"]').first().setChecked(true);
await page.locator('input[id="hide-from-ticker"]').first().setChecked(true);
// Parse info
const albumName: string = (await page.locator('h1[class="svelte-6pt9ji"]').last().innerText()).trim();
@ -81,13 +81,16 @@ async function booklet(album: URL, context: BrowserContext): Promise<File | null
// Find link with goodies
const link: Locator = page.locator('a').filter({hasText: '/goodies/'});
const linkCount: number = await link.count();
if (linkCount > 0) {
console.log(await link.innerHTML());
} else {
if (0 <= linkCount) {
console.log('No goodies link found');
await page.close();
return null;
}
console.log(`Found goodies: ${await link.innerHTML()}`)
let result: File | null = null;
try {
// Go to the goodies link
await link.dispatchEvent('click');
@ -97,13 +100,13 @@ async function booklet(album: URL, context: BrowserContext): Promise<File | null
const filename: string = download.suggestedFilename();
await download.saveAs('/home/tdpeuter/Downloads/lucida/' + filename);
console.log(`Downloaded booklet ${filename}`);
result = new File([await download.path()], filename);
await page.close();
return new File([await download.path()], filename);
console.log(`Downloaded booklet ${filename}`);
} catch (e) {
console.log('Could not download booklet:', e);
await page.close();
return null;
}
await page.close();
return result;
}