Skip to content
Snippets Groups Projects
Commit 567ab0b0 authored by Zephyr's avatar Zephyr
Browse files

fix: correctly set cookies

parent 2ca876e6
Branches
Tags
No related merge requests found
......@@ -104,13 +104,11 @@ export async function crawl(config: Config) {
// Uncomment this option to see the browser window.
// headless: false,
preNavigationHooks: [
// Abort requests for certain resource types
async ({ request, page, log }) => {
// If there are no resource exclusions, return
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? [];
if (RESOURCE_EXCLUSTIONS.length === 0) {
return;
}
// Abort requests for certain resource types and add cookies
async (crawlingContext, _gotoOptions) => {
const { request, page, log } = crawlingContext;
// Add cookies to the page
// Because the crawler has not yet navigated to the page, so the loadedUrl is always undefined. Use the request url instead.
if (config.cookie) {
const cookies = (
Array.isArray(config.cookie) ? config.cookie : [config.cookie]
......@@ -118,11 +116,16 @@ export async function crawl(config: Config) {
return {
name: cookie.name,
value: cookie.value,
url: request.loadedUrl,
url: request.url,
};
});
await page.context().addCookies(cookies);
}
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? [];
// If there are no resource exclusions, return
if (RESOURCE_EXCLUSTIONS.length === 0) {
return;
}
await page.route(
`**\/*.{${RESOURCE_EXCLUSTIONS.join()}}`,
(route) => route.abort("aborted"),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment