Skip to content

Commit

Permalink
Set cookies in advance and modify code style
Browse files Browse the repository at this point in the history
  • Loading branch information
gongzhenxing committed Dec 25, 2023
1 parent ac4e168 commit 24d687d
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ export async function crawl(config: Config) {
const crawler = new PlaywrightCrawler({
// Use the requestHandler to process each of the crawled pages.
async requestHandler({ request, page, enqueueLinks, log, pushData }) {
if (config.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();
pageCounter++;
log.info(
Expand Down Expand Up @@ -110,12 +98,24 @@ export async function crawl(config: Config) {
// headless: false,
preNavigationHooks: [
// Abort requests for certain resource types
async ({ page, log }) => {
async ({ request, page, log }) => {
// If there are no resource exclusions, return
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? [];
if (RESOURCE_EXCLUSTIONS.length === 0) {
return;
}
if (config.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);
}
await page.route(`**\/*.{${RESOURCE_EXCLUSTIONS.join()}}`, (route) =>
route.abort("aborted"),
);
Expand Down

0 comments on commit 24d687d

Please sign in to comment.