@crawlee/playwright 4.0.0-beta.7 → 4.0.0-beta.71
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 +17 -13
- package/internals/adaptive-playwright-crawler.d.ts +67 -57
- package/internals/adaptive-playwright-crawler.d.ts.map +1 -1
- package/internals/adaptive-playwright-crawler.js +298 -205
- package/internals/adaptive-playwright-crawler.js.map +1 -1
- package/internals/enqueue-links/click-elements.d.ts +32 -13
- package/internals/enqueue-links/click-elements.d.ts.map +1 -1
- package/internals/enqueue-links/click-elements.js +45 -21
- package/internals/enqueue-links/click-elements.js.map +1 -1
- package/internals/playwright-crawler.d.ts +91 -73
- package/internals/playwright-crawler.d.ts.map +1 -1
- package/internals/playwright-crawler.js +82 -19
- package/internals/playwright-crawler.js.map +1 -1
- package/internals/playwright-launcher.d.ts +2 -0
- package/internals/playwright-launcher.d.ts.map +1 -1
- package/internals/playwright-launcher.js +1 -1
- package/internals/playwright-launcher.js.map +1 -1
- package/internals/utils/playwright-utils.d.ts +30 -15
- package/internals/utils/playwright-utils.d.ts.map +1 -1
- package/internals/utils/playwright-utils.js +70 -66
- package/internals/utils/playwright-utils.js.map +1 -1
- package/internals/utils/rendering-type-prediction.d.ts +9 -5
- package/internals/utils/rendering-type-prediction.d.ts.map +1 -1
- package/internals/utils/rendering-type-prediction.js +58 -23
- package/internals/utils/rendering-type-prediction.js.map +1 -1
- package/package.json +14 -10
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { BrowserCrawler,
|
|
1
|
+
import { BrowserCrawler, RequestState, Router, serviceLocator } from '@crawlee/browser';
|
|
2
2
|
import ow from 'ow';
|
|
3
3
|
import { PlaywrightLauncher } from './playwright-launcher.js';
|
|
4
|
-
import { gotoExtended,
|
|
4
|
+
import { gotoExtended, playwrightUtils } from './utils/playwright-utils.js';
|
|
5
5
|
/**
|
|
6
6
|
* Provides a simple framework for parallel crawling of web pages
|
|
7
7
|
* using headless Chromium, Firefox and Webkit browsers with [Playwright](https://github.com/microsoft/playwright).
|
|
@@ -13,13 +13,15 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
13
13
|
* If the target website doesn't need JavaScript, consider using {@link CheerioCrawler},
|
|
14
14
|
* which downloads the pages using raw HTTP requests and is about 10x faster.
|
|
15
15
|
*
|
|
16
|
-
* The source URLs are represented using {@link Request} objects that are fed from
|
|
17
|
-
* {@link
|
|
18
|
-
*
|
|
16
|
+
* The source URLs are represented using {@link Request} objects that are fed from the
|
|
17
|
+
* {@link IRequestManager|request manager} provided via the {@link PlaywrightCrawlerOptions.requestManager|`requestManager`}
|
|
18
|
+
* constructor option (a {@link RequestQueue} is itself a request manager). To read from a read-only source such
|
|
19
|
+
* as a {@link RequestList} while still being able to enqueue new requests, combine it with a queue into a
|
|
20
|
+
* {@link RequestManagerTandem} via {@link IRequestLoader.toTandem|`requestLoader.toTandem()`} and pass the
|
|
21
|
+
* result as `requestManager`.
|
|
19
22
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* to {@link RequestQueue} before it starts their processing. This ensures that a single URL is not crawled multiple times.
|
|
23
|
+
* > The {@link PlaywrightCrawlerOptions.requestList|`requestList`} and {@link PlaywrightCrawlerOptions.requestQueue|`requestQueue`}
|
|
24
|
+
* > options are deprecated; they are still accepted and folded into a single `requestManager` for back-compat.
|
|
23
25
|
*
|
|
24
26
|
* The crawler finishes when there are no more {@link Request} objects to crawl.
|
|
25
27
|
*
|
|
@@ -66,19 +68,19 @@ import { gotoExtended, registerUtilsToContext } from './utils/playwright-utils.j
|
|
|
66
68
|
* @category Crawlers
|
|
67
69
|
*/
|
|
68
70
|
export class PlaywrightCrawler extends BrowserCrawler {
|
|
69
|
-
options;
|
|
70
|
-
config;
|
|
71
71
|
static optionsShape = {
|
|
72
72
|
...BrowserCrawler.optionsShape,
|
|
73
73
|
browserPoolOptions: ow.optional.object,
|
|
74
74
|
launcher: ow.optional.object,
|
|
75
|
+
ignoreIframes: ow.optional.boolean,
|
|
76
|
+
ignoreShadowRoots: ow.optional.boolean,
|
|
75
77
|
};
|
|
76
78
|
/**
|
|
77
79
|
* All `PlaywrightCrawler` parameters are passed via an options object.
|
|
78
80
|
*/
|
|
79
|
-
constructor(options = {}
|
|
81
|
+
constructor(options = {}) {
|
|
80
82
|
ow(options, 'PlaywrightCrawlerOptions', ow.object.exactShape(PlaywrightCrawler.optionsShape));
|
|
81
|
-
const { launchContext = {}, headless, ...browserCrawlerOptions } = options;
|
|
83
|
+
const { launchContext = {}, headless, contextPipelineBuilder, ...browserCrawlerOptions } = options;
|
|
82
84
|
const browserPoolOptions = {
|
|
83
85
|
...options.browserPoolOptions,
|
|
84
86
|
};
|
|
@@ -95,19 +97,80 @@ export class PlaywrightCrawler extends BrowserCrawler {
|
|
|
95
97
|
launchContext.launchOptions ??= {};
|
|
96
98
|
launchContext.launchOptions.headless = headless;
|
|
97
99
|
}
|
|
98
|
-
const playwrightLauncher = new PlaywrightLauncher(launchContext,
|
|
100
|
+
const playwrightLauncher = new PlaywrightLauncher(launchContext, options.configuration);
|
|
99
101
|
browserPoolOptions.browserPlugins = [playwrightLauncher.createBrowserPlugin()];
|
|
100
|
-
super({
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
super({
|
|
103
|
+
...browserCrawlerOptions,
|
|
104
|
+
launchContext,
|
|
105
|
+
browserPoolOptions,
|
|
106
|
+
contextPipelineBuilder: contextPipelineBuilder ?? (() => this.buildContextPipeline()),
|
|
107
|
+
});
|
|
103
108
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
await super._runRequestHandler(context);
|
|
109
|
+
buildContextPipeline() {
|
|
110
|
+
return super.buildContextPipeline().compose({ action: this.enhanceContext.bind(this) });
|
|
107
111
|
}
|
|
108
112
|
async _navigationHandler(crawlingContext, gotoOptions) {
|
|
109
113
|
return gotoExtended(crawlingContext.page, crawlingContext.request, gotoOptions);
|
|
110
114
|
}
|
|
115
|
+
async enhanceContext(context) {
|
|
116
|
+
const waitForSelector = async (selector, timeoutMs = 5_000) => {
|
|
117
|
+
const locator = context.page.locator(selector).first();
|
|
118
|
+
await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
injectFile: async (filePath, options) => playwrightUtils.injectFile(context.page, filePath, options),
|
|
122
|
+
injectJQuery: async () => {
|
|
123
|
+
if (context.request.state === RequestState.BEFORE_NAV) {
|
|
124
|
+
context.log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
|
|
125
|
+
await playwrightUtils.injectJQuery(context.page);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
await playwrightUtils.injectJQuery(context.page, { surviveNavigations: false });
|
|
129
|
+
},
|
|
130
|
+
blockRequests: async (options) => playwrightUtils.blockRequests(context.page, options),
|
|
131
|
+
waitForSelector,
|
|
132
|
+
parseWithCheerio: async (selector, timeoutMs = 5_000) => {
|
|
133
|
+
if (selector) {
|
|
134
|
+
await waitForSelector(selector, timeoutMs);
|
|
135
|
+
}
|
|
136
|
+
return playwrightUtils.parseWithCheerio(context.page, this.ignoreShadowRoots, this.ignoreIframes);
|
|
137
|
+
},
|
|
138
|
+
infiniteScroll: async (options) => playwrightUtils.infiniteScroll(context.page, options),
|
|
139
|
+
saveSnapshot: async (options) => playwrightUtils.saveSnapshot(context.page, { ...options, config: serviceLocator.getConfiguration() }),
|
|
140
|
+
enqueueLinksByClickingElements: async (options) => playwrightUtils.enqueueLinksByClickingElements({
|
|
141
|
+
...options,
|
|
142
|
+
page: context.page,
|
|
143
|
+
requestManager: this.requestManager,
|
|
144
|
+
}),
|
|
145
|
+
compileScript: (scriptString, ctx) => playwrightUtils.compileScript(scriptString, ctx),
|
|
146
|
+
closeCookieModals: async () => playwrightUtils.closeCookieModals(context.page),
|
|
147
|
+
handleCloudflareChallenge: async (options) => {
|
|
148
|
+
return playwrightUtils.handleCloudflareChallenge(context.page, context.request.url, options);
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Returns a `postNavigationHooks`-ready hook that runs {@link PlaywrightContextUtils.handleCloudflareChallenge}
|
|
155
|
+
* and propagates the post-challenge {@link Response} back into the crawling context via its return value.
|
|
156
|
+
*
|
|
157
|
+
* **Example usage**
|
|
158
|
+
* ```ts
|
|
159
|
+
* import { PlaywrightCrawler, handleCloudflareChallengeHook } from 'crawlee';
|
|
160
|
+
*
|
|
161
|
+
* const crawler = new PlaywrightCrawler({
|
|
162
|
+
* postNavigationHooks: [handleCloudflareChallengeHook()],
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export function handleCloudflareChallengeHook(options) {
|
|
167
|
+
return async (context) => {
|
|
168
|
+
const response = await context.handleCloudflareChallenge(options);
|
|
169
|
+
if (response !== undefined) {
|
|
170
|
+
return { response };
|
|
171
|
+
}
|
|
172
|
+
return undefined;
|
|
173
|
+
};
|
|
111
174
|
}
|
|
112
175
|
/**
|
|
113
176
|
* Creates new {@link Router} instance that works based on request labels.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"playwright-crawler.js","sourceRoot":"","sources":["../../src/internals/playwright-crawler.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,
|
|
1
|
+
{"version":3,"file":"playwright-crawler.js","sourceRoot":"","sources":["../../src/internals/playwright-crawler.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAGxF,OAAO,EAAE,MAAM,IAAI,CAAC;AAKpB,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAU9D,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAqF5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Aa,CAAC,YAAY,EAAE,GAAG,CAAC;YAC3G,iBAAiB,EAAE,KAAK,IAAI,EAAE,CAAC,eAAe,CAAC,iBAAiB,CAAC,OAAO,CAAC,IAAI,CAAC;YAC9E,yBAAyB,EAAE,KAAK,EAAE,OAA0C,EAAE,EAAE;gBAC5E,OAAO,eAAe,CAAC,yBAAyB,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YACjG,CAAC;SACJ,CAAC;IACN,CAAC;;AAGL;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,6BAA6B,CAAC,OAA0C;IACpF,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACrB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,yBAAyB,CAAC,OAAO,CAAC,CAAC;QAClE,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YACzB,OAAO,EAAE,QAAQ,EAAE,CAAC;QACxB,CAAC;QACD,OAAO,SAAS,CAAC;IACrB,CAAC,CAAC;AACN,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,UAAU,sBAAsB,CAGpC,MAAwC;IACtC,OAAO,MAAM,CAAC,MAAM,CAAU,MAAM,CAAC,CAAC;AAC1C,CAAC"}
|
|
@@ -84,6 +84,8 @@ export declare class PlaywrightLauncher extends BrowserLauncher<PlaywrightPlugin
|
|
|
84
84
|
useIncognitoPages: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
85
85
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
86
86
|
browserPerProxy: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
87
|
+
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
88
|
+
ignoreProxyCertificate: import("ow").BooleanPredicate & import("ow").BasePredicate<boolean | undefined>;
|
|
87
89
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
88
90
|
userDataDir: import("ow").StringPredicate & import("ow").BasePredicate<string | undefined>;
|
|
89
91
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"playwright-launcher.d.ts","sourceRoot":"","sources":["../../src/internals/playwright-launcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,uBAAwB,SAAQ,oBAAoB,CAAC,aAAa,EAAE,WAAW,CAAC;IAC7F;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,GAAG,UAAU,CAAC,WAAW,CAAC,yBAAyB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEtF;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAE5B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,QAAQ,CAAC,EAAE,WAAW,CAAC;CAC1B;AAED;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,eAAe,CAAC,gBAAgB,CAAC;aAY/C,MAAM;IAX5B,iBAA0B,YAAY
|
|
1
|
+
{"version":3,"file":"playwright-launcher.d.ts","sourceRoot":"","sources":["../../src/internals/playwright-launcher.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAC7D,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,OAAO,KAAK,EAAE,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEtE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,uBAAwB,SAAQ,oBAAoB,CAAC,aAAa,EAAE,WAAW,CAAC;IAC7F;;;OAGG;IACH,aAAa,CAAC,EAAE,aAAa,GAAG,UAAU,CAAC,WAAW,CAAC,yBAAyB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAEtF;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;;;;;OAQG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAE5B;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,QAAQ,CAAC,EAAE,WAAW,CAAC;CAC1B;AAED;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,eAAe,CAAC,gBAAgB,CAAC;aAY/C,MAAM;IAX5B,iBAA0B,YAAY;;;;;;;;;;;MAIpC;IAEF;;OAEG;gBAEC,aAAa,GAAE,uBAA4B,EACzB,MAAM,gBAAkC;CA2BjE;AA0BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAsB,gBAAgB,CAClC,aAAa,CAAC,EAAE,uBAAuB,EACvC,MAAM,gBAAkC,GACzC,OAAO,CAAC,OAAO,CAAC,CAIlB"}
|
|
@@ -37,7 +37,7 @@ export class PlaywrightLauncher extends BrowserLauncher {
|
|
|
37
37
|
* @ignore
|
|
38
38
|
*/
|
|
39
39
|
function getDefaultExecutablePath(launchContext, config) {
|
|
40
|
-
const pathFromPlaywrightImage = config.
|
|
40
|
+
const pathFromPlaywrightImage = config.defaultBrowserPath;
|
|
41
41
|
const { launchOptions = {} } = launchContext;
|
|
42
42
|
if (launchOptions.executablePath) {
|
|
43
43
|
return launchOptions.executablePath;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"playwright-launcher.js","sourceRoot":"","sources":["../../src/internals/playwright-launcher.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAAE,MAAM,IAAI,CAAC;AAuEpB;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,eAAiC;IAY/C;IAXZ,MAAM,CAAU,YAAY,GAAG;QACrC,GAAG,eAAe,CAAC,YAAY;QAC/B,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM;QAC5B,oBAAoB,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM;KAC3C,CAAC;IAEF;;OAEG;IACH,YACI,gBAAyC,EAAE,EACzB,SAAS,aAAa,CAAC,eAAe,EAAE;QAE1D,EAAE,CAAC,aAAa,EAAE,2BAA2B,EAAE,EAAE,CAAC,MAAM,CAAC,UAAU,CAAC,kBAAkB,CAAC,YAAY,CAAC,CAAC,CAAC;QAEtG,MAAM,EACF,QAAQ,GAAG,eAAe,CAAC,sBAAsB,CAC7C,YAAY,EACZ,+BAA+B,CAClC,CAAC,QAAQ,GACb,GAAG,aAAa,CAAC;QAElB,MAAM,EAAE,aAAa,GAAG,EAAE,EAAE,GAAG,IAAI,EAAE,GAAG,aAAa,CAAC;QAEtD,KAAK,CACD;YACI,GAAG,IAAI;YACP,aAAa,EAAE;gBACX,GAAG,aAAa;gBAChB,cAAc,EAAE,wBAAwB,CAAC,aAAa,EAAE,MAAM,CAAC;aAClE;YACD,QAAQ;SACX,EACD,MAAM,CACT,CAAC;QAvBgB,WAAM,GAAN,MAAM,CAAkC;QAyB1D,IAAI,CAAC,MAAM,GAAG,gBAAgB,CAA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BAAgB,CAAC;IACnC,CAAC;;AAGL;;;;GAIG;AACH,SAAS,wBAAwB,CAAC,aAAsC,EAAE,MAAqB;IAC3F,MAAM,uBAAuB,GAAG,MAAM,CAAC,kBAAkB,CAAC;IAC1D,MAAM,EAAE,aAAa,GAAG,EAAE,EAAE,GAAG,aAAa,CAAC;IAE7C,IAAI,aAAa,CAAC,cAAc,EAAE,CAAC;QAC/B,OAAO,aAAa,CAAC,cAAc,CAAC;IACxC,CAAC;IAED,IAAI,aAAa,CAAC,SAAS,EAAE,CAAC;QAC1B,OAAO,SAAS,CAAC;IACrB,CAAC;IAED,IAAI,uBAAuB,EAAE,CAAC;QAC1B,OAAO,uBAAuB,CAAC;IACnC,CAAC;IAED,OAAO,SAAS,CAAC;AACrB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAClC,aAAuC,EACvC,MAAM,GAAG,aAAa,CAAC,eAAe,EAAE;IAExC,MAAM,kBAAkB,GAAG,IAAI,kBAAkB,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;IAEzE,OAAO,kBAAkB,CAAC,MAAM,EAAE,CAAC;AACvC,CAAC"}
|
|
@@ -17,14 +17,13 @@
|
|
|
17
17
|
* ```
|
|
18
18
|
* @module playwrightUtils
|
|
19
19
|
*/
|
|
20
|
-
import { Configuration, type Request
|
|
20
|
+
import { Configuration, type Request } from '@crawlee/browser';
|
|
21
21
|
import type { BatchAddRequestsResult } from '@crawlee/types';
|
|
22
22
|
import { type CheerioRoot, type Dictionary } from '@crawlee/utils';
|
|
23
23
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
24
24
|
import type { Page, Response } from 'playwright';
|
|
25
25
|
import type { EnqueueLinksByClickingElementsOptions } from '../enqueue-links/click-elements.js';
|
|
26
26
|
import { enqueueLinksByClickingElements } from '../enqueue-links/click-elements.js';
|
|
27
|
-
import type { PlaywrightCrawlerOptions, PlaywrightCrawlingContext } from '../playwright-crawler.js';
|
|
28
27
|
import { RenderingTypePredictor } from './rendering-type-prediction.js';
|
|
29
28
|
export interface InjectFileOptions {
|
|
30
29
|
/**
|
|
@@ -290,7 +289,7 @@ export declare function saveSnapshot(page: Page, options?: SaveSnapshotOptions):
|
|
|
290
289
|
*/
|
|
291
290
|
export declare function parseWithCheerio(page: Page, ignoreShadowRoots?: boolean, ignoreIframes?: boolean): Promise<CheerioRoot>;
|
|
292
291
|
export declare function closeCookieModals(page: Page): Promise<void>;
|
|
293
|
-
interface HandleCloudflareChallengeOptions {
|
|
292
|
+
export interface HandleCloudflareChallengeOptions {
|
|
294
293
|
/** Logging defaults to the `debug` level, use this flag to log to `info` level instead. */
|
|
295
294
|
verbose?: boolean;
|
|
296
295
|
/** How long should we wait after the challenge is completed for the final page to load. */
|
|
@@ -304,6 +303,13 @@ interface HandleCloudflareChallengeOptions {
|
|
|
304
303
|
isChallengeCallback?: (page: Page) => Promise<boolean>;
|
|
305
304
|
/** Allows overriding the detection of Cloudflare "blocked page". */
|
|
306
305
|
isBlockedCallback?: (page: Page) => Promise<boolean>;
|
|
306
|
+
/** Allows overriding how the checkbox click position is calculated. */
|
|
307
|
+
clickPositionCallback?: (page: Page) => Promise<{
|
|
308
|
+
x: number;
|
|
309
|
+
y: number;
|
|
310
|
+
} | null>;
|
|
311
|
+
/** Optional delay (in seconds) before the first click attempt on the challenge checkbox. Defaults to 1s. */
|
|
312
|
+
preChallengeSleepSecs?: number;
|
|
307
313
|
}
|
|
308
314
|
/**
|
|
309
315
|
* This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox.
|
|
@@ -312,23 +318,24 @@ interface HandleCloudflareChallengeOptions {
|
|
|
312
318
|
* result in a SessionError which will be automatically retried, so only successful requests will get
|
|
313
319
|
* into the `requestHandler`.
|
|
314
320
|
*
|
|
321
|
+
* On a successfully solved challenge the page is reloaded and the new {@link Response} is returned, so
|
|
322
|
+
* it can be propagated back to the crawling context via a hook return value (see
|
|
323
|
+
* {@link handleCloudflareChallengeHook}).
|
|
324
|
+
*
|
|
315
325
|
* Works best with camoufox.
|
|
316
326
|
*
|
|
317
327
|
* **Example usage**
|
|
318
328
|
* ```ts
|
|
319
329
|
* postNavigationHooks: [
|
|
320
|
-
* async ({ handleCloudflareChallenge })
|
|
321
|
-
* await handleCloudflareChallenge();
|
|
322
|
-
* },
|
|
330
|
+
* async (context) => ({ response: await context.handleCloudflareChallenge() }),
|
|
323
331
|
* ],
|
|
324
332
|
* ```
|
|
325
333
|
*
|
|
326
334
|
* @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object
|
|
327
335
|
* @param url current URL for request identification, only used for logging
|
|
328
|
-
* @param [session] current session object
|
|
329
336
|
* @param [options]
|
|
330
337
|
*/
|
|
331
|
-
declare function handleCloudflareChallenge(page: Page, url: string,
|
|
338
|
+
declare function handleCloudflareChallenge(page: Page, url: string, options?: HandleCloudflareChallengeOptions): Promise<Response | undefined>;
|
|
332
339
|
/** @internal */
|
|
333
340
|
export interface PlaywrightContextUtils {
|
|
334
341
|
/**
|
|
@@ -480,7 +487,7 @@ export interface PlaywrightContextUtils {
|
|
|
480
487
|
*
|
|
481
488
|
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
482
489
|
*/
|
|
483
|
-
enqueueLinksByClickingElements(options: Omit<EnqueueLinksByClickingElementsOptions, 'page' | '
|
|
490
|
+
enqueueLinksByClickingElements(options: Omit<EnqueueLinksByClickingElementsOptions, 'page' | 'requestManager'>): Promise<BatchAddRequestsResult>;
|
|
484
491
|
/**
|
|
485
492
|
* Compiles a Playwright script into an async function that may be executed at any time
|
|
486
493
|
* by providing it with the following object:
|
|
@@ -510,6 +517,15 @@ export interface PlaywrightContextUtils {
|
|
|
510
517
|
compileScript(scriptString: string, ctx?: Dictionary): CompiledScriptFunction;
|
|
511
518
|
/**
|
|
512
519
|
* Tries to close cookie consent modals on the page. Based on the I Don't Care About Cookies browser extension.
|
|
520
|
+
*
|
|
521
|
+
* Note that this method requires the idcac-playwright package to be installed.
|
|
522
|
+
* Crawlee does not include it by default due to licensing issues.
|
|
523
|
+
*
|
|
524
|
+
* To use this method, please install the package manually by running:
|
|
525
|
+
*
|
|
526
|
+
* ```bash
|
|
527
|
+
* npm install idcac-playwright
|
|
528
|
+
* ```
|
|
513
529
|
*/
|
|
514
530
|
closeCookieModals(): Promise<void>;
|
|
515
531
|
/**
|
|
@@ -519,22 +535,21 @@ export interface PlaywrightContextUtils {
|
|
|
519
535
|
* result in a SessionError which will be automatically retried, so only successful requests will get
|
|
520
536
|
* into the `requestHandler`.
|
|
521
537
|
*
|
|
522
|
-
*
|
|
538
|
+
* On a successfully solved challenge the page is reloaded and the new {@link Response} is returned,
|
|
539
|
+
* which can be returned from the hook to update the crawling context's `response`. For the common case,
|
|
540
|
+
* prefer the pre-wrapped {@link handleCloudflareChallengeHook} hook.
|
|
523
541
|
*
|
|
524
542
|
* **Example usage**
|
|
525
543
|
* ```ts
|
|
526
544
|
* postNavigationHooks: [
|
|
527
|
-
* async ({ handleCloudflareChallenge })
|
|
528
|
-
* await handleCloudflareChallenge();
|
|
529
|
-
* },
|
|
545
|
+
* async (context) => ({ response: await context.handleCloudflareChallenge() }),
|
|
530
546
|
* ],
|
|
531
547
|
* ```
|
|
532
548
|
*
|
|
533
549
|
* @param [options]
|
|
534
550
|
*/
|
|
535
|
-
handleCloudflareChallenge(options?: HandleCloudflareChallengeOptions): Promise<
|
|
551
|
+
handleCloudflareChallenge(options?: HandleCloudflareChallengeOptions): Promise<Response | undefined>;
|
|
536
552
|
}
|
|
537
|
-
export declare function registerUtilsToContext(context: PlaywrightCrawlingContext, crawlerOptions: PlaywrightCrawlerOptions): void;
|
|
538
553
|
export { enqueueLinksByClickingElements };
|
|
539
554
|
/** @internal */
|
|
540
555
|
export declare const playwrightUtils: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"playwright-utils.d.ts","sourceRoot":"","sources":["../../../src/internals/utils/playwright-utils.ts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AAO,CAAC,IAAI,CAAC,CAAC;IAE/D;;;OAGG;IACH,YAAY,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAwCG;IACH,8BAA8B,CAC1B,OAAO,EAAE,IAAI,CAAC,qCAAqC,EAAE,MAAM,GAAG,gBAAgB,CAAC,GAChF,OAAO,CAAC,sBAAsB,CAAC,CAAC;IAEnC;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACH,aAAa,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,UAAU,GAAG,sBAAsB,CAAC;IAE9E;;;;;;;;;;;OAWG;IACH,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEnC;;;;;;;;;;;;;;;;;;;OAmBG;IACH,yBAAyB,CAAC,OAAO,CAAC,EAAE,gCAAgC,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAAC;CACxG;AAED,OAAO,EAAE,8BAA8B,EAAE,CAAC;AAE1C,gBAAgB;AAChB,eAAO,MAAM,eAAe;;;;;;;;;;;;;CAa3B,CAAC"}
|
|
@@ -20,16 +20,14 @@
|
|
|
20
20
|
import { readFile } from 'node:fs/promises';
|
|
21
21
|
import { createRequire } from 'node:module';
|
|
22
22
|
import vm from 'node:vm';
|
|
23
|
-
import { Configuration, KeyValueStore,
|
|
23
|
+
import { Configuration, KeyValueStore, serviceLocator, SessionError, validators } from '@crawlee/browser';
|
|
24
24
|
import { expandShadowRoots, sleep } from '@crawlee/utils';
|
|
25
25
|
import * as cheerio from 'cheerio';
|
|
26
|
-
import { getInjectableScript as getCookieClosingScript } from 'idcac-playwright';
|
|
27
26
|
import ow from 'ow';
|
|
28
27
|
import { LruCache } from '@apify/datastructures';
|
|
29
|
-
import log_ from '@apify/log';
|
|
30
28
|
import { enqueueLinksByClickingElements } from '../enqueue-links/click-elements.js';
|
|
31
29
|
import { RenderingTypePredictor } from './rendering-type-prediction.js';
|
|
32
|
-
const
|
|
30
|
+
const getLog = () => serviceLocator.getChildLog('Playwright Utils');
|
|
33
31
|
const require = createRequire(import.meta.url);
|
|
34
32
|
const jqueryPath = require.resolve('jquery');
|
|
35
33
|
const MAX_INJECT_FILE_CACHE_SIZE = 10;
|
|
@@ -64,7 +62,7 @@ export async function injectFile(page, filePath, options = {}) {
|
|
|
64
62
|
if (options.surviveNavigations) {
|
|
65
63
|
page.on('framenavigated', async () => page
|
|
66
64
|
.evaluate(contents)
|
|
67
|
-
.catch((error) =>
|
|
65
|
+
.catch((error) => getLog().warning('An error occurred during the script injection!', { error })));
|
|
68
66
|
}
|
|
69
67
|
return evalP;
|
|
70
68
|
}
|
|
@@ -123,7 +121,7 @@ export async function gotoExtended(page, request, gotoOptions = {}) {
|
|
|
123
121
|
const isEmpty = (o) => !o || Object.keys(o).length === 0;
|
|
124
122
|
if (method !== 'GET' || payload || !isEmpty(headers)) {
|
|
125
123
|
// This is not deprecated, we use it to log only once.
|
|
126
|
-
|
|
124
|
+
getLog().deprecated('Using other request methods than GET, rewriting headers and adding payloads has a high impact on performance ' +
|
|
127
125
|
'in recent versions of Playwright. Use only when necessary.');
|
|
128
126
|
let wasCalled = false;
|
|
129
127
|
const interceptRequestHandler = async (route) => {
|
|
@@ -144,7 +142,7 @@ export async function gotoExtended(page, request, gotoOptions = {}) {
|
|
|
144
142
|
await route.continue(overrides);
|
|
145
143
|
}
|
|
146
144
|
catch (error) {
|
|
147
|
-
|
|
145
|
+
getLog().debug('Error inside request interceptor', { error });
|
|
148
146
|
}
|
|
149
147
|
return undefined;
|
|
150
148
|
};
|
|
@@ -213,7 +211,7 @@ export async function blockRequests(page, options = {}) {
|
|
|
213
211
|
await client.send('Network.setBlockedURLs', { urls: patternsToBlock });
|
|
214
212
|
}
|
|
215
213
|
catch {
|
|
216
|
-
|
|
214
|
+
getLog().warning('blockRequests() helper is incompatible with non-Chromium browsers.');
|
|
217
215
|
}
|
|
218
216
|
}
|
|
219
217
|
/**
|
|
@@ -249,7 +247,7 @@ export function compileScript(scriptString, context = Object.create(null)) {
|
|
|
249
247
|
func = vm.runInNewContext(funcString, context); // "Secure" the context by removing prototypes, unless custom context is provided.
|
|
250
248
|
}
|
|
251
249
|
catch (err) {
|
|
252
|
-
|
|
250
|
+
getLog().exception(err, 'Cannot compile script!');
|
|
253
251
|
throw err;
|
|
254
252
|
}
|
|
255
253
|
if (typeof func !== 'function')
|
|
@@ -360,7 +358,7 @@ export async function saveSnapshot(page, options = {}) {
|
|
|
360
358
|
}));
|
|
361
359
|
const { key = 'SNAPSHOT', screenshotQuality = 50, saveScreenshot = true, saveHtml = true, keyValueStoreName, config, } = options;
|
|
362
360
|
try {
|
|
363
|
-
const store = await KeyValueStore.open(keyValueStoreName, {
|
|
361
|
+
const store = await KeyValueStore.open(keyValueStoreName ? { name: keyValueStoreName } : null, {
|
|
364
362
|
config: config ?? Configuration.getGlobalConfig(),
|
|
365
363
|
});
|
|
366
364
|
if (saveScreenshot) {
|
|
@@ -403,7 +401,15 @@ export async function parseWithCheerio(page, ignoreShadowRoots = false, ignoreIf
|
|
|
403
401
|
try {
|
|
404
402
|
const iframe = await frame.contentFrame();
|
|
405
403
|
if (iframe) {
|
|
406
|
-
const
|
|
404
|
+
const getIframeHTML = async () => {
|
|
405
|
+
try {
|
|
406
|
+
return iframe.locator('body').first().innerHTML();
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
return iframe.content();
|
|
410
|
+
}
|
|
411
|
+
};
|
|
412
|
+
const contents = await getIframeHTML();
|
|
407
413
|
await frame.evaluate((f, c) => {
|
|
408
414
|
const replacementNode = document.createElement('div');
|
|
409
415
|
replacementNode.innerHTML = c;
|
|
@@ -413,7 +419,7 @@ export async function parseWithCheerio(page, ignoreShadowRoots = false, ignoreIf
|
|
|
413
419
|
}
|
|
414
420
|
}
|
|
415
421
|
catch (error) {
|
|
416
|
-
|
|
422
|
+
getLog().warning(`Failed to extract iframe content: ${error}`);
|
|
417
423
|
}
|
|
418
424
|
}));
|
|
419
425
|
}
|
|
@@ -423,9 +429,34 @@ export async function parseWithCheerio(page, ignoreShadowRoots = false, ignoreIf
|
|
|
423
429
|
const pageContent = html || (await page.content());
|
|
424
430
|
return cheerio.load(pageContent);
|
|
425
431
|
}
|
|
432
|
+
let idcacPlaywright = null;
|
|
433
|
+
async function getIdcacPlaywright() {
|
|
434
|
+
if (idcacPlaywright)
|
|
435
|
+
return idcacPlaywright;
|
|
436
|
+
try {
|
|
437
|
+
idcacPlaywright = await import('idcac-playwright');
|
|
438
|
+
}
|
|
439
|
+
catch (error) {
|
|
440
|
+
getLog().warning(`Failed to import 'idcac-playwright'.
|
|
441
|
+
|
|
442
|
+
We recently made idcac-playwright an optional dependency due to licensing issues.
|
|
443
|
+
To use this feature, please install it manually by running
|
|
444
|
+
|
|
445
|
+
npm install idcac-playwright
|
|
446
|
+
|
|
447
|
+
Original error message follows:
|
|
448
|
+
|
|
449
|
+
${error.message}
|
|
450
|
+
`);
|
|
451
|
+
}
|
|
452
|
+
return idcacPlaywright;
|
|
453
|
+
}
|
|
426
454
|
export async function closeCookieModals(page) {
|
|
427
455
|
ow(page, ow.object.validate(validators.browserPage));
|
|
428
|
-
await
|
|
456
|
+
const idcac = await getIdcacPlaywright();
|
|
457
|
+
if (idcac?.getInjectableScript()) {
|
|
458
|
+
await page.evaluate(idcac.getInjectableScript());
|
|
459
|
+
}
|
|
429
460
|
}
|
|
430
461
|
/**
|
|
431
462
|
* This helper tries to solve the Cloudflare challenge automatically by clicking on the checkbox.
|
|
@@ -434,30 +465,24 @@ export async function closeCookieModals(page) {
|
|
|
434
465
|
* result in a SessionError which will be automatically retried, so only successful requests will get
|
|
435
466
|
* into the `requestHandler`.
|
|
436
467
|
*
|
|
468
|
+
* On a successfully solved challenge the page is reloaded and the new {@link Response} is returned, so
|
|
469
|
+
* it can be propagated back to the crawling context via a hook return value (see
|
|
470
|
+
* {@link handleCloudflareChallengeHook}).
|
|
471
|
+
*
|
|
437
472
|
* Works best with camoufox.
|
|
438
473
|
*
|
|
439
474
|
* **Example usage**
|
|
440
475
|
* ```ts
|
|
441
476
|
* postNavigationHooks: [
|
|
442
|
-
* async ({ handleCloudflareChallenge })
|
|
443
|
-
* await handleCloudflareChallenge();
|
|
444
|
-
* },
|
|
477
|
+
* async (context) => ({ response: await context.handleCloudflareChallenge() }),
|
|
445
478
|
* ],
|
|
446
479
|
* ```
|
|
447
480
|
*
|
|
448
481
|
* @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object
|
|
449
482
|
* @param url current URL for request identification, only used for logging
|
|
450
|
-
* @param [session] current session object
|
|
451
483
|
* @param [options]
|
|
452
484
|
*/
|
|
453
|
-
async function handleCloudflareChallenge(page, url,
|
|
454
|
-
// eslint-disable-next-line dot-notation
|
|
455
|
-
const blockedStatusCodes = session?.['sessionPool']['blockedStatusCodes'];
|
|
456
|
-
// Cloudflare pages are 403, which are blocked by default
|
|
457
|
-
if (blockedStatusCodes?.includes(403)) {
|
|
458
|
-
const idx = blockedStatusCodes.indexOf(403);
|
|
459
|
-
blockedStatusCodes.splice(idx, 1);
|
|
460
|
-
}
|
|
485
|
+
async function handleCloudflareChallenge(page, url, options = {}) {
|
|
461
486
|
options.isBlockedCallback ??= async () => {
|
|
462
487
|
const isBlocked = await page.evaluate(() => {
|
|
463
488
|
return document.querySelector('h1')?.textContent?.trim().includes('Sorry, you have been blocked');
|
|
@@ -481,10 +506,10 @@ async function handleCloudflareChallenge(page, url, session, options = {}) {
|
|
|
481
506
|
};
|
|
482
507
|
if (!(await isChallenge())) {
|
|
483
508
|
await retryBlocked();
|
|
484
|
-
return;
|
|
509
|
+
return undefined;
|
|
485
510
|
}
|
|
486
511
|
const logLevel = options.verbose ? 'info' : 'debug';
|
|
487
|
-
|
|
512
|
+
getLog()[logLevel](`Detected Cloudflare challenge at ${url}, trying to solve it. This can take up to ${10 + (options.sleepSecs ?? 10)} seconds.`);
|
|
488
513
|
const bb = await page
|
|
489
514
|
.evaluate(() => {
|
|
490
515
|
const div = document.querySelector('.main-content div');
|
|
@@ -492,20 +517,27 @@ async function handleCloudflareChallenge(page, url, session, options = {}) {
|
|
|
492
517
|
})
|
|
493
518
|
.catch(() => undefined);
|
|
494
519
|
if (!bb) {
|
|
495
|
-
return;
|
|
520
|
+
return undefined;
|
|
496
521
|
}
|
|
497
522
|
const randomOffset = (range) => {
|
|
498
523
|
return Math.round(100 * range * Math.random()) / 100;
|
|
499
524
|
};
|
|
500
|
-
|
|
501
|
-
|
|
525
|
+
let x = bb.x + 30;
|
|
526
|
+
let y = bb.y + 25;
|
|
502
527
|
// try to click the checkbox every second
|
|
503
528
|
for (let i = 0; i < 10; i++) {
|
|
504
|
-
await sleep(1000);
|
|
529
|
+
await sleep((options.preChallengeSleepSecs ?? 1) * 1000);
|
|
505
530
|
// break early if we are no longer on the CF challenge page
|
|
506
531
|
if (!(await isChallenge())) {
|
|
507
532
|
break;
|
|
508
533
|
}
|
|
534
|
+
if (options.clickPositionCallback) {
|
|
535
|
+
const pos = await options.clickPositionCallback(page);
|
|
536
|
+
if (pos) {
|
|
537
|
+
x = pos.x;
|
|
538
|
+
y = pos.y;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
509
541
|
if (options.clickCallback) {
|
|
510
542
|
await options.clickCallback(page, { x, y });
|
|
511
543
|
continue;
|
|
@@ -513,7 +545,10 @@ async function handleCloudflareChallenge(page, url, session, options = {}) {
|
|
|
513
545
|
// we can click on the text too, so X can be a bit larger
|
|
514
546
|
const xRandomized = x + randomOffset(10);
|
|
515
547
|
const yRandomized = y + randomOffset(10);
|
|
516
|
-
|
|
548
|
+
getLog()[logLevel](`Trying to click on the Cloudflare checkbox at ${url}`, {
|
|
549
|
+
x: xRandomized,
|
|
550
|
+
y: yRandomized,
|
|
551
|
+
});
|
|
517
552
|
await page.mouse.click(xRandomized, yRandomized);
|
|
518
553
|
// sometimes the checkbox is lower (could be caused by a lag when rendering the logo)
|
|
519
554
|
await page.mouse.click(xRandomized, yRandomized + 35);
|
|
@@ -523,40 +558,9 @@ async function handleCloudflareChallenge(page, url, session, options = {}) {
|
|
|
523
558
|
throw new SessionError(`Blocked by Cloudflare when processing ${url}`);
|
|
524
559
|
}
|
|
525
560
|
await retryBlocked();
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
context.injectJQuery = async () => {
|
|
530
|
-
if (context.request.state === RequestState.BEFORE_NAV) {
|
|
531
|
-
log.warning('Using injectJQuery() in preNavigationHooks leads to unstable results. Use it in a postNavigationHook or a requestHandler instead.');
|
|
532
|
-
await injectJQuery(context.page);
|
|
533
|
-
return;
|
|
534
|
-
}
|
|
535
|
-
await injectJQuery(context.page, { surviveNavigations: false });
|
|
536
|
-
};
|
|
537
|
-
context.blockRequests = async (options) => blockRequests(context.page, options);
|
|
538
|
-
context.waitForSelector = async (selector, timeoutMs = 5_000) => {
|
|
539
|
-
const locator = context.page.locator(selector).first();
|
|
540
|
-
await locator.waitFor({ timeout: timeoutMs, state: 'attached' });
|
|
541
|
-
};
|
|
542
|
-
context.parseWithCheerio = async (selector, timeoutMs = 5_000) => {
|
|
543
|
-
if (selector) {
|
|
544
|
-
await context.waitForSelector(selector, timeoutMs);
|
|
545
|
-
}
|
|
546
|
-
return parseWithCheerio(context.page, crawlerOptions.ignoreShadowRoots, crawlerOptions.ignoreIframes);
|
|
547
|
-
};
|
|
548
|
-
context.infiniteScroll = async (options) => infiniteScroll(context.page, options);
|
|
549
|
-
context.saveSnapshot = async (options) => saveSnapshot(context.page, { ...options, config: context.crawler.config });
|
|
550
|
-
context.enqueueLinksByClickingElements = async (options) => enqueueLinksByClickingElements({
|
|
551
|
-
...options,
|
|
552
|
-
page: context.page,
|
|
553
|
-
requestQueue: context.crawler.requestQueue,
|
|
554
|
-
});
|
|
555
|
-
context.compileScript = (scriptString, ctx) => compileScript(scriptString, ctx);
|
|
556
|
-
context.closeCookieModals = async () => closeCookieModals(context.page);
|
|
557
|
-
context.handleCloudflareChallenge = async (options) => {
|
|
558
|
-
return handleCloudflareChallenge(context.page, context.request.url, context.session, options);
|
|
559
|
-
};
|
|
561
|
+
// Reload to obtain a fresh Response without the challenge interstitial, which the caller can
|
|
562
|
+
// propagate back into the crawling context so downstream status-code checks see the new value.
|
|
563
|
+
return (await page.reload()) ?? undefined;
|
|
560
564
|
}
|
|
561
565
|
export { enqueueLinksByClickingElements };
|
|
562
566
|
/** @internal */
|