Advertisement
anmiles

Listen ajax responses in phantomJS

Jun 1st, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //example
  2. listenAjaxResponses(page, 'getUserDetails', function(responseData)
  3. {
  4.     // this will be executed within the sandboxed page, not within the phantomjs
  5.     alert('User details has requested!');
  6.     var username = eval('(' + responseData + ')').user.username;
  7.     alert(username);
  8. });
  9.  
  10. //listener for all ajax responses of the page
  11. function listenAjaxResponses(page, urlPattern, callback)
  12. {
  13.     page.evaluate(function(urlPattern, callback)
  14.     {
  15.         try
  16.         {
  17.             (function(open)
  18.             {
  19.                 XMLHttpRequest.prototype.open = function(method, url, async, user, pass)
  20.                 {
  21.                     this.addEventListener("readystatechange", function()
  22.                     {
  23.                         if (url.indexOf(urlPattern) === 0)
  24.                         {                      
  25.                             if (this.readyState == XMLHttpRequest.DONE)
  26.                             {
  27.                                 callback(this.responseText);
  28.                             }
  29.                         }
  30.                     }, false);
  31.  
  32.                     open.call(this, method, url, async, user, pass);
  33.                 };
  34.  
  35.             })(XMLHttpRequest.prototype.open);
  36.         }
  37.         catch (ex)
  38.         {
  39.             alert(ex);
  40.         }          
  41.     }, urlPattern, callback);
  42. }
  43.  
  44. //create a blank page
  45. function createPage(viewportSize)
  46. {
  47.     var page = webpage.create();
  48.    
  49.     page.settings.webSecurityEnabled = false;
  50.     page.settings.localToRemoteUrlAccessEnabled = true;
  51.     page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36';
  52.     if (viewportSize) page.viewportSize = viewportSize;
  53.    
  54.     page.onAlert = function(error)
  55.     {
  56.         console.log('Alert: {0}'.format(error));
  57.     };
  58.    
  59.     page.onError = function(error, trace)
  60.     {
  61.         var msgStack = ['ERROR: {0}'.format(error)];
  62.        
  63.         if (trace && trace.length)
  64.         {
  65.             msgStack.push('TRACE:');
  66.            
  67.             trace.forEach(function(t)
  68.             {
  69.                 msgStack.push(' -> {file}: {line} (in function "{function}")'.format(t));
  70.             });
  71.         }
  72.         console.log('Error: {0}'.format(JSON.stringify(trace)));
  73.     };
  74.    
  75.     return page;
  76. }
  77.  
  78. //formatting function
  79. //example 1: 'loren {0} dolor {1} amet'.format(['ipsum', 'sit']) = 'loren ipsum dolor sit amet'
  80. //example 2: '{city} is a {{0}} of {country}'.format({city: 'London', country: 'Great Britain'}) = 'London is a {0} of Great Britain'
  81. String.prototype.format = function format()
  82. {
  83.     if (arguments.length == 0) return this;
  84.  
  85.     var data;
  86.     if (arguments.length > 1) data = arguments;
  87.     else if (typeof arguments[0] == 'object') data = arguments[0];
  88.     else data = arguments;
  89.  
  90.     return this.split('{{').map(function(q)
  91.     {
  92.         return q.replace(/\{([^\{\}]+)\}/g, function ($0, $1)
  93.             {
  94.                 return data[$1];
  95.             });
  96.     }).join('{').replace(/\}\}/g, '}');
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement