1var fs = require('fs'); 2var system = require('system'); 3var page = require('webpage').create(); 4 5// Examine console log for message from chart drawing 6page.onConsoleMessage = function(msg) { 7 console.log(msg); 8 if (msg === "ALL CHARTS READY") { 9 window.charts_ready = true; 10 } 11 else if (msg.slice(0, 11) === "CHART READY") { 12 var chart_id = msg.split(" ")[2]; 13 console.log('grabbing ' + chart_id); 14 var png_data = page.evaluate(function (chart_id) { 15 var chart_div = document.getElementById(chart_id + '_png'); 16 return chart_div.outerHTML; 17 }, chart_id); 18 fs.write(args[2] + '/' + chart_id + '.png', png_data, 'w'); 19 } 20}; 21 22// Check command line arguments 23var args = system.args; 24if (args.length != 3) { 25 console.log("USAGE: " + args[0] + " REPORT_HTML OUT_DIR\n"); 26 phantom.exit(1); 27} 28 29// Open the web page 30page.open(args[1], function(status) { 31 if (status == 'fail') { 32 console.log("Failed to open file '" + args[1] + "'"); 33 phantom.exit(1); 34 } 35}); 36 37// Check status every 100 ms 38interval = window.setInterval(function () { 39 //console.log('waiting'); 40 if (window.charts_ready) { 41 clearTimeout(timer); 42 clearInterval(interval); 43 44 var fname = args[1].replace(/\/+$/, "").split("/").pop() 45 console.log("saving " + fname); 46 fs.write(args[2] + '/' + fname, page.content, 'w'); 47 phantom.exit(0); 48 } 49}, 100); 50 51// Time-out after 10 seconds 52timer = window.setTimeout(function () { 53 clearInterval(interval); 54 console.log("ERROR: timeout"); 55 phantom.exit(1); 56}, 10000); 57