Created
April 9, 2013 18:52
-
-
Save ryandoherty/5348343 to your computer and use it in GitHub Desktop.
Renders a webpage to a png. Waits 500ms after the last network request to ensure ajax content loads (not 100% guaranteed, but 500ms is usually enough).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var webpage = require('webpage'), | |
system = require('system'), | |
fs = require('fs'), | |
start, | |
urls = fs.read('urls.txt').split("\n"), | |
index = 0; | |
function fetch(url) { | |
var timer, | |
firstRequest = true; | |
page = webpage.create(); | |
start = Date.now(); | |
console.log("Fetching: "+url); | |
page.settings.loadImages = false; | |
page.viewportSize = {width:1280, height: 900}; | |
page.onResourceRequested = function(request) { | |
clearTimeout(timer); | |
if(firstRequest) { | |
console.log("Saw first request: "+request.url); | |
timer = setTimeout(function() { render(page, url); }, 1000); | |
firstRequest = false; | |
} else { | |
timer = setTimeout(function() { render(page, url); }, 500); | |
} | |
}; | |
page.open(url, function() { | |
}); | |
} | |
function render(page, url) { | |
var time = Date.now() - start; | |
var filename = url; | |
filename = filename.replace(/[^-a-zA-Z0-9,&\s]+/ig, ''); | |
filename = filename.replace(/-/gi, "_"); | |
filename = filename.replace(/\s/gi, "-"); | |
console.log(time); | |
page.onResourceRequested = null; | |
page.render('images/'+filename+'-'+time+'.png'); | |
page.release(); | |
read(); | |
} | |
function read() { | |
var url = urls[index]; | |
index++; | |
fetch(url); | |
} | |
read(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment