Skip to content

Commit 247b87b

Browse files
author
gongzhenxing
committed
Support for multiple cookies
1 parent 9d536ec commit 247b87b

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/config.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ export const configSchema = z.object({
3737
outputFileName: z.string(),
3838
/** Optional cookie to be set. E.g. for Cookie Consent */
3939
cookie: z
40-
.object({
41-
name: z.string(),
42-
value: z.string(),
43-
})
40+
.union([
41+
z.object({
42+
name: z.string(),
43+
value: z.string(),
44+
}),
45+
z.array(z.object({
46+
name: z.string(),
47+
value: z.string(),
48+
})),
49+
])
4450
.optional(),
4551
/** Optional function to run for each page found */
4652
onVisitPage: z

src/core.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export async function crawl(config: Config) {
5656
// Use the requestHandler to process each of the crawled pages.
5757
async requestHandler({ request, page, enqueueLinks, log, pushData }) {
5858
if (config.cookie) {
59-
// Set the cookie for the specific URL
60-
const cookie = {
61-
name: config.cookie.name,
62-
value: config.cookie.value,
63-
url: request.loadedUrl,
64-
};
65-
await page.context().addCookies([cookie]);
59+
const cookies = (Array.isArray(config.cookie) ? config.cookie : [config.cookie])
60+
.map((cookie)=>{
61+
return {
62+
name:cookie.name,
63+
value:cookie.value,
64+
url:request.loadedUrl
65+
}
66+
});
67+
await page.context().addCookies(cookies);
6668
}
6769

6870
const title = await page.title();

0 commit comments

Comments
 (0)