mirror of https://github.com/vitalif/phantomjs
Converted 'cycle_multiple_urls.js' to PhantomJS 1.2 (and renamed to 'render_multi_url.js').
Given that I practically rewrote it, I took the liberty to rename it based on my personal taste ;)1.2
parent
93d2c30248
commit
e59054247c
|
@ -1,64 +0,0 @@
|
|||
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
Cycle array of URLs and process with phantom.js
|
||||
Adds Array.prototype.forEachWebPage() iterator.
|
||||
|
||||
EXAMPLE:
|
||||
Save screenshots. Command line:
|
||||
phantomjs phantom_js_url_cycle.js ./screenshots
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
|
||||
[
|
||||
|
||||
'www.google.com',
|
||||
'www.bbc.co.uk',
|
||||
'www.phantomjs.org'
|
||||
|
||||
].forEachWebPage(function (url) {
|
||||
var out_dir = phantom.args[0] || '.';
|
||||
var file_name = [out_dir, url + '.png'].join('/'); // ./screenshots/www.google.com.png
|
||||
phantom.render(file_name);
|
||||
console.log('Generated ' + file_name);
|
||||
});
|
||||
|
||||
-------------------------- */
|
||||
var UrlCycle = (function () {
|
||||
|
||||
function extend (target, source) {
|
||||
for(var i in source) {
|
||||
target[i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
var prot = 'http://', urls, opts = {width: 800, height: 600};
|
||||
|
||||
function cycle (urls, o, callback) {
|
||||
if(phantom.state.length === 0){ // first pass
|
||||
urls = urls;
|
||||
extend(opts, o || {});
|
||||
phantom.viewportSize = { width: opts.width, height: opts.height };
|
||||
phantom.state = 0;
|
||||
phantom.userAgent = 'Phantom.js bot';
|
||||
phantom.open(prot + urls[phantom.state]);
|
||||
} else { // page open
|
||||
|
||||
callback(urls[phantom.state]);
|
||||
|
||||
if(next_url = urls[++phantom.state]) {
|
||||
console.log('opening '+next_url)
|
||||
phantom.open(prot+next_url);
|
||||
} else {
|
||||
console.log('Done. Bye!')
|
||||
phantom.exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Array.prototype.forEachWebPage = function (callback, opts) {
|
||||
cycle(this, opts, callback);
|
||||
}
|
||||
|
||||
return {
|
||||
cycle: cycle
|
||||
};
|
||||
|
||||
})();
|
|
@ -0,0 +1,60 @@
|
|||
// Render Multiple URLs to file
|
||||
// FIXME: For now it is fine with pure domain names: don't think it would work with paths and stuff like that
|
||||
|
||||
// Extend the Array Prototype with a 'foreach'
|
||||
Array.prototype.forEach = function (action) {
|
||||
var i, len;
|
||||
for ( i = 0, len = this.length; i < len; ++i ) {
|
||||
action(i, this[i], len);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Render a given url to a given file
|
||||
* @param url URL to render
|
||||
* @param file File to render to
|
||||
* @param callback Callback function
|
||||
*/
|
||||
function renderUrlToFile(url, file, callback) {
|
||||
var page = new WebPage();
|
||||
page.viewportSize = { width: 800, height : 600 };
|
||||
page.settings.userAgent = "Phantom.js bot";
|
||||
|
||||
page.open(url, function(status){
|
||||
if ( status !== "success") {
|
||||
console.log("Unable to render '"+url+"'");
|
||||
} else {
|
||||
page.render(file);
|
||||
}
|
||||
delete page;
|
||||
callback(url, file);
|
||||
});
|
||||
}
|
||||
|
||||
// Read the passed args
|
||||
var arrayOfUrls;
|
||||
if ( phantom.args.length > 0 ) {
|
||||
arrayOfUrls = phantom.args;
|
||||
} else {
|
||||
// Default (no args passed)
|
||||
console.log("Usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]");
|
||||
arrayOfUrls = [
|
||||
'www.google.com',
|
||||
'www.bbc.co.uk',
|
||||
'www.phantomjs.org'
|
||||
];
|
||||
}
|
||||
|
||||
// For each URL
|
||||
arrayOfUrls.forEach(function(pos, url, total){
|
||||
var file_name = "./" + url + ".png";
|
||||
|
||||
// Render to a file
|
||||
renderUrlToFile("http://"+url, file_name, function(url, file){
|
||||
console.log("Rendered '"+url+"' at '"+file+"'");
|
||||
if ( pos === total-1 ) {
|
||||
// Close Phantom if it's the last URL
|
||||
phantom.exit();
|
||||
}
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue