Skip to content

Commit

Permalink
feat(chrome): add web automation wait for dom
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Nov 1, 2024
1 parent 8595e52 commit a55214e
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider"
version = "2.11.2"
version = "2.11.3"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
34 changes: 34 additions & 0 deletions spider/src/features/chrome_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ pub enum WebAutomation {
Wait(u64),
/// Waits for the next navigation event.
WaitForNavigation,
/// Wait for dom updates to stop.
WaitForDom {
/// The selector of the input element to fill.
selector: Option<String>,
/// The timeout to wait for.
timeout: u32,
},
/// Waits for an element to appear.
WaitFor(String),
/// Waits for an element to appear and then clicks on it.
Expand Down Expand Up @@ -475,6 +482,25 @@ pub enum WebAutomation {
},
}

#[cfg(feature = "chrome")]
/// Generate the wait for function.
fn generate_wait_for_dom_js_code_with_selector(timeout: u32, selector: Option<&str>) -> String {
// Ensure the timeout doesn't exceed the maximum limit of 60000 milliseconds.
let clamped_timeout = if timeout > 60000 { 60000 } else { timeout };
let query_selector = selector.unwrap_or("body");

format!(
"function w(){{return new Promise((r,j)=>{{ \
let t={}; \
let i=setTimeout(()=>{{j(new Error('Timeout: DOM did not update within the allowed time.'))}},t); \
if(document.querySelector('{}')){{clearTimeout(i);r();}}else{{ \
const o=new MutationObserver((m,a)=>{{ \
if(document.querySelector('{}')){{a.disconnect();clearTimeout(i);r();}}}}); \
o.observe(document,{{childList:true,subtree:true}});}}}});}}",
clamped_timeout, query_selector, query_selector
)
}

impl WebAutomation {
#[cfg(feature = "chrome")]
/// Run the web automation step.
Expand All @@ -497,6 +523,14 @@ impl WebAutomation {
WebAutomation::Wait(ms) => {
tokio::time::sleep(Duration::from_millis(*ms).min(Duration::from_secs(60))).await;
}
WebAutomation::WaitForDom { selector, timeout } => {
let _ = page
.evaluate(
generate_wait_for_dom_js_code_with_selector(*timeout, selector.as_deref())
.as_str(),
)
.await;
}
WebAutomation::WaitFor(selector) => {
wait_for_selector(page, Some(Duration::from_secs(60)), &selector).await;
}
Expand Down
2 changes: 1 addition & 1 deletion spider_chrome/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_chrome"
version = "2.11.2"
version = "2.11.3"
rust-version = "1.70"
authors = [
"j-mendez <jeff@spider.cloud>"
Expand Down
2 changes: 1 addition & 1 deletion spider_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_cli"
version = "2.11.2"
version = "2.11.3"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_transformations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_transformations"
version = "2.11.2"
version = "2.11.3"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_utils"
version = "2.11.2"
version = "2.11.3"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_worker"
version = "2.11.2"
version = "2.11.3"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down

0 comments on commit a55214e

Please sign in to comment.