Skip to content

Commit

Permalink
chore(chrome): add automation all routes handling
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Oct 22, 2024
1 parent dd4202d commit 5368bf9
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 39 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.10.5"
version = "2.10.6"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
71 changes: 45 additions & 26 deletions spider/src/features/chrome_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,22 +577,6 @@ pub type ExecutionScripts = Trie<String>;
/// Automation scripts to run on the page when using chrome by url.
pub type AutomationScripts = Trie<Vec<WebAutomation>>;

/// Convert ExecutionScripts to Trie.
pub fn convert_to_trie_execution_scripts(
input: &Option<ExecutionScriptsMap>,
) -> Option<Trie<String>> {
match input {
Some(ref scripts) => {
let mut trie = Trie::new();
for (path, script) in scripts {
trie.insert(path, script.clone());
}
Some(trie)
}
None => None,
}
}

#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// Chrome request interception configurations.
Expand Down Expand Up @@ -629,6 +613,22 @@ impl RequestInterceptConfiguration {
}
}

/// Convert ExecutionScripts to Trie.
pub fn convert_to_trie_execution_scripts(
input: &Option<ExecutionScriptsMap>,
) -> Option<Trie<String>> {
match input {
Some(ref scripts) => {
let mut trie = Trie::new();
for (path, script) in scripts {
trie.insert(path, script.clone());
}
Some(trie)
}
None => None,
}
}

/// Convert AutomationScripts to Trie.
pub fn convert_to_trie_automation_scripts(
input: &Option<AutomationScriptsMap>,
Expand All @@ -653,12 +653,18 @@ pub async fn eval_execution_scripts(
execution_scripts: &Option<ExecutionScripts>,
) {
match execution_scripts {
Some(ref scripts) => match scripts.search(target_url) {
Some(script) => {
Some(ref scripts) => {
if let Some(script) = scripts.search(target_url) {
let _ = page.evaluate(script.as_str()).await;
} else if scripts.match_all {
match scripts.root.value.as_ref() {
Some(script) => {
let _ = page.evaluate(script.as_str()).await;
}
_ => (),
}
}
_ => (),
},
}
_ => (),
}
}
Expand All @@ -673,15 +679,28 @@ pub async fn eval_automation_scripts(
if let Some(script_map) = automation_scripts {
if let Some(scripts) = script_map.search(target_url) {
for script in scripts {
let result =
if let Err(elasped) =
tokio::time::timeout(tokio::time::Duration::from_secs(60), script.run(page))
.await;
match result {
Ok(_) => (),
Err(_) => {
crate::utils::log("Script execution timed out for - ", target_url);
.await
{
log::warn!("Script execution timed out for: {target_url} - {elasped}",);
}
}
} else if script_map.match_all {
match script_map.root.value.as_ref() {
Some(scripts) => {
for script in scripts {
if let Err(elasped) = tokio::time::timeout(
tokio::time::Duration::from_secs(60),
script.run(page),
)
.await
{
log::warn!("Script execution timed out for: {target_url} - {elasped}",);
}
}
}
_ => (),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion spider/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,4 +1687,4 @@ async fn test_duration() {
"Duration took longer than expected {}.",
duration_elasped,
);
}
}
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.10.5"
version = "2.10.6"
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.10.5"
version = "2.10.6"
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.10.5"
version = "2.10.6"
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.10.5"
version = "2.10.6"
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.10.5"
version = "2.10.6"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down

0 comments on commit 5368bf9

Please sign in to comment.