scrape-html-report.js 1.6 KB

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