How can I add dynamically JS string function into `preNavigationHooks`?
I would like to dynamically add a string (which describes a JS function) to
preNavigationHooks
array in CheerioCrawlerOptions [1]
If I do crawlerOptions.preNavigationHooks.push(jsFunction);
, when I run crawler, I got error:
WARN CheerioCrawler: Reclaiming failed request back to the list or queue. TypeError: hook is not a function at CheerioCrawler._executeHooks (D:\Developpement\NodeJS\Nowis_Scraper\node_modules@crawlee\basic\internals\basic-crawler.js:834:23) at CheerioCrawler._handleNavigation (D:\Developpement\NodeJS\Nowis_Scraper\node_modules@crawlee\http\internals\http-crawler.js:326:20) at CheerioCrawler._runRequestHandler (D:\Developpement\NodeJS\Nowis_Scraper\node_modules@crawlee\http\internals\http-crawler.js:286:24)[1] https://crawlee.dev/api/cheerio-crawler/interface/CheerioCrawlerOptions#preNavigationHooks
3 Replies
fair-rose•3y ago
you can try
push(eval(jsFunction))
broad-brownOP•3y ago
Thanks for your suggestion. But
eval
can be harmful.
I was inspired by _runHookWithEnhancedContext
[1] to make my own following JS function.
And do this:
[1] https://github.com/apify/apify-sdk-js/blob/master/packages/actor-scraper/cheerio-scraper/src/internals/crawler_setup.ts#L236GitHub
apify-sdk-js/crawler_setup.ts at master · apify/apify-sdk-js
Apify SDK monorepo. Contribute to apify/apify-sdk-js development by creating an account on GitHub.
absent-sapphire•3y ago
I guess in this case it would be better to simply extend the Crawler class