@crawlee/playwright 4.0.0-beta.325 → 4.0.0-beta.327

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,8 @@
1
1
  import type { BrowserHook, LoadedRequest, RouterHandler, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
2
- import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
2
+ import type { BasicCrawlerOptions, CrawlingContext, EnqueueLinksOptions, ExtractLinksOptions, GetUserDataFromRequest, RouterRoutes } from '@crawlee/basic';
3
3
  import { BasicCrawler } from '@crawlee/basic';
4
4
  import { CrawlingRequest } from '@crawlee/browser';
5
- import type { StorageTransactionView } from '@crawlee/core';
5
+ import type { AddRequestsBatchedResult, StorageTransactionView } from '@crawlee/core';
6
6
  import type { Dictionary, Awaitable } from '@crawlee/types';
7
7
  import { type Cheerio, type CheerioAPI } from 'cheerio';
8
8
  import type { AnyNode } from 'domhandler';
@@ -82,7 +82,18 @@ export interface AdaptivePlaywrightCrawlerContext<UserData extends Dictionary =
82
82
  * ```
83
83
  */
84
84
  parseWithCheerio(selector?: string, timeoutMs?: number): Promise<CheerioAPI>;
85
- enqueueLinks(options?: EnqueueLinksOptions): Promise<unknown>;
85
+ /**
86
+ * Extracts URLs from the current page, without adding them to the request queue.
87
+ * In a browser, it first waits for an element matching the selector, with a 5s timeout.
88
+ */
89
+ extractLinks(options?: ExtractLinksOptions): Promise<string[]>;
90
+ /**
91
+ * Extracts URLs from the current page and adds them to the request queue.
92
+ *
93
+ * By default, the requests are buffered until the crawler settles on this attempt's result, so the returned
94
+ * `requestId` of a newly added request is provisional, and nothing is added when the attempt is discarded.
95
+ */
96
+ enqueueLinks(options?: EnqueueLinksOptions): Promise<AddRequestsBatchedResult>;
86
97
  }
87
98
  interface AdaptiveHookContext extends Pick<AdaptivePlaywrightCrawlerContext, 'id' | 'session' | 'proxyInfo' | 'log' | 'extendTimeout'> {
88
99
  page?: Page;
@@ -319,20 +319,22 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
319
319
  get parseWithCheerio() {
320
320
  throw new Error(errorMessage('parseWithCheerio'));
321
321
  },
322
+ get extractLinks() {
323
+ throw new Error(errorMessage('extractLinks'));
324
+ },
322
325
  get enqueueLinks() {
323
326
  throw new Error(errorMessage('enqueueLinks'));
324
327
  },
325
328
  }));
326
329
  }
327
330
  async #adaptCheerioContext(cheerioContext) {
331
+ const extractLinks = async (options = {}) => extractUrlsFromCheerio(cheerioContext.$, options.selector, options.baseUrl ?? cheerioContext.request.loadedUrl);
328
332
  return {
329
333
  get page() {
330
334
  throw new Error('Page object was used in HTTP-only request handler');
331
335
  },
332
- enqueueLinks: async (options = {}) => {
333
- const urls = extractUrlsFromCheerio(cheerioContext.$, options.selector, options.baseUrl ?? cheerioContext.request.loadedUrl);
334
- return (await this.#enqueueLinks(urls, options, cheerioContext.request));
335
- },
336
+ extractLinks,
337
+ enqueueLinks: async (options = {}) => this.#enqueueLinks(await extractLinks(options), options, cheerioContext.request),
336
338
  // `skipNavigation` keeps the inner throwing getter. The body is already consumed (and decoded to UTF-8).
337
339
  ...(cheerioContext.request.skipNavigation
338
340
  ? {}
@@ -365,20 +367,21 @@ export class AdaptivePlaywrightCrawler extends BasicCrawler {
365
367
  url: originalResponse.url(),
366
368
  });
367
369
  }
370
+ const extractLinks = async (options = {}, timeoutMs = 5000) => {
371
+ // TODO consider using `context.parseWithCheerio` to make this universal and avoid code duplication
372
+ const selector = options.selector ?? 'a';
373
+ const locator = playwrightContext.page.locator(selector).first();
374
+ // Wait for client-rendered links, but like the HTTP side, return nothing when there are none.
375
+ await locator.waitFor({ timeout: timeoutMs, state: 'attached' }).catch((error) => {
376
+ if (error.name !== 'TimeoutError')
377
+ throw error;
378
+ });
379
+ return extractUrlsFromPage(playwrightContext.page, selector, options.baseUrl ?? playwrightContext.request.loadedUrl);
380
+ };
368
381
  return {
369
382
  ...(response && { response }),
370
- enqueueLinks: async (options = {}, timeoutMs = 5000) => {
371
- // TODO consider using `context.parseWithCheerio` to make this universal and avoid code duplication
372
- const selector = options.selector ?? 'a';
373
- const locator = playwrightContext.page.locator(selector).first();
374
- // Wait for client-rendered links, but like the HTTP side, enqueue nothing when there are none.
375
- await locator.waitFor({ timeout: timeoutMs, state: 'attached' }).catch((error) => {
376
- if (error.name !== 'TimeoutError')
377
- throw error;
378
- });
379
- const urls = await extractUrlsFromPage(playwrightContext.page, selector, options.baseUrl ?? playwrightContext.request.loadedUrl);
380
- return (await this.#enqueueLinks(urls, options, playwrightContext.request));
381
- },
383
+ extractLinks,
384
+ enqueueLinks: async (options = {}, timeoutMs = 5000) => this.#enqueueLinks(await extractLinks(options, timeoutMs), options, playwrightContext.request),
382
385
  };
383
386
  }
384
387
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/playwright",
3
- "version": "4.0.0-beta.325",
3
+ "version": "4.0.0-beta.327",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
6
  "node": ">=22.13.0"
@@ -49,14 +49,14 @@
49
49
  "dependencies": {
50
50
  "@apify/datastructures": "^3.0.1",
51
51
  "@apify/timeout": "^1.1.0",
52
- "@crawlee/basic": "4.0.0-beta.325",
53
- "@crawlee/browser": "4.0.0-beta.325",
54
- "@crawlee/browser-pool": "4.0.0-beta.325",
55
- "@crawlee/cheerio": "4.0.0-beta.325",
56
- "@crawlee/core": "4.0.0-beta.325",
57
- "@crawlee/http-client": "4.0.0-beta.325",
58
- "@crawlee/types": "4.0.0-beta.325",
59
- "@crawlee/utils": "4.0.0-beta.325",
52
+ "@crawlee/basic": "4.0.0-beta.327",
53
+ "@crawlee/browser": "4.0.0-beta.327",
54
+ "@crawlee/browser-pool": "4.0.0-beta.327",
55
+ "@crawlee/cheerio": "4.0.0-beta.327",
56
+ "@crawlee/core": "4.0.0-beta.327",
57
+ "@crawlee/http-client": "4.0.0-beta.327",
58
+ "@crawlee/types": "4.0.0-beta.327",
59
+ "@crawlee/utils": "4.0.0-beta.327",
60
60
  "cheerio": "^1.2.0",
61
61
  "domhandler": "^6.0.1",
62
62
  "jquery": "^3.8.0",
@@ -81,5 +81,5 @@
81
81
  }
82
82
  }
83
83
  },
84
- "gitHead": "72ed70d29fe1a4122d4b7384c14ee4828229345f"
84
+ "gitHead": "a7dea40b5857e79e5107ee134cf624f0733d425f"
85
85
  }