Skip to content

Instantly share code, notes, and snippets.

@alvises
Created September 16, 2015 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvises/e9822ed8c2c0e4c4e6fc to your computer and use it in GitHub Desktop.
Save alvises/e9822ed8c2c0e4c4e6fc to your computer and use it in GitHub Desktop.
var url, contentType;
casper.start(....)
//download time
//waitForResource because sometime the server is redirecting multiple time
//the browser, so we need the final url to download it as pdf (or render it as html)
.waitForResource(function testResource(resource) {
console.log("resource: "+resource.status+" : "+resource.contentType+" : "+resource.url);
if(resource.status == 200) {
url = resource.url;
contentType = resource.contentType;
}
return resource.status == 200
},function onResourceReceived(resource){
console.log("contentType: "+resource.contentType);
})
.then(function() {
//we could also use the "contentType" to distinguish pdf to html
if(url.indexOf(".pdf") > 0) {
//there is a pdf to download
console.log("downloading pdf: "+downloadPath+" - "+url);
this.download(url,downloadPath);
} else {
//there is an html page to convert
console.log("capturing html: "+downloadPath+" - "+url);
this.capture(downloadPath);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment