Advertisement
Guest User

Untitled

a guest
Dec 15th, 2014
1,810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Captures a webpage with a Google JavaScript Map and exports it to PNG.
  3. */
  4. //Original code: https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js
  5.  
  6. function waitFor(testFx, onReady, timeOutMillis) {
  7.     var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
  8.         start = new Date().getTime(),
  9.         condition = false,
  10.         interval = setInterval(function() {
  11.             if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
  12.                 // If not time-out yet and condition not yet fulfilled
  13.                 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
  14.             } else {
  15.                 if(!condition) {
  16.                     // If condition still not fulfilled (timeout but condition is 'false')
  17.                     console.log("'waitFor()' timeout");
  18.                     phantom.exit(1);
  19.                 } else {
  20.                     // Condition fulfilled (timeout and/or condition is 'true')
  21.                     console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
  22.                     typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
  23.                     clearInterval(interval); //< Stop this interval
  24.                 }
  25.             }
  26.         }, 250); //< repeat check every 250ms
  27. };
  28.  
  29.  
  30. var page = require('webpage').create();
  31. page.viewportSize = {
  32.   width: 1000,
  33.   height: 1000
  34. };
  35.  
  36. page.open("http://jsbin.com/pevizusana", function (status) { //Works best on localhost
  37.     // Check for page load success
  38.     if (status !== "success") {
  39.         console.log("Unable to access network");
  40.     } else {
  41.         // Wait for 'signin-dropdown' to be visible
  42.         waitFor(function() {
  43.             // Check in the page if a specific element is now visible
  44.             return page.evaluate(function() {
  45.                 return document.getElementById("loaded");
  46.             });
  47.         }, function() {
  48.            console.log("The map should be visible now.");
  49.            page.render('googleScreenShot' + '.png');
  50.            phantom.exit();
  51.         });        
  52.     }
  53. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement