Phantomjs – how to check what resources are being requested by a page

Phantomjs lets you add hooks when it fetches a web page. Here is code snippet which can be used to print all resources phantomjs fetches after fetching the main page.

var url = require('system').args[1];
var page = require('webpage').create();
page.onResourceRequested = function (requestData, request) {
  console.log('url requested: ' + requestData.url);
};
page.open(url, function (status) {
  if (status !== 'success') {
    console.log('Unable to access network');
  } else {
    console.log("Done...");
    phantom.exit(0);
  }
});

Save above code as resources.js and run

phantomjs resources.js http://dev.infoheap.com/

Here is the sample outcome (first few lines) which come from running this on Ubuntu Linix:

url requested: http://dev.infoheap.com/
url requested: http://dev.infoheap.com/wp-content/plugins/yet-another-related-posts-plugin/style/widget.css?ver=4.3.1
url requested: http://dev.infoheap.com/wp-content/themes/shell-master/style.css?ver=0.1.1
url requested: http://dev.infoheap.com/wp-content/themes/shell-master/media-queries.css?ver=0.1.1
url requested: http://dev.infoheap.com/wp-content/themes/shell-child-colorful/style.css?ver=0.0.1
url requested: http://dev.infoheap.com/wp-includes/js/jquery/jquery.js?ver=1.11.3
url requested: http://dev.infoheap.com/wp-includes/js/jquery/jquery-migrate.js?ver=1.2.1
url requested: http://www.google-analytics.com/analytics.js
...
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments