Skip to content

Commit 1dc78de

Browse files
fix: enable JSON responses
1 parent 287d37e commit 1dc78de

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

checker-cheerio/src/handlePage.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ export async function handlePage(
1111
input: CheerioActorInput,
1212
requestQueue: RequestQueue,
1313
state: ActorCheckDetailedOutput,
14-
{ request, $, body, response, enqueueLinks }: CheerioCheckerHandlePageInputs,
14+
{ request, $, body, response, enqueueLinks, json }: CheerioCheckerHandlePageInputs,
1515
) {
1616
/** @type {string | undefined} */
1717
let htmlUrl;
1818

1919
if (input.saveSnapshot) {
2020
const key = `SNAPSHOT-${Math.random().toString()}`;
21-
await Actor.setValue(`${key}.html`, body, { contentType: 'text/html' });
21+
if (json) {
22+
await Actor.setValue(key, json);
23+
} else {
24+
await Actor.setValue(`${key}.html`, body, { contentType: 'text/html' });
25+
}
2226
htmlUrl = `https://api.apify.com/v2/key-value-stores/${Actor.getEnv().defaultKeyValueStoreId}/records/${key}.html?disableRedirect=true`;
2327
}
2428

@@ -30,15 +34,18 @@ export async function handlePage(
3034
state.statusCodes[statusCode!].push({ url: request.url, htmlUrl });
3135

3236
const captchas: string[] = [];
33-
const testResult = testHtml($);
37+
// We don't have $ for JSON responses nor we can recognize captchas from it
38+
if ($) {
39+
const testResult = testHtml($);
3440

35-
for (const testResultEntry of Object.entries(testResult)) {
36-
const wasFound = testResultEntry[1];
37-
const testCase = testResultEntry[0] as 'accessDenied' | 'distilCaptcha' | 'recaptcha' | 'hCaptcha';
38-
if (wasFound) {
39-
captchas.push(testCase);
41+
for (const testResultEntry of Object.entries(testResult)) {
42+
const wasFound = testResultEntry[1];
43+
const testCase = testResultEntry[0] as 'accessDenied' | 'distilCaptcha' | 'recaptcha' | 'hCaptcha';
44+
if (wasFound) {
45+
captchas.push(testCase);
4046

41-
state[testCase].push({ url: request.url, htmlUrl });
47+
state[testCase].push({ url: request.url, htmlUrl });
48+
}
4249
}
4350
}
4451

@@ -59,7 +66,7 @@ export async function handlePage(
5966
const info = await requestQueue.getInfo();
6067

6168
// Only queue up more requests in the queue if we should (this should avoid excessive queue writes)
62-
if (input.maxNumberOfPagesCheckedPerDomain > info!.totalRequestCount) {
69+
if (input.maxNumberOfPagesCheckedPerDomain > info!.totalRequestCount && !!$) {
6370
await enqueueLinks({
6471
selector: input.linkSelector,
6572
pseudoUrls: input.pseudoUrls.map(

0 commit comments

Comments
 (0)