@@ -3,21 +3,49 @@ import { log } from './logger.js';
33import { Rec , HttpMethod } from './types.js' ;
44import { ScrapeConfigError } from './errors.js' ;
55
6- type ScreenshotFlags = "load_images" | "dark_mode" | "block_banners" | "high_quality" | "print_media_format" ;
7- type Format = "raw" | "json" | "text" | "markdown" | "clean_html" ;
6+ export enum ScreenshotFlags {
7+ /**
8+ Options to customize the screenshot behavior
9+ Attributes:
10+ LOAD_IMAGES: Enable image rendering with the request, add extra usage for the bandwidth consumed.
11+ DARK_MODE: Enable dark mode display.
12+ BLOCK_BANNERS: Block cookies banners and overlay that cover the screen.
13+ PRINT_MEDIA_FORMAT: Render the page in the print mode.
14+ */
15+ LOAD_IMAGES = 'load_images' ,
16+ DARK_MODE = 'dark_mode' ,
17+ BLOCK_BANNERS = 'block_banners' ,
18+ PRINT_MEDIA_FORMAT = 'print_media_format' ,
19+ HIGH_QUALITY = 'high_quality' ,
20+ }
21+
22+ export enum Format {
23+ /**
24+ Format of the scraped content.
25+ Attributes:
26+ JSON: JSON format.
27+ TEXT: Text format.
28+ MARKDOWN: Markdown format.
29+ CLEAN_HTML: Clean HTML format.
30+ */
31+ JSON = 'json' ,
32+ TEXT = 'text' ,
33+ MARKDOWN = 'markdown' ,
34+ CLEAN_HTML = 'clean_html' ,
35+ }
836
937export class ScrapeConfig {
1038 static PUBLIC_DATACENTER_POOL = 'public_datacenter_pool' ;
1139 static PUBLIC_RESIDENTIAL_POOL = 'public_residential_pool' ;
12-
40+
1341 url : string ;
1442 retry = true ;
1543 method : HttpMethod = 'GET' ;
1644 country ?: string = null ;
1745 render_js = false ;
1846 cache = false ;
1947 cache_clear = false ;
20- cost_budget ?: number = null
48+ cost_budget ?: number = null ;
2149 ssl = false ;
2250 dns = false ;
2351 asp = false ;
@@ -85,6 +113,17 @@ export class ScrapeConfig {
85113 lang ?: string [ ] ;
86114 auto_scroll ?: boolean ;
87115 } ) {
116+ if ( options . format && ! Object . values ( Format ) . includes ( options . format ) ) {
117+ throw new ScrapeConfigError ( `Invalid format param value: ${ options . format } ` ) ;
118+ }
119+ this . format = options . format ?? this . format ;
120+ if ( options . screenshot_flags ) {
121+ options . screenshot_flags . forEach ( ( flag ) => {
122+ if ( ! Object . values ( ScreenshotFlags ) . includes ( flag ) ) {
123+ throw new ScrapeConfigError ( `Invalid screenshot_flags param value: ${ flag } ` ) ;
124+ }
125+ } ) ;
126+ }
88127 this . url = options . url ;
89128 this . retry = options . retry ?? this . retry ;
90129 this . method = options . method ?? this . method ;
@@ -209,7 +248,7 @@ export class ScrapeConfig {
209248 } else {
210249 if ( this . screenshot_flags ) {
211250 log . warn ( 'Params "screenshot_flags" is ignored. Works only if screenshots is enabled' ) ;
212- }
251+ }
213252 }
214253 if ( this . auto_scroll !== null ) {
215254 params . auto_scroll = this . auto_scroll ;
0 commit comments