Skip to content

Commit 80304b4

Browse files
committed
feat: add init script
1 parent 26ad972 commit 80304b4

3 files changed

Lines changed: 108 additions & 2 deletions

File tree

examples/add-init-script.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use chromiumoxide::browser::{Browser, BrowserConfig};
2+
use futures::StreamExt;
3+
4+
#[tokio::main]
5+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6+
tracing_subscriber::fmt::init();
7+
8+
let (browser, mut handler) =
9+
Browser::launch(BrowserConfig::builder().with_head().build()?).await?;
10+
11+
let handle = tokio::spawn(async move {
12+
loop {
13+
let _ = handler.next().await.unwrap();
14+
}
15+
});
16+
17+
let page = browser.new_page("about:blank").await?;
18+
19+
// Add the init script BEFORE navigation
20+
page.add_init_script(
21+
r#"
22+
Object.defineProperty(navigator, 'webdriver', {
23+
get: () => undefined
24+
});
25+
"#,
26+
)
27+
.await?;
28+
29+
// Navigate to a page
30+
page.goto("https://www.wikipedia.org").await?;
31+
page.wait_for_navigation().await?;
32+
33+
page.find_element("h1").await?;
34+
35+
let _html = page.wait_for_navigation().await?.content().await?;
36+
37+
handle.await?;
38+
Ok(())
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use chromiumoxide::browser::{Browser, BrowserConfig};
2+
use futures::StreamExt;
3+
4+
#[tokio::main]
5+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
6+
tracing_subscriber::fmt::init();
7+
8+
let (browser, mut handler) =
9+
Browser::launch(BrowserConfig::builder().with_head().build()?).await?;
10+
11+
let handle = tokio::spawn(async move {
12+
loop {
13+
let _ = handler.next().await.unwrap();
14+
}
15+
});
16+
17+
let page = browser.new_page("about:blank").await?;
18+
19+
// Add the init script BEFORE navigation
20+
page.evaluate_on_new_document(
21+
r#"
22+
Object.defineProperty(navigator, 'webdriver', {
23+
get: () => undefined
24+
});
25+
"#,
26+
)
27+
.await?;
28+
29+
// Navigate to a page
30+
page.goto("https://www.wikipedia.org").await?;
31+
page.wait_for_navigation().await?;
32+
33+
page.find_element("h1").await?;
34+
35+
let _html = page.wait_for_navigation().await?.content().await?;
36+
37+
handle.await?;
38+
Ok(())
39+
}

src/page.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,15 +1146,43 @@ impl Page {
11461146
self.inner.frame_secondary_execution_context(frame_id).await
11471147
}
11481148

1149-
/// Evaluates given script in every frame upon creation (before loading
1150-
/// frame's scripts)
1149+
/// Evaluates given script in every frame upon creation (before loading frame's scripts).
1150+
///
1151+
/// This is equivalent to Playwright's `addInitScript()` or Puppeteer's `evaluateOnNewDocument()`.
1152+
/// Useful for modifying the JavaScript environment before page scripts run, such as:
1153+
/// - Hiding automation detection properties
1154+
/// - Injecting polyfills
1155+
/// - Setting up global variables
1156+
///
1157+
/// # Example
1158+
/// ```
1159+
/// # use chromiumoxide::page::Page;
1160+
/// # async fn example(page: Page) -> Result<(), Box<dyn std::error::Error>> {
1161+
/// // Hide webdriver property for stealth scraping
1162+
/// page.evaluate_on_new_document(r#"
1163+
/// Object.defineProperty(navigator, 'webdriver', {
1164+
/// get: () => undefined
1165+
/// });
1166+
/// "#).await?;
1167+
/// # Ok(())
1168+
/// # }
1169+
/// ```
11511170
pub async fn evaluate_on_new_document(
11521171
&self,
11531172
script: impl Into<AddScriptToEvaluateOnNewDocumentParams>,
11541173
) -> Result<ScriptIdentifier> {
11551174
Ok(self.execute(script.into()).await?.result.identifier)
11561175
}
11571176

1177+
/// Alias for `evaluate_on_new_document` - familiar to Playwright users.
1178+
#[inline]
1179+
pub async fn add_init_script(
1180+
&self,
1181+
script: impl Into<AddScriptToEvaluateOnNewDocumentParams>,
1182+
) -> Result<ScriptIdentifier> {
1183+
self.evaluate_on_new_document(script).await
1184+
}
1185+
11581186
/// Set the content of the frame.
11591187
///
11601188
/// # Example

0 commit comments

Comments
 (0)