Skip to main content

profile_reuse/
profile_reuse.rs

1//! Profile reuse demo — same profile across sessions keeps cookies + fingerprint.
2//!
3//! Run: CLAWSER_CHROME_PATH=out/Default/chrome.exe cargo run --manifest-path clawser-browser/Cargo.toml --example profile_reuse
4
5use clawser_browser::Browser;
6
7const PROFILE_INDEX: usize = 42;
8const SEED: u64 = 12345;
9
10#[tokio::main]
11async fn main() {
12    println!("=== Profile Reuse Demo ===\n");
13
14    // --- Session 1: first visit ---
15    println!("[Session 1] First visit with profile({}, {})", PROFILE_INDEX, SEED);
16    let browser = Browser::builder()
17        .headful()
18        .profile(PROFILE_INDEX, SEED)
19        .build().await
20        .expect("failed to create browser");
21
22    let page = browser.navigate("https://www.youtube.com").await
23        .expect("navigate failed");
24    tokio::time::sleep(std::time::Duration::from_secs(3)).await;
25
26    let title = page.js("document.title").await.unwrap_or_default();
27    let cookies = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
28    let fingerprint = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
29
30    println!("    Title: {}", title);
31    println!("    Fingerprint: {}", fingerprint);
32    println!("    Cookies: {} total", cookies.len());
33    for c in cookies.iter().take(5) {
34        println!("      {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
35    }
36
37    println!("    Shutting down session 1...\n");
38    browser.shutdown().await.expect("shutdown failed");
39
40    // --- Session 2: same profile, cookies should persist ---
41    println!("[Session 2] Reusing profile({}, {}) — cookies should persist", PROFILE_INDEX, SEED);
42    let browser = Browser::builder()
43        .headful()
44        .profile(PROFILE_INDEX, SEED)
45        .build().await
46        .expect("failed to create browser");
47
48    // Check cookies BEFORE navigating — they should be in the user-data-dir
49    let page = browser.navigate("https://www.youtube.com").await
50        .expect("navigate failed");
51    tokio::time::sleep(std::time::Duration::from_secs(3)).await;
52
53    let cookies2 = browser.cookies("https://www.youtube.com").await.unwrap_or_default();
54    let fingerprint2 = page.js("navigator.hardwareConcurrency + 'c|' + screen.width + 'x' + screen.height + '|' + Intl.DateTimeFormat().resolvedOptions().timeZone").await.unwrap_or_default();
55
56    println!("    Fingerprint: {}", fingerprint2);
57    println!("    Cookies: {} total", cookies2.len());
58    for c in cookies2.iter().take(5) {
59        println!("      {} = {}...", c.name, &c.value[..c.value.len().min(30)]);
60    }
61
62    // Verify
63    println!("\n--- Verification ---");
64    println!("    Fingerprint match: {}", fingerprint == fingerprint2);
65    println!("    Cookies persisted: {}", cookies2.len() >= cookies.len());
66
67    browser.shutdown().await.expect("shutdown failed");
68    println!("\n=== DONE ===");
69}