Browse Source

add optional cookie

samfromaway 1 year ago
parent
commit
3721069c10
2 changed files with 13 additions and 0 deletions
  1. 2 0
      config.ts
  2. 11 0
      src/main.ts

+ 2 - 0
config.ts

@@ -11,6 +11,8 @@ type Config = {
   maxPagesToCrawl: number;
   /** File name for the finished data */
   outputFileName: string;
+  /** Optional cookie to be set. E.g. for Cookie Consent */
+  cookie?: {name: string; value: string}
   /** Optional function to run for each page found */
   onVisitPage?: (options: {
     page: Page;

+ 11 - 0
src/main.ts

@@ -18,6 +18,17 @@ if (process.env.NO_CRAWL !== "true") {
   const crawler = new PlaywrightCrawler({
     // 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 title = await page.title();
       log.info(`Crawling ${request.loadedUrl}...`);