-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an exmple how to use js bindings (#224)
* Added an exmple how to use js bindings * rustmft --------- Co-authored-by: Matthias Seitz <[email protected]>
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use chromiumoxide::browser::{Browser, BrowserConfig}; | ||
use chromiumoxide_cdp::cdp::js_protocol::runtime::{AddBindingParams, EventBindingCalled}; | ||
use futures::StreamExt; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
tracing_subscriber::fmt::init(); | ||
|
||
let (mut browser, mut handler) = | ||
Browser::launch(BrowserConfig::builder().with_head().build()?).await?; | ||
|
||
let handle = async_std::task::spawn(async move { | ||
while let Some(h) = handler.next().await { | ||
match h { | ||
Ok(_) => continue, | ||
Err(_) => break, | ||
} | ||
} | ||
}); | ||
|
||
let page = browser.new_page("about:blank").await?; | ||
|
||
let mut listener = page.event_listener::<EventBindingCalled>().await?; | ||
|
||
let value_from_js: Arc<Mutex<String>> = Arc::new(Mutex::new("".to_string())); | ||
let value_from_js_clone = Arc::clone(&value_from_js); | ||
|
||
tokio::spawn(async move { | ||
while let Some(event) = listener.next().await { | ||
if event.name == "testFunc1" { | ||
let mut locked_value = value_from_js_clone.lock().await; | ||
*locked_value = event.payload.clone(); | ||
} | ||
} | ||
}); | ||
|
||
page.execute(AddBindingParams::new("testFunc1")).await?; | ||
|
||
page.evaluate("window.testFunc1('30');").await?; | ||
|
||
let value = value_from_js.lock().await; | ||
assert_eq!(*value, "30"); | ||
|
||
browser.close().await?; | ||
handle.await; | ||
|
||
Ok(()) | ||
} |