Public
Snippet $351 authored by ValVin

PhantomJS - extradomains.js

extradomain.js
//from netsniff.js in phantomjs example

var page = require('webpage').create(),
    system = require('system');
var redirectUrl = null;

//URI parser
function getLocation(href) {
  var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([\/]{0,1}[^?#]*)(\?[^#]*|)(#.*|)$/);
  return match && {
    protocol: match[1],
    host: match[2],
    hostname: match[3],
    port: match[4],
    pathname: match[5],
    search: match[6],
    hash: match[7]
  }
}

function renderUrl(url){
  console.log('Rendering ' + url);
  page.address = url;
  page.resources = [];

  page.onLoadStarted = function () {
    page.startTime = new Date();
  };

  page.onResourceRequested = function (req) {
    page.resources[req.id] = {
      request: req,
      startReply: null,
      endReply: null
    };
  };

  page.onResourceReceived = function (res) {
    // to manage redirect url
    if (url == res.url && res.redirectURL) {
      redirectURL = res.redirectURL;
    }
    if (res.stage === 'start') {
      page.resources[res.id].startReply = res;
    }
    if (res.stage === 'end') {
      page.resources[res.id].endReply = res;
    }
  };

  page.open(page.address, function (status) {
    if(redirectUrl)
      renderUrl(redirectUrl);
    if (status !== 'success') {
      console.log('FAIL to load the address');
      phantom.exit(1);
    } else {
      page.endTime = new Date();
      page.title = page.evaluate(function () {
        return document.title;
      });
      extradomains = [];
      page.resources.forEach(function(resource){
        var host = getLocation(resource.request.url).host;
        var hostKnown = false;
        for(i = 0; i < extradomains.length; i++)
        {
          if(host == extradomains[i]) hostKnown = true;
        }
        if(!hostKnown)
          extradomains.push(host);
      });
      console.log(JSON.stringify(extradomains));
      phantom.exit();
    }
  });
}
if (system.args.length === 1) {
  console.log('Usage: extradomain.js <some URL>');
  phantom.exit(1);
} else {
  renderUrl(system.args[1]);
}

//phantomjs extradomain.js http://www.google.fr
//Rendering http://www.google.fr
//["www.google.fr","ssl.gstatic.com"]