Skip to content

Instantly share code, notes, and snippets.

@artjomb
Created November 25, 2014 14:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artjomb/a84e915f05b03cf19bdc to your computer and use it in GitHub Desktop.
Save artjomb/a84e915f05b03cf19bdc to your computer and use it in GitHub Desktop.
nightmareDownload.js
var useOldDownloadWay = false;
var Nightmare = require('nightmare');
new Nightmare()
.goto('http://eprint.iacr.org/2004/152')
.evaluate(function ev(old){
var el = document.querySelector("[href*='.pdf']");
var xhr = new XMLHttpRequest();
xhr.open("GET", el.href, false);
if (old) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
} else {
xhr.responseType = "arraybuffer";
}
xhr.send();
if (old) {
return xhr.responseText;
} else {
var bytes = [];
var array = new Uint8Array(xhr.response);
for (var i = 0; i < array.length; i++) {
bytes[i] = array[i];
}
return bytes;
}
}, function cb(data){
var fs = require("fs");
if (useOldDownloadWay) {
fs.writeFileSync("book.epub", data, "binary");
} else {
fs.writeFileSync("book.epub", new Buffer(data), "binary");
}
}, useOldDownloadWay)
.run(function (err, nightmare) {
if (err) return console.log(err);
console.log('Done!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment