webshot 0.2.0

A command-line tool for automated website screenshots and web scraping
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
use crate::config::{Config, ScreenshotConfig};
use crate::error::{Result, WebshotError};
use crate::screenshot::{ImageFormat, ScreenshotOptions};
use headless_chrome::protocol::cdp::Page;
use headless_chrome::types::PrintToPdfOptions;
use headless_chrome::{Browser as ChromeBrowser, LaunchOptions, Tab};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;
use tracing::{debug, info, warn};

/// Browser automation wrapper
pub struct Browser {
    browser: ChromeBrowser,
    javascript_enabled: bool,
}

impl Browser {
    /// Create a new browser instance
    pub async fn new(
        chrome_path: Option<PathBuf>,
        chrome_flags: Vec<String>,
        javascript_enabled: bool,
    ) -> Result<Self> {
        info!("Launching browser...");

        let mut args_str = vec![
            "--no-sandbox",
            "--disable-gpu", 
            "--disable-dev-shm-usage",
            "--disable-setuid-sandbox",
            "--no-first-run",
        ];

        // Collect additional flags
        let mut flag_strings = Vec::new();
        for flag in chrome_flags {
            flag_strings.push(flag);
        }

        // Disable JavaScript if requested
        if !javascript_enabled {
            flag_strings.push("--disable-javascript".to_string());
        }

        // Convert to OsStr refs
        for flag in &flag_strings {
            args_str.push(flag.as_str());
        }

        let args_os: Vec<std::ffi::OsString> = args_str.iter().map(|s| (*s).into()).collect();
        let args_refs: Vec<&std::ffi::OsStr> = args_os.iter().map(|s| s.as_os_str()).collect();
        
        let launch_options = if let Some(path) = chrome_path {
            LaunchOptions::default_builder()
                .headless(true)
                .sandbox(false)
                .args(args_refs)
                .path(Some(path))
                .build()
                .unwrap()
        } else {
            LaunchOptions::default_builder()
                .headless(true)
                .sandbox(false)
                .args(args_refs)
                .build()
                .unwrap()
        };

        let browser = ChromeBrowser::new(launch_options)
            .map_err(|e| WebshotError::BrowserLaunch(e.to_string()))?;

        debug!("Browser launched successfully");

        Ok(Self {
            browser,
            javascript_enabled,
        })
    }

    /// Take a screenshot of a webpage
    pub async fn screenshot<P: AsRef<Path>>(
        &self,
        url: &str,
        output_path: P,
        options: &ScreenshotOptions,
    ) -> Result<()> {
        options.validate()?;

        let tab = self.browser.new_tab()
            .map_err(|e| WebshotError::Tab(e.to_string()))?;
        self.setup_tab(&tab, options).await?;

        info!("Navigating to: {}", url);
        tab.navigate_to(url)
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;

        // Wait for page load
        tab.wait_until_navigated()
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;

        // Execute custom JavaScript if provided
        if let Some(script) = &options.javascript {
            if self.javascript_enabled {
                info!("Executing JavaScript: {}", script);
                tab.evaluate(script, false)
                    .map_err(|e| WebshotError::javascript(e.to_string()))?;
            } else {
                warn!("JavaScript disabled, skipping script execution");
            }
        }

        // Wait for specific element if requested
        if let Some(selector) = &options.wait_for {
            info!("Waiting for element: {}", selector);
            self.wait_for_element(&tab, selector, options.timeout).await?;
        }

        // Additional wait time
        if options.wait > 0 {
            info!("Waiting {} seconds before screenshot", options.wait);
            sleep(Duration::from_secs(options.wait)).await;
        }

        let format = options.output_format(&output_path)?;

        match format {
            ImageFormat::Pdf => {
                return Err(WebshotError::screenshot(
                    "PDF generation not supported in screenshot method, use pdf() method instead",
                ));
            }
            ImageFormat::Png | ImageFormat::Jpeg | ImageFormat::WebP => {
                self.take_image_screenshot(&tab, &output_path, options, format)
                    .await?;
            }
        }

        info!("Screenshot saved to: {}", output_path.as_ref().display());
        Ok(())
    }

    /// Generate a PDF from a webpage
    pub async fn pdf<P: AsRef<Path>>(
        &self,
        url: &str,
        output_path: P,
        _format: &str,
        landscape: bool,
        background: bool,
        scale: f64,
        javascript: Option<String>,
        wait_for: Option<String>,
        timeout: u64,
        user_agent: Option<String>,
    ) -> Result<()> {
        let tab = self.browser.new_tab()
            .map_err(|e| WebshotError::Tab(e.to_string()))?;

        // Set up the tab
        if let Some(user_agent) = user_agent {
            tab.set_user_agent(&user_agent, None, None)
                .map_err(|e| WebshotError::Browser(e.into()))?;
        }

        info!("Navigating to: {}", url);
        tab.navigate_to(url)
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;
        tab.wait_until_navigated()
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;

        // Execute custom JavaScript if provided
        if let Some(script) = &javascript {
            if self.javascript_enabled {
                info!("Executing JavaScript: {}", script);
                tab.evaluate(&script, false)
                    .map_err(|e| WebshotError::javascript(e.to_string()))?;
            } else {
                warn!("JavaScript disabled, skipping script execution");
            }
        }

        // Wait for specific element if requested
        if let Some(selector) = &wait_for {
            info!("Waiting for element: {}", selector);
            self.wait_for_element(&tab, selector, timeout).await?;
        }

        info!("Generating PDF...");

        let pdf_options = PrintToPdfOptions {
            landscape: Some(landscape),
            display_header_footer: Some(false),
            print_background: Some(background),
            scale: Some(scale),
            paper_width: None,
            paper_height: None,
            margin_top: None,
            margin_bottom: None,
            margin_left: None,
            margin_right: None,
            page_ranges: None,
            ignore_invalid_page_ranges: None,
            header_template: None,
            footer_template: None,
            prefer_css_page_size: Some(true),
            transfer_mode: None,
            generate_document_outline: Some(false),
            generate_tagged_pdf: Some(false),
        };

        let pdf_data = tab.print_to_pdf(Some(pdf_options))
            .map_err(|e| WebshotError::pdf(e.to_string()))?;
        std::fs::write(&output_path, pdf_data)?;

        info!("PDF saved to: {}", output_path.as_ref().display());
        Ok(())
    }

    /// Extract text content from a webpage
    pub async fn extract_text(
        &self,
        url: &str,
        selector: Option<String>,
        javascript: Option<String>,
        wait_for: Option<String>,
        timeout: u64,
        user_agent: Option<String>,
    ) -> Result<String> {
        let tab = self.browser.new_tab()
            .map_err(|e| WebshotError::Tab(e.to_string()))?;

        // Set up the tab
        if let Some(user_agent) = user_agent {
            tab.set_user_agent(&user_agent, None, None)
                .map_err(|e| WebshotError::Browser(e.into()))?;
        }

        info!("Navigating to: {}", url);
        tab.navigate_to(url)
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;
        tab.wait_until_navigated()
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;

        // Execute custom JavaScript if provided
        if let Some(script) = &javascript {
            if self.javascript_enabled {
                info!("Executing JavaScript: {}", script);
                tab.evaluate(&script, false)
                    .map_err(|e| WebshotError::javascript(e.to_string()))?;
            } else {
                warn!("JavaScript disabled, skipping script execution");
            }
        }

        // Wait for specific element if requested
        if let Some(selector_str) = &wait_for {
            info!("Waiting for element: {}", selector_str);
            self.wait_for_element(&tab, selector_str, timeout).await?;
        }

        let text = if let Some(selector_str) = selector {
            info!("Extracting text from element: {}", selector_str);
            let element = tab.find_element(&selector_str)
                .map_err(|_e| WebshotError::ElementNotFound {
                    selector: selector_str,
                })?;
            element.get_inner_text()
                .map_err(|e| WebshotError::Browser(e.into()))?
        } else {
            info!("Extracting text from entire page");
            tab.get_content()
                .map_err(|e| WebshotError::Browser(e.into()))?
        };

        Ok(text)
    }

    /// Process multiple screenshots from configuration
    pub async fn process_config(
        &self,
        config: &Config,
        output_dir: Option<PathBuf>,
        parallel: usize,
    ) -> Result<()> {
        config.validate()?;

        info!(
            "Processing {} screenshots with {} parallel tasks",
            config.screenshots.len(),
            parallel
        );

        use futures::stream::{self, StreamExt};

        let semaphore = Arc::new(tokio::sync::Semaphore::new(parallel));

        let tasks = config.screenshots.iter().map(|screenshot_config| {
            let semaphore = semaphore.clone();
            let screenshot_config = screenshot_config.clone();
            let output_dir = output_dir.clone();

            async move {
                let _permit = semaphore.acquire().await.unwrap();
                self.process_single_screenshot(screenshot_config, output_dir)
                    .await
            }
        });

        let results: Vec<Result<()>> = stream::iter(tasks).buffer_unordered(parallel).collect().await;

        // Check for errors
        for (i, result) in results.into_iter().enumerate() {
            if let Err(e) = result {
                warn!("Screenshot {} failed: {}", i, e);
            }
        }

        Ok(())
    }

    async fn setup_tab(&self, tab: &Tab, options: &ScreenshotOptions) -> Result<()> {
        // Set viewport using emulation
        tab.set_default_timeout(std::time::Duration::from_secs(options.timeout));

        tab.call_method(headless_chrome::protocol::cdp::Emulation::SetDeviceMetricsOverride {
            width: options.width,
            height: options.height,
            device_scale_factor: options.device_scale_factor(),
            mobile: false,
            scale: None,
            screen_width: None,
            screen_height: None,
            position_x: None,
            position_y: None,
            dont_set_visible_size: None,
            screen_orientation: None,
            viewport: None,
            display_feature: None,
            device_posture: None,
        }).map_err(|e| WebshotError::Browser(e.into()))?;

        // Set user agent if provided
        if let Some(user_agent) = &options.user_agent {
            tab.set_user_agent(user_agent, None, None)
                .map_err(|e| WebshotError::Browser(e.into()))?;
        }

        Ok(())
    }

    async fn wait_for_element(&self, tab: &Tab, selector: &str, timeout: u64) -> Result<()> {
        let start = std::time::Instant::now();
        let timeout_duration = Duration::from_secs(timeout);

        loop {
            if start.elapsed() > timeout_duration {
                return Err(WebshotError::timeout(format!(
                    "waiting for element: {}",
                    selector
                )));
            }

            if tab.find_element(selector).is_ok() {
                debug!("Element found: {}", selector);
                return Ok(());
            }

            sleep(Duration::from_millis(100)).await;
        }
    }

    async fn take_image_screenshot<P: AsRef<Path>>(
        &self,
        tab: &Tab,
        output_path: P,
        options: &ScreenshotOptions,
        format: ImageFormat,
    ) -> Result<()> {
        let screenshot_data = if let Some(selector) = &options.selector {
            info!("Taking element screenshot: {}", selector);
            let element = tab
                .find_element(selector)
                .map_err(|_e| WebshotError::ElementNotFound {
                    selector: selector.clone(),
                })?;
            element.capture_screenshot(Page::CaptureScreenshotFormatOption::Png)
                .map_err(|e| WebshotError::screenshot(e.to_string()))?
        } else {
            info!("Taking full page screenshot");
            tab.capture_screenshot(
                Page::CaptureScreenshotFormatOption::Png,
                None,
                None,
                true,
            ).map_err(|e| WebshotError::screenshot(e.to_string()))?
        };

        match format {
            ImageFormat::Png => {
                std::fs::write(&output_path, screenshot_data)?;
            }
            ImageFormat::Jpeg => {
                // Convert PNG to JPEG
                let img = image::load_from_memory(&screenshot_data)?;
                let mut output = std::fs::File::create(&output_path)?;
                let quality = options.quality.unwrap_or(90);
                
                let encoder = image::codecs::jpeg::JpegEncoder::new_with_quality(&mut output, quality);
                img.write_with_encoder(encoder)?;
            }
            ImageFormat::WebP => {
                // Convert PNG to WebP
                let img = image::load_from_memory(&screenshot_data)?;
                let mut output = std::fs::File::create(&output_path)?;
                
                let encoder = image::codecs::webp::WebPEncoder::new_lossless(&mut output);
                img.write_with_encoder(encoder)?;
            }
            ImageFormat::Pdf => {
                return Err(WebshotError::screenshot(
                    "PDF format should be handled by pdf() method",
                ));
            }
        }

        Ok(())
    }

    async fn process_single_screenshot(
        &self,
        config: ScreenshotConfig,
        output_dir: Option<PathBuf>,
    ) -> Result<()> {
        let tab = self.browser.new_tab()
            .map_err(|e| WebshotError::Tab(e.to_string()))?;

        // Determine output path
        let output_path = if let Some(dir) = output_dir {
            dir.join(&config.output)
        } else {
            config.output.clone()
        };

        // Create parent directories if they don't exist
        if let Some(parent) = output_path.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let options = ScreenshotOptions {
            width: config.width,
            height: config.height,
            selector: config.selector.clone(),
            javascript: config.javascript.clone(),
            wait_for: config.wait_for.clone(),
            timeout: config.timeout,
            retina: config.retina,
            quality: config.quality,
            wait: config.wait,
            user_agent: config.user_agent.clone(),
        };

        self.setup_tab(&tab, &options).await?;

        info!("Processing: {} -> {}", config.url, output_path.display());

        // Set cookies if any
        for cookie in &config.cookies {
            let cookie_param = headless_chrome::protocol::cdp::Network::CookieParam {
                name: cookie.name.clone(),
                value: cookie.value.clone(),
                url: None,
                domain: cookie.domain.clone(),
                path: cookie.path.clone(),
                secure: cookie.secure,
                http_only: cookie.http_only,
                same_site: None,
                expires: None,
                priority: None,
                same_party: None,
                source_scheme: None,
                source_port: None,
                partition_key: None,
            };
            tab.set_cookies(vec![cookie_param])
                .map_err(|e| WebshotError::Browser(e.into()))?;
        }

        // Set custom headers
        if !config.headers.is_empty() {
            let headers: std::collections::HashMap<&str, &str> = config
                .headers
                .iter()
                .map(|(k, v)| (k.as_str(), v.as_str()))
                .collect();
            tab.set_extra_http_headers(headers)
                .map_err(|e| WebshotError::Browser(e.into()))?;
        }

        // Handle authentication
        if let Some(auth) = &config.auth {
            tab.authenticate(Some(auth.username.clone()), Some(auth.password.clone()))
                .map_err(|e| WebshotError::Browser(e.into()))?;
        }

        // Navigate and process
        tab.navigate_to(&config.url)
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;
        tab.wait_until_navigated()
            .map_err(|e| WebshotError::Navigation(e.to_string()))?;

        // Execute JavaScript
        if let Some(script) = &config.javascript {
            if self.javascript_enabled {
                tab.evaluate(script, false)
                    .map_err(|e| WebshotError::javascript(e.to_string()))?;
            }
        }

        // Wait for element
        if let Some(selector) = &config.wait_for {
            self.wait_for_element(&tab, selector, config.timeout).await?;
        }

        // Wait before screenshot
        if config.wait > 0 {
            sleep(Duration::from_secs(config.wait)).await;
        }

        // Take screenshot
        let format = options.output_format(&output_path)?;
        match format {
            ImageFormat::Pdf => {
                let pdf_options = PrintToPdfOptions {
                    landscape: Some(false),
                    display_header_footer: Some(false),
                    print_background: Some(true),
                    scale: Some(1.0),
                    paper_width: None,
                    paper_height: None,
                    margin_top: None,
                    margin_bottom: None,
                    margin_left: None,
                    margin_right: None,
                    page_ranges: None,
                    ignore_invalid_page_ranges: None,
                    header_template: None,
                    footer_template: None,
                    prefer_css_page_size: Some(true),
                    transfer_mode: None,
                    generate_document_outline: Some(false),
                    generate_tagged_pdf: Some(false),
                };

                let pdf_data = tab.print_to_pdf(Some(pdf_options))
                    .map_err(|e| WebshotError::pdf(e.to_string()))?;
                std::fs::write(&output_path, pdf_data)?;
            }
            _ => {
                self.take_image_screenshot(&tab, &output_path, &options, format)
                    .await?;
            }
        }

        Ok(())
    }
}