From cbf6fdc0f5f6bacf7f668e75f0fa481024f9e9f2 Mon Sep 17 00:00:00 2001 From: Alex P Date: Sun, 25 Apr 2021 14:04:53 +0300 Subject: [PATCH] feat: add waitUntil and waitForSelctor to linksBuilder --- src/store/fetch-links.ts | 26 ++++++++++++++++++-------- src/store/model/store.ts | 2 ++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/store/fetch-links.ts b/src/store/fetch-links.ts index 039c3f8536..76fbd2752c 100644 --- a/src/store/fetch-links.ts +++ b/src/store/fetch-links.ts @@ -3,7 +3,7 @@ import {Print, logger} from '../logger'; import {Browser} from 'puppeteer'; import cheerio from 'cheerio'; import {filterSeries} from './filter'; -import {usingResponse} from '../util'; +import {usingPage} from '../util'; function addNewLinks(store: Store, links: Link[], series: Series) { if (links.length === 0) { @@ -28,14 +28,15 @@ function addNewLinks(store: Store, links: Link[], series: Series) { } export async function fetchLinks(store: Store, browser: Browser) { - if (!store.linksBuilder) { + const linksBuilder = store.linksBuilder; + if (!linksBuilder) { return; } const promises: Array> = []; // eslint-disable-next-line prefer-const - for (let {series, url} of store.linksBuilder.urls) { + for (let {series, url} of linksBuilder.urls) { if (!filterSeries(series)) { continue; } @@ -48,16 +49,25 @@ export async function fetchLinks(store: Store, browser: Browser) { url.map(x => promises.push( - usingResponse(browser, x, async response => { - const text = await response?.text(); + usingPage(browser, async page => { + const waitUntil = linksBuilder.waitUntil + ? linksBuilder.waitUntil + : 'domcontentloaded'; + await page.goto(x, {waitUntil}); + + if (linksBuilder.waitForSelector) { + await page.waitForSelector(linksBuilder.waitForSelector); + } + + const html = await page.content(); - if (!text) { + if (!html) { logger.error(Print.message('NO RESPONSE', series, store, true)); return; } - const docElement = cheerio.load(text).root(); - const links = store.linksBuilder!.builder(docElement, series); + const docElement = cheerio.load(html).root(); + const links = linksBuilder.builder(docElement, series); addNewLinks(store, links, series); }) diff --git a/src/store/model/store.ts b/src/store/model/store.ts index da6dfe75ca..a1c21049ad 100644 --- a/src/store/model/store.ts +++ b/src/store/model/store.ts @@ -251,6 +251,8 @@ export type Store = { linksBuilder?: { builder: (docElement: cheerio.Cheerio, series: Series) => Link[]; ttl?: number; + waitUntil?: PuppeteerLifeCycleEvent; + waitForSelector?: string; urls: Array<{series: Series; url: string | string[]}>; }; labels: Labels;