Created
November 11, 2015 15:02
-
-
Save clebuchegger/53ea406964be966253c0 to your computer and use it in GitHub Desktop.
phantomJS script (not production ready, can hang)
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
/* | |
* phantomJS script to generate screenshots from line break seperated list of urls | |
* todo: multi-threaded via nodeJS | |
* */ | |
var fs = require('fs'), | |
system = require('system'), | |
content = '', | |
f = null, | |
lines = null, | |
pages =null, | |
destinations = null, | |
RenderUrlsToFile, | |
eol = system.os.name == 'windows' ? "\r\n" : "\n"; | |
/* | |
* Configuration | |
*/ | |
var browserWidth = 1024; | |
var browserHeight = 768; | |
var cropIt = true; | |
var cropSizeFromLeft_Width = 1024; | |
var cropSizeFromTop_Height = 768; | |
/* | |
*helper functions | |
*/ | |
function pad(n, width, z) { | |
z = z || '0'; | |
n = n + ''; | |
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n; | |
} | |
/* | |
Render given urls | |
@param array of URLs to render | |
@param callbackPerUrl Function called after finishing each URL, including the last URL | |
@param callbackFinal Function called after finishing everything | |
*/ | |
RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) { | |
var getFilename, next, page, retrieve, urlIndex, webpage; | |
urlIndex = 0; | |
webpage = require("webpage"); | |
page = null; | |
getFilename = function() { | |
return "render-" + pad((urlIndex),5) + ".png"; | |
}; | |
next = function(status, url, file) { | |
page.close(); | |
callbackPerUrl(status, url, file); | |
return retrieve(); | |
}; | |
retrieve = function() { | |
var url; | |
if (urls.length > 0) { | |
url = urls.shift(); | |
urlIndex++; | |
console.log("****************** " + urlIndex ); | |
page = webpage.create(); | |
page.viewportSize = { | |
width: browserWidth, | |
height: browserHeight | |
}; | |
page.settings.userAgent = "Phantom.js bot"; | |
return page.open("http://" + url, function(status) { | |
var file; | |
file = getFilename(); | |
if (status === "success") { | |
return window.setTimeout((function() { | |
page.evaluate(function() { | |
document.body.bgColor = 'white'; | |
}); | |
if (cropIt) { | |
page.clipRect = { | |
top: 0, | |
left: 0, | |
width: cropSizeFromLeft_Width, | |
height: cropSizeFromTop_Height | |
}; | |
} | |
page.render(file); | |
return next(status, url, file); | |
}), 200); | |
} else { | |
return next(status, url, file); | |
} | |
}); | |
} else { | |
return callbackFinal(); | |
} | |
}; | |
return retrieve(); | |
}; | |
//read in a line break separated list of urls | |
try { | |
f = fs.open("urls.txt", "r"); | |
content = f.read(); | |
} catch (e) { | |
console.log(e); | |
} | |
if (f) { | |
f.close(); | |
} | |
if (content) { | |
lines = content.split(eol); | |
pages = new Array(); | |
for (var i = 0, len = lines.length; i < len; i++) { | |
//remove http | |
var temp = lines[i].replace(new RegExp(/^http:\/\//i),""); | |
//remove trailing hash | |
if (temp.charAt(temp.length - 1) == '#') { | |
temp = temp.substr(0, temp.length - 1); | |
} | |
pages[i] = temp; | |
} | |
console.log('URLs found: ' + pages.length); | |
} else { | |
console.log("no URLs found"); | |
} | |
//start render recursevly | |
RenderUrlsToFile(pages, (function(status, url, file) { | |
if (status !== "success") { | |
return console.log("Unable to render '" + url + "'"); | |
} else { | |
return console.log("Rendered '" + url + "' at '" + file + "'"); | |
} | |
}), function() { | |
return phantom.exit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment