feat: expose X-Locale parameter

This commit is contained in:
Zhaofeng Miao 2024-08-20 16:14:48 +08:00
parent fb5bd58ee4
commit de50c93825
3 changed files with 36 additions and 0 deletions

View File

@ -1106,6 +1106,7 @@ ${suffixMixins.length ? `\n${suffixMixins.join('\n\n')}\n` : ''}`;
overrideUserAgent: opts.userAgent,
timeoutMs: opts.timeout ? opts.timeout * 1000 : undefined,
withIframe: opts.withIframe,
locale: opts.locale,
};
return crawlOpts;

View File

@ -111,6 +111,11 @@ import { parseString as parseSetCookieString } from 'set-cookie-parser';
in: 'header',
schema: { type: 'string' }
},
'X-Locale': {
description: 'Specify browser locale for the page.',
in: 'header',
schema: { type: 'string' }
}
}
}
}
@ -188,6 +193,9 @@ export class CrawlerOptions extends AutoCastable {
})
timeout?: number | null;
@Prop()
locale?: string;
static override from(input: any) {
const instance = super.from(input) as CrawlerOptions;
const ctx = Reflect.get(input, RPC_CALL_ENVIRONMENT) as {
@ -200,6 +208,11 @@ export class CrawlerOptions extends AutoCastable {
instance.respondWith = customMode;
}
const locale = ctx?.req.get('x-locale');
if (locale !== undefined) {
instance.locale = locale;
}
const withGeneratedAlt = ctx?.req.get('x-with-generated-alt');
if (withGeneratedAlt !== undefined) {
instance.withGeneratedAlt = Boolean(withGeneratedAlt);

View File

@ -68,6 +68,7 @@ export interface ScrappingOptions {
minIntervalMs?: number;
overrideUserAgent?: string;
timeoutMs?: number;
locale?: string;
}
@ -472,6 +473,27 @@ document.addEventListener('load', handlePageLoad);
const page = await this.getNextPage();
const sn = this.snMap.get(page);
this.logger.info(`Page ${sn}: Scraping ${url}`, { url });
this.logger.info(`Locale setting: ${options?.locale}`);
if (options?.locale) {
await page.setExtraHTTPHeaders({
'Accept-Language': options?.locale
});
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, "language", {
get: function() {
return options?.locale;
}
});
Object.defineProperty(navigator, "languages", {
get: function() {
return [options?.locale];
}
});
})
}
if (options?.proxyUrl) {
await page.useProxy(options.proxyUrl);
}