Skip to content

Commit c34bde5

Browse files
authored
Merge pull request #116 from little873/multiple-cookies
Support for multiple cookies
2 parents 3ec5fee + 581cc77 commit c34bde5

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/config.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ 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(
46+
z.object({
47+
name: z.string(),
48+
value: z.string(),
49+
}),
50+
),
51+
])
4452
.optional(),
4553
/** Optional function to run for each page found */
4654
onVisitPage: z

src/core.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ export async function crawl(config: Config) {
5555
const crawler = new PlaywrightCrawler({
5656
// Use the requestHandler to process each of the crawled pages.
5757
async requestHandler({ request, page, enqueueLinks, log, pushData }) {
58-
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]);
66-
}
67-
6858
const title = await page.title();
6959
pageCounter++;
7060
log.info(
@@ -108,12 +98,24 @@ export async function crawl(config: Config) {
10898
// headless: false,
10999
preNavigationHooks: [
110100
// Abort requests for certain resource types
111-
async ({ page, log }) => {
101+
async ({ request, page, log }) => {
112102
// If there are no resource exclusions, return
113103
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? [];
114104
if (RESOURCE_EXCLUSTIONS.length === 0) {
115105
return;
116106
}
107+
if (config.cookie) {
108+
const cookies = (
109+
Array.isArray(config.cookie) ? config.cookie : [config.cookie]
110+
).map((cookie) => {
111+
return {
112+
name: cookie.name,
113+
value: cookie.value,
114+
url: request.loadedUrl,
115+
};
116+
});
117+
await page.context().addCookies(cookies);
118+
}
117119
await page.route(`**\/*.{${RESOURCE_EXCLUSTIONS.join()}}`, (route) =>
118120
route.abort("aborted"),
119121
);

0 commit comments

Comments
 (0)