Skip to content

Commit

Permalink
Support for multiple cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
gongzhenxing committed Dec 20, 2023
1 parent 9d536ec commit 247b87b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
14 changes: 10 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ export const configSchema = z.object({
outputFileName: z.string(),
/** Optional cookie to be set. E.g. for Cookie Consent */
cookie: z
.object({
name: z.string(),
value: z.string(),
})
.union([
z.object({
name: z.string(),
value: z.string(),
}),
z.array(z.object({
name: z.string(),
value: z.string(),
})),
])
.optional(),
/** Optional function to run for each page found */
onVisitPage: z
Expand Down
16 changes: 9 additions & 7 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ export async function crawl(config: Config) {
// Use the requestHandler to process each of the crawled pages.
async requestHandler({ request, page, enqueueLinks, log, pushData }) {
if (config.cookie) {
// Set the cookie for the specific URL
const cookie = {
name: config.cookie.name,
value: config.cookie.value,
url: request.loadedUrl,
};
await page.context().addCookies([cookie]);
const cookies = (Array.isArray(config.cookie) ? config.cookie : [config.cookie])
.map((cookie)=>{
return {
name:cookie.name,
value:cookie.value,
url:request.loadedUrl
}
});
await page.context().addCookies(cookies);
}

const title = await page.title();
Expand Down

0 comments on commit 247b87b

Please sign in to comment.