Skip to content

Commit

Permalink
Merge pull request #104 from pwnwriter/feat/js
Browse files Browse the repository at this point in the history
feat(args): run arbitary javascript in websites [closes #101[
  • Loading branch information
pwnwriter committed Jun 20, 2024
2 parents 386da9f + 53987da commit 184a95b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub struct Cli {
/// Accept invalid certs, trust dns
#[arg(long)]
pub accept_invalid_certs: bool,

/// Run arbiraty javascript
#[arg(long)]
pub javascript: Option<String>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
Expand Down
4 changes: 4 additions & 0 deletions src/cli/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub async fn run(
screenshot_type,
ports,
accept_invalid_certs,
javascript,
}: Cli,
) -> anyhow::Result<()> {
let browser = Path::new(&binary_path);
Expand Down Expand Up @@ -105,6 +106,7 @@ pub async fn run(
fullpage,
screenshot_type,
accept_invalid_certs,
javascript,
)
.await?;
} else {
Expand All @@ -122,6 +124,7 @@ pub async fn run(
fullpage,
screenshot_type,
accept_invalid_certs,
javascript,
)
.await?;
}
Expand All @@ -142,6 +145,7 @@ pub async fn run(
fullpage,
screenshot_type,
accept_invalid_certs,
javascript,
)
.await?;
}
Expand Down
18 changes: 18 additions & 0 deletions src/cli/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ pub async fn take_screenshot_in_bulk(
full_page: bool,
screenshot_type: ScreenshotType,
danger_accept_invalid_certs: bool,
javascript: Option<String>,
) -> anyhow::Result<()> {
let url_chunks: Vec<Vec<_>> = urls.chunks(tabs).map(ToOwned::to_owned).collect();
let mut handles = Vec::with_capacity(url_chunks.len());

for urls in url_chunks {
let browser = Arc::clone(browser);
let js = javascript.clone(); // Clone the JavaScript for each chunk
let handle = tokio::spawn(async move {
for url in urls {
if let Err(error) = take_screenshot(
Expand All @@ -40,6 +42,7 @@ pub async fn take_screenshot_in_bulk(
full_page,
screenshot_type,
danger_accept_invalid_certs,
js.clone(), // Pass the JavaScript code
)
.await
{
Expand Down Expand Up @@ -67,6 +70,7 @@ pub async fn take_screenshot(
full_page: bool,
screenshot_type: ScreenshotType,
danger_accept_invalid_certs: bool,
javascript: Option<String>,
) -> anyhow::Result<()> {
let parsed_url = Url::parse(&url)?;
let client = reqwest::Client::builder()
Expand All @@ -90,6 +94,20 @@ pub async fn take_screenshot(
};
let page = browser.new_page(parsed_url.clone()).await?;
tokio::time::sleep(Duration::from_secs(delay)).await;


// Evaluate JavaScript if provided
if let Some(js) = javascript {
let result = page.evaluate(js.as_str()).await;
match result {
Ok(_) => log::info(
"JavaScript executed successfully".to_string(),
colored::Color::Magenta,
),
Err(e) => log::warn(format!("JavaScript execution failed: {:?}", e)),
}
}

page.save_screenshot(
ScreenshotParams::builder()
.format(screenshot_format)
Expand Down

0 comments on commit 184a95b

Please sign in to comment.