Advertisement
Guest User

phantomjs

a guest
Apr 7th, 2017
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function waitFor(testFx, onReady, timeOutMillis) {
  2.     var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
  3.         start = new Date().getTime(),
  4.         condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()), //< defensive code
  5.         interval = setInterval(function() {
  6.             if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
  7.                 // If not time-out yet and condition not yet fulfilled
  8.                 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
  9.             } else {
  10.                 if(!condition) {
  11.                     // If condition still not fulfilled (timeout but condition is 'false')
  12.                     console.log("'waitFor()' timeout");
  13.                     phantom.exit(1);
  14.                 } else {
  15.                     // Condition fulfilled (timeout and/or condition is 'true')
  16.                     console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
  17.                     typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
  18.                     clearInterval(interval); //< Stop this interval
  19.                 }
  20.             }
  21.         }, 250); //< repeat check every 250ms
  22. };
  23.  
  24.  
  25. var system = require('system'), page = require('webpage').create(), url, output, waitTime, pwidth = 1400, pheight = 700;
  26. console.log(system.args+"\n");
  27.  
  28. function isInt(value) {
  29.     return !isNaN(value) && parseInt(Number(value)) == value && !isNaN(parseInt(value, 10));
  30. }
  31. if(system.args.length < 4 || system.args.length > 6){
  32.     console.log("Invalid parameter number, Usage: phantomjs script.js url output(e.g file.png) waitTime width(optional) height(option) \n Default size is {width: 1400, height: 700}");
  33.     phantom.exit();
  34. }
  35. url = system.args[1];
  36. output = system.args[2];
  37. waitTime = system.args[3];
  38.  
  39. if(system.args.length == 6 && isInt(system.args[4]) && isInt(system.args[5])){
  40.     pwidth = system.args[4];
  41.     pheight = system.args[5];
  42. }
  43.  
  44. var resources = [];
  45. page.onResourceRequested = function(request) {
  46.     resources[request.id] = request.stage;
  47. };
  48. page.onResourceReceived = function(response) {
  49.     resources[response.id] = response.stage;
  50. };
  51.  
  52. page.viewportSize = { width: pwidth, height: pheight };
  53. page.open(url, function(status) {
  54.     if(status !== 'success'){
  55.         console.log('Unable to load the address!');
  56.         phantom.exit();
  57.     }else{
  58.             waitFor(function() {
  59.                 // Check in the page if a specific element is now visible
  60.                 for ( var i = 1; i < resources.length; ++i) {
  61.                     if (resources[i] != 'end') {
  62.                         return false;
  63.                     }
  64.                 }
  65.                 return true;
  66.             }, function() {
  67.                page.render(output);
  68.                phantom.exit();
  69.             }, 10000);
  70.         /*window.setTimeout(function(){
  71.             page.render(output);
  72.             phantom.exit();  
  73.         }, waitTime
  74.         );*/  
  75.     }
  76. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement