[31525] | 1 | // Render Multiple URLs to file
|
---|
| 2 |
|
---|
| 3 | var RenderUrlsToFile, arrayOfUrls, system;
|
---|
| 4 |
|
---|
| 5 | system = require("system");
|
---|
| 6 |
|
---|
| 7 | /*
|
---|
| 8 | Render given urls
|
---|
| 9 | @param array of URLs to render
|
---|
| 10 | @param callbackPerUrl Function called after finishing each URL, including the last URL
|
---|
| 11 | @param callbackFinal Function called after finishing everything
|
---|
| 12 | */
|
---|
| 13 | RenderUrlsToFile = function(urls, callbackPerUrl, callbackFinal) {
|
---|
| 14 | var getFilename, next, page, retrieve, urlIndex, webpage;
|
---|
| 15 | urlIndex = 0;
|
---|
| 16 | webpage = require("webpage");
|
---|
| 17 | page = null;
|
---|
| 18 | getFilename = function() {
|
---|
| 19 | return "rendermulti-" + urlIndex + ".png";
|
---|
| 20 | };
|
---|
| 21 | next = function(status, url, file) {
|
---|
| 22 | page.close();
|
---|
| 23 | callbackPerUrl(status, url, file);
|
---|
| 24 | return retrieve();
|
---|
| 25 | };
|
---|
| 26 | retrieve = function() {
|
---|
| 27 | var url;
|
---|
| 28 | if (urls.length > 0) {
|
---|
| 29 | url = urls.shift();
|
---|
| 30 | urlIndex++;
|
---|
| 31 | page = webpage.create();
|
---|
| 32 | page.viewportSize = {
|
---|
| 33 | width: 800,
|
---|
| 34 | height: 600
|
---|
| 35 | };
|
---|
| 36 | page.settings.userAgent = "Phantom.js bot";
|
---|
| 37 | return page.open("http://" + url, function(status) {
|
---|
| 38 | var file;
|
---|
| 39 | file = getFilename();
|
---|
| 40 | if (status === "success") {
|
---|
| 41 | return window.setTimeout((function() {
|
---|
| 42 | page.render(file);
|
---|
| 43 | return next(status, url, file);
|
---|
| 44 | }), 200);
|
---|
| 45 | } else {
|
---|
| 46 | return next(status, url, file);
|
---|
| 47 | }
|
---|
| 48 | });
|
---|
| 49 | } else {
|
---|
| 50 | return callbackFinal();
|
---|
| 51 | }
|
---|
| 52 | };
|
---|
| 53 | return retrieve();
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | arrayOfUrls = null;
|
---|
| 57 |
|
---|
| 58 | if (system.args.length > 1) {
|
---|
| 59 | arrayOfUrls = Array.prototype.slice.call(system.args, 1);
|
---|
| 60 | } else {
|
---|
| 61 | console.log("Usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]");
|
---|
| 62 | arrayOfUrls = ["www.google.com", "www.bbc.co.uk", "www.phantomjs.org"];
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | RenderUrlsToFile(arrayOfUrls, (function(status, url, file) {
|
---|
| 66 | if (status !== "success") {
|
---|
| 67 | return console.log("Unable to render '" + url + "'");
|
---|
| 68 | } else {
|
---|
| 69 | return console.log("Rendered '" + url + "' at '" + file + "'");
|
---|
| 70 | }
|
---|
| 71 | }), function() {
|
---|
| 72 | return phantom.exit();
|
---|
| 73 | });
|
---|