@crawlee/puppeteer 4.0.0-beta.8 → 4.0.0-beta.80

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.
package/README.md CHANGED
@@ -1,19 +1,23 @@
1
1
  <h1 align="center">
2
2
  <a href="https://crawlee.dev">
3
3
  <picture>
4
- <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true">
5
- <img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500">
4
+ <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true" />
5
+ <img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500" />
6
6
  </picture>
7
7
  </a>
8
- <br>
8
+ <br />
9
9
  <small>A web scraping and browser automation library</small>
10
10
  </h1>
11
11
 
12
- <p align=center>
13
- <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/v/@crawlee/core.svg" alt="NPM latest version" data-canonical-src="https://img.shields.io/npm/v/@crawlee/core/next.svg" style="max-width: 100%;"></a>
14
- <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/dm/@crawlee/core.svg" alt="Downloads" data-canonical-src="https://img.shields.io/npm/dm/@crawlee/core.svg" style="max-width: 100%;"></a>
15
- <a href="https://discord.gg/jyEM2PRvMU" rel="nofollow"><img src="https://img.shields.io/discord/801163717915574323?label=discord" alt="Chat on discord" data-canonical-src="https://img.shields.io/discord/801163717915574323?label=discord" style="max-width: 100%;"></a>
16
- <a href="https://github.com/apify/crawlee/actions/workflows/test-ci.yml"><img src="https://github.com/apify/crawlee/actions/workflows/test-ci.yml/badge.svg?branch=master" alt="Build Status" style="max-width: 100%;"></a>
12
+ <p align="center">
13
+ <a href="https://trendshift.io/repositories/5179" target="_blank"><img src="https://trendshift.io/api/badge/repositories/5179" alt="apify%2Fcrawlee | Trendshift" width="250" height="55"/></a>
14
+ </p>
15
+
16
+ <p align="center">
17
+ <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/v/@crawlee/core.svg" alt="NPM latest version" data-canonical-src="https://img.shields.io/npm/v/@crawlee/core/next.svg" /></a>
18
+ <a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/dm/@crawlee/core.svg" alt="Downloads" data-canonical-src="https://img.shields.io/npm/dm/@crawlee/core.svg" /></a>
19
+ <a href="https://discord.gg/jyEM2PRvMU" rel="nofollow"><img src="https://img.shields.io/discord/801163717915574323?label=discord" alt="Chat on discord" data-canonical-src="https://img.shields.io/discord/801163717915574323?label=discord" /></a>
20
+ <a href="https://github.com/apify/crawlee/actions/workflows/test-ci.yml"><img src="https://github.com/apify/crawlee/actions/workflows/test-ci.yml/badge.svg?branch=master" alt="Build Status" /></a>
17
21
  </p>
18
22
 
19
23
  Crawlee covers your crawling and scraping end-to-end and **helps you build reliable scrapers. Fast.**
@@ -24,7 +28,7 @@ Crawlee is available as the [`crawlee`](https://www.npmjs.com/package/crawlee) N
24
28
 
25
29
  > 👉 **View full documentation, guides and examples on the [Crawlee project website](https://crawlee.dev)** 👈
26
30
 
27
- > Crawlee for Python is open for early adopters. 🐍 [👉 Checkout the source code 👈](https://github.com/apify/crawlee-python).
31
+ > Do you prefer 🐍 Python instead of JavaScript? [👉 Checkout Crawlee for Python 👈](https://github.com/apify/crawlee-python).
28
32
 
29
33
  ## Installation
30
34
 
@@ -85,7 +89,7 @@ By default, Crawlee stores data to `./storage` in the current working directory.
85
89
  We provide automated beta builds for every merged code change in Crawlee. You can find them in the npm [list of releases](https://www.npmjs.com/package/crawlee?activeTab=versions). If you want to test new features or bug fixes before we release them, feel free to install a beta build like this:
86
90
 
87
91
  ```bash
88
- npm install crawlee@3.12.3-beta.13
92
+ npm install crawlee@next
89
93
  ```
90
94
 
91
95
  If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need to specify dependency overrides in your `package.json` file so that you don't end up with multiple versions of Crawlee installed:
@@ -94,9 +98,9 @@ If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need
94
98
  {
95
99
  "overrides": {
96
100
  "apify": {
97
- "@crawlee/core": "3.12.3-beta.13",
98
- "@crawlee/types": "3.12.3-beta.13",
99
- "@crawlee/utils": "3.12.3-beta.13"
101
+ "@crawlee/core": "$crawlee",
102
+ "@crawlee/types": "$crawlee",
103
+ "@crawlee/utils": "$crawlee"
100
104
  }
101
105
  }
102
106
  }
package/index.d.ts CHANGED
@@ -7,4 +7,3 @@ export * as puppeteerUtils from './internals/utils/puppeteer_utils.js';
7
7
  export type { BlockRequestsOptions, CompiledScriptFunction, CompiledScriptParams, DirectNavigationOptions as PuppeteerDirectNavigationOptions, InfiniteScrollOptions, InjectFileOptions, SaveSnapshotOptions, } from './internals/utils/puppeteer_utils.js';
8
8
  export * as puppeteerClickElements from './internals/enqueue-links/click-elements.js';
9
9
  export type { EnqueueLinksByClickingElementsOptions } from './internals/enqueue-links/click-elements.js';
10
- //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -4,4 +4,3 @@ export * from './internals/puppeteer-launcher.js';
4
4
  export * as puppeteerRequestInterception from './internals/utils/puppeteer_request_interception.js';
5
5
  export * as puppeteerUtils from './internals/utils/puppeteer_utils.js';
6
6
  export * as puppeteerClickElements from './internals/enqueue-links/click-elements.js';
7
- //# sourceMappingURL=index.js.map
@@ -1,4 +1,4 @@
1
- import type { GlobInput, PseudoUrlInput, RegExpInput, RequestProvider, RequestTransform } from '@crawlee/browser';
1
+ import type { GlobInput, IRequestManager, PseudoUrlInput, RegExpInput, RequestTransform, SkippedRequestCallback } from '@crawlee/browser';
2
2
  import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
3
3
  // @ts-ignore optional peer dependency or compatibility with es2022
4
4
  import type { ClickOptions, Page, Target } from 'puppeteer';
@@ -8,9 +8,9 @@ export interface EnqueueLinksByClickingElementsOptions {
8
8
  */
9
9
  page: Page;
10
10
  /**
11
- * A request queue to which the URLs will be enqueued.
11
+ * * A request manager to which the URLs will be enqueued.
12
12
  */
13
- requestQueue: RequestProvider;
13
+ requestManager: IRequestManager;
14
14
  /**
15
15
  * A CSS selector matching elements to be clicked on. Unlike in {@link enqueueLinks}, there is no default
16
16
  * value. This is to prevent suboptimal use of this function by using it too broadly.
@@ -39,6 +39,16 @@ export interface EnqueueLinksByClickingElementsOptions {
39
39
  * after clicking on elements matching the provided CSS selector.
40
40
  */
41
41
  globs?: GlobInput[];
42
+ /**
43
+ * An array of glob pattern strings, regexp patterns or plain objects
44
+ * containing patterns matching URLs that will **never** be enqueued.
45
+ *
46
+ * The plain objects must include either the `glob` property or the `regexp` property.
47
+ *
48
+ * Glob matching is always case-insensitive.
49
+ * If you need case-sensitive matching, provide a regexp.
50
+ */
51
+ exclude?: readonly (GlobInput | RegExpInput)[];
42
52
  /**
43
53
  * An array of regular expressions or plain objects
44
54
  * containing regular expressions matching the URLs to be enqueued.
@@ -72,25 +82,28 @@ export interface EnqueueLinksByClickingElementsOptions {
72
82
  */
73
83
  pseudoUrls?: PseudoUrlInput[];
74
84
  /**
75
- * Just before a new {@link Request} is constructed and enqueued to the {@link RequestQueue}, this function can be used
76
- * to remove it or modify its contents such as `userData`, `payload` or, most importantly `uniqueKey`. This is useful
77
- * when you need to enqueue multiple `Requests` to the queue that share the same URL, but differ in methods or payloads,
78
- * or to dynamically update or create `userData`.
79
- *
80
- * For example: by adding `useExtendedUniqueKey: true` to the `request` object, `uniqueKey` will be computed from
81
- * a combination of `url`, `method` and `payload` which enables crawling of websites that navigate using form submits
82
- * (POST requests).
85
+ * After {@link Request} objects are constructed and filtered by URL patterns (`globs`, `regexps`, `pseudoUrls`),
86
+ * this function can be used to remove them or modify their contents such as `userData`, `payload` or, most importantly
87
+ * `uniqueKey`. This is useful when you need to enqueue multiple `Requests` to the queue that share the same URL,
88
+ * but differ in methods or payloads, or to dynamically update or create `userData`.
83
89
  *
84
90
  * **Example:**
85
91
  * ```javascript
86
92
  * {
87
93
  * transformRequestFunction: (request) => {
88
94
  * request.userData.foo = 'bar';
89
- * request.useExtendedUniqueKey = true;
90
95
  * return request;
91
96
  * }
92
97
  * }
93
98
  * ```
99
+ *
100
+ * Note that `transformRequestFunction` has the highest priority and can overwrite request options
101
+ * specified in `globs`, `regexps`, or `pseudoUrls` objects, as well as the global `label` option.
102
+ *
103
+ * The function receives a {@link RequestOptions} object and can return either:
104
+ * - The modified {@link RequestOptions} object
105
+ * - `'unchanged'` to keep the original options as-is
106
+ * - A falsy value or `'skip'` to exclude the request from the queue
94
107
  */
95
108
  transformRequestFunction?: RequestTransform;
96
109
  /**
@@ -130,6 +143,12 @@ export interface EnqueueLinksByClickingElementsOptions {
130
143
  * @default false
131
144
  */
132
145
  skipNavigation?: boolean;
146
+ /**
147
+ * When a request is skipped for some reason, you can use this callback to act on it.
148
+ * This is fired for requests skipped because they don't match enqueueLinks filters
149
+ * or because they were removed by `transformRequestFunction`.
150
+ */
151
+ onSkippedRequest?: SkippedRequestCallback;
133
152
  }
134
153
  /**
135
154
  * The function finds elements matching a specific CSS selector in a Puppeteer page,
@@ -161,7 +180,7 @@ export interface EnqueueLinksByClickingElementsOptions {
161
180
  * ```javascript
162
181
  * await utils.puppeteer.enqueueLinksByClickingElements({
163
182
  * page,
164
- * requestQueue,
183
+ * requestManager,
165
184
  * selector: 'a.product-detail',
166
185
  * pseudoUrls: [
167
186
  * 'https://www.example.com/handbags/[.*]'
@@ -202,6 +221,7 @@ export declare function isTargetRelevant(page: Page, target: Target): boolean;
202
221
  * for large element sets, this will take considerable amount of time.
203
222
  * @ignore
204
223
  */
205
- export declare function clickElements(page: Page, selector: string, clickOptions?: ClickOptions): Promise<void>;
224
+ export declare function clickElements(page: Page, selector: string, clickOptions?: ClickOptions & {
225
+ clickCount?: number;
226
+ }): Promise<void>;
206
227
  export {};
207
- //# sourceMappingURL=click-elements.d.ts.map
@@ -1,10 +1,9 @@
1
1
  import { URL } from 'node:url';
2
- import { constructGlobObjectsFromGlobs, constructRegExpObjectsFromPseudoUrls, constructRegExpObjectsFromRegExps, createRequestOptions, createRequests, } from '@crawlee/browser';
2
+ import { applyRequestTransform, constructGlobObjectsFromGlobs, constructRegExpObjectsFromPseudoUrls, constructRegExpObjectsFromRegExps, createRequestOptions, filterRequestOptionsByPatterns, Request, serviceLocator, } from '@crawlee/browser';
3
3
  import ow from 'ow';
4
- import log_ from '@apify/log';
5
4
  import { addInterceptRequestHandler, removeInterceptRequestHandler } from '../utils/puppeteer_request_interception.js';
6
5
  const STARTING_Z_INDEX = 2147400000;
7
- const log = log_.child({ prefix: 'Puppeteer Click Elements' });
6
+ const getLog = () => serviceLocator.getChildLog('Puppeteer Click Elements');
8
7
  /**
9
8
  * The function finds elements matching a specific CSS selector in a Puppeteer page,
10
9
  * clicks all those elements using a mouse move and a left mouse button click and intercepts
@@ -35,7 +34,7 @@ const log = log_.child({ prefix: 'Puppeteer Click Elements' });
35
34
  * ```javascript
36
35
  * await utils.puppeteer.enqueueLinksByClickingElements({
37
36
  * page,
38
- * requestQueue,
37
+ * requestManager,
39
38
  * selector: 'a.product-detail',
40
39
  * pseudoUrls: [
41
40
  * 'https://www.example.com/handbags/[.*]'
@@ -49,26 +48,41 @@ const log = log_.child({ prefix: 'Puppeteer Click Elements' });
49
48
  export async function enqueueLinksByClickingElements(options) {
50
49
  ow(options, ow.object.exactShape({
51
50
  page: ow.object.hasKeys('goto', 'evaluate'),
52
- requestQueue: ow.object.hasKeys('fetchNextRequest', 'addRequest'),
51
+ requestManager: ow.object.hasKeys('fetchNextRequest', 'addRequestsBatched'),
53
52
  selector: ow.string,
54
53
  userData: ow.optional.object,
55
- clickOptions: ow.optional.object.hasKeys('clickCount', 'delay'),
54
+ clickOptions: ow.optional.object,
56
55
  pseudoUrls: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('purl'))),
57
56
  globs: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('glob'))),
58
57
  regexps: ow.optional.array.ofType(ow.any(ow.regExp, ow.object.hasKeys('regexp'))),
58
+ exclude: ow.optional.array.ofType(ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'))),
59
59
  transformRequestFunction: ow.optional.function,
60
60
  waitForPageIdleSecs: ow.optional.number,
61
61
  maxWaitForPageIdleSecs: ow.optional.number,
62
62
  label: ow.optional.string,
63
63
  forefront: ow.optional.boolean,
64
64
  skipNavigation: ow.optional.boolean,
65
+ onSkippedRequest: ow.optional.function,
65
66
  }));
66
- const { page, requestQueue, selector, clickOptions, pseudoUrls, globs, regexps, transformRequestFunction, waitForPageIdleSecs = 1, maxWaitForPageIdleSecs = 5, forefront, } = options;
67
+ const { page, requestManager, selector, clickOptions,
68
+ // oxlint-disable-next-line typescript/no-deprecated -- still accepted for backwards compat
69
+ pseudoUrls, globs, regexps, transformRequestFunction, waitForPageIdleSecs = 1, maxWaitForPageIdleSecs = 5, forefront, exclude, onSkippedRequest, } = options;
67
70
  const waitForPageIdleMillis = waitForPageIdleSecs * 1000;
68
71
  const maxWaitForPageIdleMillis = maxWaitForPageIdleSecs * 1000;
72
+ const urlExcludePatternObjects = [];
69
73
  const urlPatternObjects = [];
74
+ if (exclude?.length) {
75
+ for (const excl of exclude) {
76
+ if (typeof excl === 'string' || 'glob' in excl) {
77
+ urlExcludePatternObjects.push(...constructGlobObjectsFromGlobs([excl]));
78
+ }
79
+ else if (excl instanceof RegExp || 'regexp' in excl) {
80
+ urlExcludePatternObjects.push(...constructRegExpObjectsFromRegExps([excl]));
81
+ }
82
+ }
83
+ }
70
84
  if (pseudoUrls?.length) {
71
- log.deprecated('`pseudoUrls` option is deprecated, use `globs` or `regexps` instead');
85
+ getLog().deprecated('`pseudoUrls` option is deprecated, use `globs` or `regexps` instead');
72
86
  urlPatternObjects.push(...constructRegExpObjectsFromPseudoUrls(pseudoUrls));
73
87
  }
74
88
  if (globs?.length) {
@@ -84,12 +98,21 @@ export async function enqueueLinksByClickingElements(options) {
84
98
  maxWaitForPageIdleMillis,
85
99
  clickOptions,
86
100
  });
87
- let requestOptions = createRequestOptions(interceptedRequests, options);
101
+ const requestOptions = createRequestOptions(interceptedRequests, options);
102
+ const skippedByFilters = [];
103
+ let filteredOptions = filterRequestOptionsByPatterns(requestOptions, urlPatternObjects.length > 0 ? urlPatternObjects : undefined, urlExcludePatternObjects, undefined, (url) => skippedByFilters.push(url));
104
+ if (onSkippedRequest && skippedByFilters.length > 0) {
105
+ await Promise.all(skippedByFilters.map(async (url) => onSkippedRequest({ url, reason: 'filters' })));
106
+ }
88
107
  if (transformRequestFunction) {
89
- requestOptions = requestOptions.map(transformRequestFunction).filter((r) => !!r);
108
+ const skippedByTransform = [];
109
+ filteredOptions = applyRequestTransform(filteredOptions, transformRequestFunction, (r) => skippedByTransform.push(r));
110
+ if (onSkippedRequest && skippedByTransform.length > 0) {
111
+ await Promise.all(skippedByTransform.map(async (r) => onSkippedRequest({ url: r.url, reason: 'transform' })));
112
+ }
90
113
  }
91
- const requests = createRequests(requestOptions, urlPatternObjects);
92
- const { addedRequests } = await requestQueue.addRequestsBatched(requests, { forefront });
114
+ const requests = filteredOptions.map((opts) => new Request(opts));
115
+ const { addedRequests } = await requestManager.addRequestsBatched(requests, { forefront });
93
116
  return { processedRequests: addedRequests, unprocessedRequests: [] };
94
117
  }
95
118
  /**
@@ -130,6 +153,7 @@ function createInterceptRequestHandler(page, requests) {
130
153
  url,
131
154
  headers: req.headers(),
132
155
  method: req.method(),
156
+ // oxlint-disable-next-line typescript/no-deprecated -- fetchPostData() is async and adds a CDP roundtrip per request; keep the sync page-cached read
133
157
  payload: req.postData(),
134
158
  }));
135
159
  if (req.redirectChain().length) {
@@ -163,7 +187,9 @@ function createTargetCreatedHandler(page, requests) {
163
187
  await createdPage.close();
164
188
  }
165
189
  catch (err) {
166
- log.debug('enqueueLinksByClickingElements: Could not close spawned page.', { error: err.stack });
190
+ getLog().debug('enqueueLinksByClickingElements: Could not close spawned page.', {
191
+ error: err.stack,
192
+ });
167
193
  }
168
194
  };
169
195
  }
@@ -172,6 +198,7 @@ function createTargetCreatedHandler(page, requests) {
172
198
  * There will generally be a lot of other targets being created in the browser.
173
199
  */
174
200
  export function isTargetRelevant(page, target) {
201
+ // oxlint-disable-next-line typescript/no-deprecated -- the non-deprecated replacement (opener.page()) is async and would force every call site to await, including EventEmitter callbacks
175
202
  return target.type() === 'page' && page.target() === target.opener();
176
203
  }
177
204
  /**
@@ -218,8 +245,12 @@ async function preventHistoryNavigation(page) {
218
245
  * @ignore
219
246
  */
220
247
  export async function clickElements(page, selector, clickOptions) {
248
+ // Puppeteer 25 removed the deprecated `clickCount` option in favor of `count`, older versions ignore `count`, so we pass both.
249
+ if (clickOptions?.clickCount !== undefined && clickOptions.count === undefined) {
250
+ clickOptions = { ...clickOptions, count: clickOptions.clickCount };
251
+ }
221
252
  const elementHandles = await page.$$(selector);
222
- log.debug(`enqueueLinksByClickingElements: There are ${elementHandles.length} elements to click.`);
253
+ getLog().debug(`enqueueLinksByClickingElements: There are ${elementHandles.length} elements to click.`);
223
254
  let clickedElementsCount = 0;
224
255
  let zIndex = STARTING_Z_INDEX;
225
256
  let shouldLogWarning = true;
@@ -232,15 +263,15 @@ export async function clickElements(page, selector, clickOptions) {
232
263
  catch (err) {
233
264
  const e = err;
234
265
  if (shouldLogWarning && e.stack.includes('is detached from document')) {
235
- log.warning(`An element with selector ${selector} that you're trying to click has been removed from the page. ` +
266
+ getLog().warning(`An element with selector ${selector} that you're trying to click has been removed from the page. ` +
236
267
  'This was probably caused by an earlier click which triggered some JavaScript on the page that caused it to change. ' +
237
268
  'If you\'re trying to enqueue pagination links, we suggest using the "next" button, if available and going one by one.');
238
269
  shouldLogWarning = false;
239
270
  }
240
- log.debug('enqueueLinksByClickingElements: Click failed.', { stack: e.stack });
271
+ getLog().debug('enqueueLinksByClickingElements: Click failed.', { stack: e.stack });
241
272
  }
242
273
  }
243
- log.debug(`enqueueLinksByClickingElements: Successfully clicked ${clickedElementsCount} elements out of ${elementHandles.length}`);
274
+ getLog().debug(`enqueueLinksByClickingElements: Successfully clicked ${clickedElementsCount} elements out of ${elementHandles.length}`);
244
275
  }
245
276
  /* istanbul ignore next */
246
277
  /**
@@ -277,7 +308,6 @@ function updateElementCssToEnableMouseClick(el, zIndex) {
277
308
  async function waitForPageIdle({ page, waitForPageIdleMillis, maxWaitForPageIdleMillis, }) {
278
309
  return new Promise((resolve) => {
279
310
  let timeout;
280
- let maxTimeout;
281
311
  const context = page.browserContext();
282
312
  function newTabTracker(target) {
283
313
  if (isTargetRelevant(page, target))
@@ -291,7 +321,7 @@ async function waitForPageIdle({ page, waitForPageIdleMillis, maxWaitForPageIdle
291
321
  }, waitForPageIdleMillis);
292
322
  }
293
323
  function maxTimeoutHandler() {
294
- log.debug(`enqueueLinksByClickingElements: Page still showed activity after ${maxWaitForPageIdleMillis}ms. ` +
324
+ getLog().debug(`enqueueLinksByClickingElements: Page still showed activity after ${maxWaitForPageIdleMillis}ms. ` +
295
325
  'This is probably due to the website itself dispatching requests, but some links may also have been missed.');
296
326
  finish();
297
327
  }
@@ -301,7 +331,7 @@ async function waitForPageIdle({ page, waitForPageIdleMillis, maxWaitForPageIdle
301
331
  context.off('targetcreated', newTabTracker);
302
332
  resolve();
303
333
  }
304
- maxTimeout = setTimeout(maxTimeoutHandler, maxWaitForPageIdleMillis);
334
+ const maxTimeout = setTimeout(maxTimeoutHandler, maxWaitForPageIdleMillis);
305
335
  activityHandler(); // We call this once manually in case there would be no requests at all.
306
336
  page.on('request', activityHandler);
307
337
  page.on('framenavigated', activityHandler);
@@ -325,8 +355,7 @@ async function restoreHistoryNavigationAndSaveCapturedUrls(page, requests) {
325
355
  requests.add(JSON.stringify({ url }));
326
356
  }
327
357
  catch (err) {
328
- log.debug('enqueueLinksByClickingElements: Failed to ', { error: err.stack });
358
+ getLog().debug('enqueueLinksByClickingElements: Failed to ', { error: err.stack });
329
359
  }
330
360
  });
331
361
  }
332
- //# sourceMappingURL=click-elements.js.map
@@ -1,20 +1,20 @@
1
- import type { BrowserCrawlerOptions, BrowserCrawlingContext, BrowserHook, BrowserRequestHandler, GetUserDataFromRequest, RouterRoutes } from '@crawlee/browser';
2
- import { BrowserCrawler, Configuration } from '@crawlee/browser';
3
- import type { PuppeteerController, PuppeteerPlugin } from '@crawlee/browser-pool';
1
+ import type { BrowserCrawlerOptions, BrowserCrawlingContext, BrowserHook, GetUserDataFromRequest, RouterHandler, RouterRoutes, RouteSchemas, RoutesFromSchemas } from '@crawlee/browser';
2
+ import { BrowserCrawler } from '@crawlee/browser';
3
+ import type { PuppeteerPlugin } from '@crawlee/browser-pool';
4
4
  import type { Dictionary } from '@crawlee/types';
5
5
  // @ts-ignore optional peer dependency or compatibility with es2022
6
6
  import type { HTTPResponse, LaunchOptions, Page } from 'puppeteer';
7
+ import type { EnqueueLinksByClickingElementsOptions } from './enqueue-links/click-elements.js';
7
8
  import type { PuppeteerLaunchContext } from './puppeteer-launcher.js';
8
- import type { DirectNavigationOptions, PuppeteerContextUtils } from './utils/puppeteer_utils.js';
9
- export interface PuppeteerCrawlingContext<UserData extends Dictionary = Dictionary> extends BrowserCrawlingContext<PuppeteerCrawler, Page, HTTPResponse, PuppeteerController, UserData>, PuppeteerContextUtils {
9
+ import type { InterceptHandler } from './utils/puppeteer_request_interception.js';
10
+ import type { BlockRequestsOptions, DirectNavigationOptions, InfiniteScrollOptions, InjectFileOptions, PuppeteerContextUtils, SaveSnapshotOptions } from './utils/puppeteer_utils.js';
11
+ export type PuppeteerGoToOptions = NonNullable<Parameters<Page['goto']>[1]>;
12
+ export interface PuppeteerCrawlingContext<UserData extends Dictionary = Dictionary> extends BrowserCrawlingContext<Page, HTTPResponse, UserData, PuppeteerGoToOptions>, PuppeteerContextUtils {
10
13
  }
11
14
  // @ts-ignore optional peer dependency or compatibility with es2022
12
- export interface PuppeteerHook extends BrowserHook<PuppeteerCrawlingContext, PuppeteerGoToOptions> {
15
+ export interface PuppeteerHook extends BrowserHook<PuppeteerCrawlingContext> {
13
16
  }
14
- export interface PuppeteerRequestHandler extends BrowserRequestHandler<PuppeteerCrawlingContext> {
15
- }
16
- export type PuppeteerGoToOptions = Parameters<Page['goto']>[1];
17
- export interface PuppeteerCrawlerOptions extends BrowserCrawlerOptions<PuppeteerCrawlingContext, {
17
+ export interface PuppeteerCrawlerOptions<ContextExtension = Dictionary<never>, ExtendedContext extends PuppeteerCrawlingContext = PuppeteerCrawlingContext & ContextExtension> extends BrowserCrawlerOptions<Page, HTTPResponse, PuppeteerCrawlingContext, ContextExtension, ExtendedContext, {
18
18
  browserPlugins: [PuppeteerPlugin];
19
19
  }> {
20
20
  /**
@@ -23,25 +23,25 @@ export interface PuppeteerCrawlerOptions extends BrowserCrawlerOptions<Puppeteer
23
23
  launchContext?: PuppeteerLaunchContext;
24
24
  /**
25
25
  * Async functions that are sequentially evaluated before the navigation. Good for setting additional cookies
26
- * or browser properties before navigation. The function accepts two parameters, `crawlingContext` and `gotoOptions`,
27
- * which are passed to the `page.goto()` function the crawler calls to navigate.
26
+ * or browser properties before navigation. The function receives the `crawlingContext`; the options object
27
+ * forwarded to `page.goto()` is available as `crawlingContext.gotoOptions` and can be mutated in place.
28
+ * A hook may optionally return a partial object whose properties are merged into the crawling context
29
+ * (e.g. to override context members for subsequent hooks and pipeline stages).
28
30
  * Example:
29
31
  * ```
30
32
  * preNavigationHooks: [
31
- * async (crawlingContext, gotoOptions) => {
32
- * const { page } = crawlingContext;
33
+ * async ({ page, gotoOptions }) => {
33
34
  * await page.evaluate((attr) => { window.foo = attr; }, 'bar');
35
+ * gotoOptions.timeout = 60_000;
34
36
  * },
35
37
  * ]
36
38
  * ```
37
- *
38
- * Modyfing `pageOptions` is supported only in Playwright incognito.
39
- * See {@link PrePageCreateHook}
40
39
  */
41
40
  preNavigationHooks?: PuppeteerHook[];
42
41
  /**
43
42
  * Async functions that are sequentially evaluated after the navigation. Good for checking if the navigation was successful.
44
- * The function accepts `crawlingContext` as the only parameter.
43
+ * The function accepts `crawlingContext` as the only parameter. A hook may optionally return a partial object
44
+ * whose properties are merged into the crawling context (e.g. to override `response` after solving a challenge).
45
45
  * Example:
46
46
  * ```
47
47
  * postNavigationHooks: [
@@ -67,13 +67,15 @@ export interface PuppeteerCrawlerOptions extends BrowserCrawlerOptions<Puppeteer
67
67
  * If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
68
68
  * which downloads the pages using raw HTTP requests and is about 10x faster.
69
69
  *
70
- * The source URLs are represented using {@link Request} objects that are fed from
71
- * {@link RequestList} or {@link RequestQueue} instances provided by the {@link PuppeteerCrawlerOptions.requestList}
72
- * or {@link PuppeteerCrawlerOptions.requestQueue} constructor options, respectively.
70
+ * The source URLs are represented using {@link Request} objects that are fed from the
71
+ * {@link IRequestManager|request manager} provided via the {@link PuppeteerCrawlerOptions.requestManager|`requestManager`}
72
+ * constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
73
+ * as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
74
+ * {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
75
+ * result as `requestManager`.
73
76
  *
74
- * If both {@link PuppeteerCrawlerOptions.requestList} and {@link PuppeteerCrawlerOptions.requestQueue} are used,
75
- * the instance first processes URLs from the {@link RequestList} and automatically enqueues all of them
76
- * to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
77
+ * > The {@link PuppeteerCrawlerOptions.requestList|`requestList`} and {@link PuppeteerCrawlerOptions.requestQueue|`requestQueue`}
78
+ * > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
77
79
  *
78
80
  * The crawler finishes when there are no more {@link Request} objects to crawl.
79
81
  *
@@ -119,11 +121,9 @@ export interface PuppeteerCrawlerOptions extends BrowserCrawlerOptions<Puppeteer
119
121
  * ```
120
122
  * @category Crawlers
121
123
  */
122
- export declare class PuppeteerCrawler extends BrowserCrawler<{
124
+ export declare class PuppeteerCrawler<ContextExtension = Dictionary<never>, ExtendedContext extends PuppeteerCrawlingContext = PuppeteerCrawlingContext & ContextExtension> extends BrowserCrawler<Page, HTTPResponse, {
123
125
  browserPlugins: [PuppeteerPlugin];
124
- }, LaunchOptions, PuppeteerCrawlingContext> {
125
- private readonly options;
126
- readonly config: Configuration;
126
+ }, LaunchOptions, PuppeteerCrawlingContext, ContextExtension, ExtendedContext> {
127
127
  protected static optionsShape: {
128
128
  // @ts-ignore optional peer dependency or compatibility with es2022
129
129
  browserPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
@@ -138,17 +138,17 @@ export declare class PuppeteerCrawler extends BrowserCrawler<{
138
138
  // @ts-ignore optional peer dependency or compatibility with es2022
139
139
  headless: import("ow").AnyPredicate<string | boolean>;
140
140
  // @ts-ignore optional peer dependency or compatibility with es2022
141
- sessionPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
141
+ browserPool: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
142
142
  // @ts-ignore optional peer dependency or compatibility with es2022
143
- persistCookiesPerSession: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
143
+ remoteBrowser: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
144
144
  // @ts-ignore optional peer dependency or compatibility with es2022
145
- useSessionPool: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
145
+ saveResponseCookies: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
146
146
  // @ts-ignore optional peer dependency or compatibility with es2022
147
147
  proxyConfiguration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
148
148
  // @ts-ignore optional peer dependency or compatibility with es2022
149
- ignoreShadowRoots: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
149
+ contextPipelineBuilder: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
150
150
  // @ts-ignore optional peer dependency or compatibility with es2022
151
- ignoreIframes: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
151
+ extendContext: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
152
152
  // @ts-ignore optional peer dependency or compatibility with es2022
153
153
  requestList: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
154
154
  // @ts-ignore optional peer dependency or compatibility with es2022
@@ -165,24 +165,40 @@ export declare class PuppeteerCrawler extends BrowserCrawler<{
165
165
  maxRequestRetries: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
166
166
  // @ts-ignore optional peer dependency or compatibility with es2022
167
167
  sameDomainDelaySecs: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
168
- // @ts-ignore optional peer dependency or compatibility with es2022
169
- maxSessionRotations: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
170
168
  // @ts-ignore optional peer dependency or compatibility with es2022
171
169
  maxRequestsPerCrawl: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
170
+ // @ts-ignore optional peer dependency or compatibility with es2022
171
+ maxCrawlDepth: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
172
172
  // @ts-ignore optional peer dependency or compatibility with es2022
173
173
  autoscaledPoolOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
174
+ // @ts-ignore optional peer dependency or compatibility with es2022
175
+ sessionPool: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
174
176
  // @ts-ignore optional peer dependency or compatibility with es2022
175
177
  statusMessageLoggingInterval: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
176
178
  // @ts-ignore optional peer dependency or compatibility with es2022
177
179
  statusMessageCallback: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
180
+ // @ts-ignore optional peer dependency or compatibility with es2022
181
+ additionalHttpErrorStatusCodes: import("ow").ArrayPredicate<number>;
182
+ // @ts-ignore optional peer dependency or compatibility with es2022
183
+ ignoreHttpErrorStatusCodes: import("ow").ArrayPredicate<number>;
184
+ // @ts-ignore optional peer dependency or compatibility with es2022
185
+ blockedStatusCodes: import("ow").ArrayPredicate<number>;
178
186
  // @ts-ignore optional peer dependency or compatibility with es2022
179
187
  retryOnBlocked: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
180
188
  // @ts-ignore optional peer dependency or compatibility with es2022
181
- respectRobotsTxtFile: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
189
+ respectRobotsTxtFile: import("ow").AnyPredicate<boolean | object>;
182
190
  // @ts-ignore optional peer dependency or compatibility with es2022
183
191
  onSkippedRequest: import("ow").Predicate<Function> & import("ow").BasePredicate<Function | undefined>;
184
192
  // @ts-ignore optional peer dependency or compatibility with es2022
185
193
  httpClient: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
194
+ // @ts-ignore optional peer dependency or compatibility with es2022
195
+ configuration: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
196
+ // @ts-ignore optional peer dependency or compatibility with es2022
197
+ storageBackend: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
198
+ // @ts-ignore optional peer dependency or compatibility with es2022
199
+ eventManager: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
200
+ // @ts-ignore optional peer dependency or compatibility with es2022
201
+ logger: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
186
202
  // @ts-ignore optional peer dependency or compatibility with es2022
187
203
  minConcurrency: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
188
204
  // @ts-ignore optional peer dependency or compatibility with es2022
@@ -191,18 +207,34 @@ export declare class PuppeteerCrawler extends BrowserCrawler<{
191
207
  maxRequestsPerMinute: import("ow").NumberPredicate & import("ow").BasePredicate<number | undefined>;
192
208
  // @ts-ignore optional peer dependency or compatibility with es2022
193
209
  keepAlive: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
194
- // @ts-ignore optional peer dependency or compatibility with es2022
195
- log: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
196
- // @ts-ignore optional peer dependency or compatibility with es2022
197
- experiments: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
198
210
  // @ts-ignore optional peer dependency or compatibility with es2022
199
211
  statisticsOptions: import("ow").ObjectPredicate<object> & import("ow").BasePredicate<object | undefined>;
212
+ // @ts-ignore optional peer dependency or compatibility with es2022
213
+ id: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
200
214
  };
201
215
  /**
202
216
  * All `PuppeteerCrawler` parameters are passed via an options object.
203
217
  */
204
- constructor(options?: PuppeteerCrawlerOptions, config?: Configuration);
205
- protected _runRequestHandler(context: PuppeteerCrawlingContext): Promise<void>;
218
+ constructor(options?: PuppeteerCrawlerOptions<ContextExtension, ExtendedContext>);
219
+ // @ts-ignore optional peer dependency or compatibility with es2022
220
+ protected buildContextPipeline(): import("@crawlee/browser").ContextPipeline<import("@crawlee/browser").CrawlingContext<Dictionary>, BrowserCrawlingContext<Page, HTTPResponse, Dictionary, Dictionary> & {
221
+ injectFile: (filePath: string, options?: InjectFileOptions) => Promise<unknown>;
222
+ injectJQuery: () => Promise<void>;
223
+ waitForSelector: (selector: string, timeoutMs?: number) => Promise<void>;
224
+ // @ts-ignore optional peer dependency or compatibility with es2022
225
+ parseWithCheerio: (selector?: string, timeoutMs?: number) => Promise<import("@crawlee/browser").CheerioAPI>;
226
+ // @ts-ignore optional peer dependency or compatibility with es2022
227
+ enqueueLinksByClickingElements: (options: Omit<EnqueueLinksByClickingElementsOptions, "page" | "requestManager">) => Promise<import("@crawlee/types").BatchAddRequestsResult>;
228
+ blockRequests: (options?: BlockRequestsOptions) => Promise<void>;
229
+ // @ts-ignore optional peer dependency or compatibility with es2022
230
+ compileScript: (scriptString: string, ctx?: Dictionary) => import("./utils/puppeteer_utils.js").CompiledScriptFunction;
231
+ addInterceptRequestHandler: (handler: InterceptHandler) => Promise<void>;
232
+ removeInterceptRequestHandler: (handler: InterceptHandler) => Promise<void>;
233
+ infiniteScroll: (options?: InfiniteScrollOptions) => Promise<void>;
234
+ saveSnapshot: (options?: SaveSnapshotOptions) => Promise<void>;
235
+ closeCookieModals: () => Promise<void>;
236
+ }>;
237
+ private enhanceContext;
206
238
  protected _navigationHandler(crawlingContext: PuppeteerCrawlingContext, gotoOptions: DirectNavigationOptions): Promise<HTTPResponse | null>;
207
239
  }
208
240
  /**
@@ -229,6 +261,6 @@ export declare class PuppeteerCrawler extends BrowserCrawler<{
229
261
  * await crawler.run();
230
262
  * ```
231
263
  */
232
- // @ts-ignore optional peer dependency or compatibility with es2022
233
- export declare function createPuppeteerRouter<Context extends PuppeteerCrawlingContext = PuppeteerCrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, UserData>): import("@crawlee/browser").RouterHandler<Context>;
234
- //# sourceMappingURL=puppeteer-crawler.d.ts.map
264
+ export declare function createPuppeteerRouter<Context extends PuppeteerCrawlingContext = PuppeteerCrawlingContext, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<Context['request']>>>(routes?: RouterRoutes<Context, Routes>): RouterHandler<Context, Routes>;
265
+ export declare function createPuppeteerRouter<Context extends PuppeteerCrawlingContext = PuppeteerCrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, Record<string, UserData>>): RouterHandler<Context, Record<string, UserData>>;
266
+ export declare function createPuppeteerRouter<Context extends PuppeteerCrawlingContext = PuppeteerCrawlingContext, const Schemas extends RouteSchemas = RouteSchemas>(schemas: Schemas): RouterHandler<Context, RoutesFromSchemas<Schemas>>;