Corion, forget the JavaScript for now. The goal is to use a single method to download all type of files (images, pdfs, CSVs etc..)
According to the documentations, the following method should be able to do it.
my @links = $mech->find_all_links(url_regex => qr/\.pdf/i);
my @urls = map { $_->url_abs } @links;
foreach (my $foo @urls )
{
my $abs_path = "C:/path";
$mech->set_download_directory( $abs_path );
$mech->get($foo);
}
Unfortunately this method works only with files that the browser normally does not load such as (.csv). If you use this method with images (.jpg etc...) or PDFs (.pdf) the documents will load in the browser and the download fails.
This exact same task has worked with WWW::Mechanize using the following method
my $filename = "C:/path/filename";
my $foo = "http link";
$mech->get($foo, ':content_file'=>$filename);
The workaround that you have suggested below didn't work with PDFs and CSVs. it worked with images only.
my $response = $mech->get($url);
my $c = $mech->content; # Dummy request to initialize everything
open my $output, '>:raw', '/tmp/output.jpg';
print { $output } $response->decoded_content;