Advertisement
Guest User

Untitled

a guest
Apr 30th, 2014
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var system = require('system');
  2. var fs = require('fs');
  3.  
  4. if (system.args.length !== 2) {
  5.     console.log('Not enough args: [config_infile]');
  6.     phantom.exit();
  7. }
  8.  
  9. var resource_log = {};
  10.  
  11. var infile = system.args[1];
  12. var config = JSON.parse(fs.read(infile));
  13.  
  14. var url = config.url;
  15. var imgfile = config.imgfile;  // PhantomJS won't actually take the screenshot unless the destination file ends in ".png"
  16. var htmlfile = config.htmlfile;
  17.  
  18. cookies = config["cookies"];
  19. for(var cookie_index in cookies) {
  20.     var could_store = phantom.addCookie(cookies[cookie_index]);
  21. }
  22.  
  23. console.log("");
  24. console.log("Screenshotting " + url + " to " + imgfile);
  25. var page = require('webpage').create();
  26. // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
  27.  
  28. // Log JavaScript console messages from the page to the command line
  29. page.onConsoleMessage = function(msg) {
  30.     console.log(msg);
  31. };
  32.  
  33. // Store the HTTP status code for the requested page
  34. page.onResourceReceived = function(res) {
  35.     if(typeof page.httpstatus == "undefined") {  // PhantomJS calls this callback for every resource on the page. First resource is the page itself.
  36.         //console.log(JSON.stringify(res, null, 4));
  37.         page.httpstatus = res.status;
  38.     }
  39. };
  40.  
  41. function dump_html(page, htmlfile) {
  42.     // TODO: Add logic here to validate that the size of the HTML the screenshot script captures is sane and not abusable
  43.     fs.write(htmlfile, page.content);
  44. }
  45.  
  46. function render_page(page, imgfile) {
  47.     console.log("Page HTTP status: " + page.httpstatus);
  48.     var could_screenshot = page.render(imgfile);
  49.     console.log("Imgfile: " + imgfile);
  50.     console.log("Could screenshot:" + could_screenshot);
  51.     if(could_screenshot === true) {
  52.         console.log("Screenshotted!");
  53.     }
  54.  
  55.     return could_screenshot;
  56. }
  57.  
  58. function log_resource_requested(request) {
  59.     //console.log("request started: " + request.id);
  60.     resource_log[request.id] = true;
  61. }
  62.  
  63. function log_resource_received(response) {
  64.     //console.log("response started: " + response.id);
  65.     delete resource_log[response.id];
  66. }
  67.  
  68. function screenshot_page(force_go) {
  69.     if(Object.keys(resource_log).length !== 0) {
  70.         //console.log("Waiting for resources to finish...");
  71.         return setTimeout(function() {
  72.             screenshot_page();
  73.         }, 50);
  74.     }
  75.  
  76.     if(force_go !== true) {  // All resources finished loading. Give page 400s more to load any more before we screenshot it.
  77.         //console.log("Giving page one last chance to lead resources...");
  78.         return setTimeout(function() {
  79.             screenshot_page(true);
  80.         }, 400);
  81.     }
  82.  
  83.     //console.log("Screenshotting page");
  84.     dump_html(page, htmlfile);
  85.     var could_screenshot = render_page(page, imgfile);
  86.     if(!could_screenshot) {
  87.         phantom.exit(2);
  88.         return;
  89.     }
  90.  
  91.     phantom.exit(0);
  92. }
  93.  
  94. page.viewportSize = {width: 960, height: 768};
  95. page.onResourceRequested = log_resource_requested;
  96. page.onResourceReceived = log_resource_received;
  97.  
  98. page.open(url, function(status) {
  99.     console.log("Page opened: " + status);
  100.     if(status != "success") {
  101.         phantom.exit(1);
  102.         return; // phantom.exit(n) doesn't reliably return that code if it's not the last call to exit() in the script. So. Stupid.
  103.     }
  104.  
  105.     setTimeout(screenshot_page, 150);
  106. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement