Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var system = require('system');
- var fs = require('fs');
- if (system.args.length !== 2) {
- console.log('Not enough args: [config_infile]');
- phantom.exit();
- }
- var resource_log = {};
- var infile = system.args[1];
- var config = JSON.parse(fs.read(infile));
- var url = config.url;
- var imgfile = config.imgfile; // PhantomJS won't actually take the screenshot unless the destination file ends in ".png"
- var htmlfile = config.htmlfile;
- cookies = config["cookies"];
- for(var cookie_index in cookies) {
- var could_store = phantom.addCookie(cookies[cookie_index]);
- }
- console.log("");
- console.log("Screenshotting " + url + " to " + imgfile);
- var page = require('webpage').create();
- // Route "console.log()" calls from within the Page context to the main Phantom context (i.e. current "this")
- // Log JavaScript console messages from the page to the command line
- page.onConsoleMessage = function(msg) {
- console.log(msg);
- };
- // Store the HTTP status code for the requested page
- page.onResourceReceived = function(res) {
- if(typeof page.httpstatus == "undefined") { // PhantomJS calls this callback for every resource on the page. First resource is the page itself.
- //console.log(JSON.stringify(res, null, 4));
- page.httpstatus = res.status;
- }
- };
- function dump_html(page, htmlfile) {
- // TODO: Add logic here to validate that the size of the HTML the screenshot script captures is sane and not abusable
- fs.write(htmlfile, page.content);
- }
- function render_page(page, imgfile) {
- console.log("Page HTTP status: " + page.httpstatus);
- var could_screenshot = page.render(imgfile);
- console.log("Imgfile: " + imgfile);
- console.log("Could screenshot:" + could_screenshot);
- if(could_screenshot === true) {
- console.log("Screenshotted!");
- }
- return could_screenshot;
- }
- function log_resource_requested(request) {
- //console.log("request started: " + request.id);
- resource_log[request.id] = true;
- }
- function log_resource_received(response) {
- //console.log("response started: " + response.id);
- delete resource_log[response.id];
- }
- function screenshot_page(force_go) {
- if(Object.keys(resource_log).length !== 0) {
- //console.log("Waiting for resources to finish...");
- return setTimeout(function() {
- screenshot_page();
- }, 50);
- }
- if(force_go !== true) { // All resources finished loading. Give page 400s more to load any more before we screenshot it.
- //console.log("Giving page one last chance to lead resources...");
- return setTimeout(function() {
- screenshot_page(true);
- }, 400);
- }
- //console.log("Screenshotting page");
- dump_html(page, htmlfile);
- var could_screenshot = render_page(page, imgfile);
- if(!could_screenshot) {
- phantom.exit(2);
- return;
- }
- phantom.exit(0);
- }
- page.viewportSize = {width: 960, height: 768};
- page.onResourceRequested = log_resource_requested;
- page.onResourceReceived = log_resource_received;
- page.open(url, function(status) {
- console.log("Page opened: " + status);
- if(status != "success") {
- phantom.exit(1);
- return; // phantom.exit(n) doesn't reliably return that code if it's not the last call to exit() in the script. So. Stupid.
- }
- setTimeout(screenshot_page, 150);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement