QPTR
QPTR

Reputation: 1690

Phantomjs not loading the correct url inside onResourceRequested

I have a simple script based off Phantomjs's Network Monitoring example that tries to log all the requests and the response headers associated with a particular website:

"use strict";

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

if (system.args.length === 1) {
    console.log('Usage: getRequests.js <some URL>');
    phantom.exit(1);
} else {

    address = system.args[1];

    console.log('The url entered is ' + address + '\n')

    page.onResourceReceived = function(response) {
        console.log('Received Request: ' + JSON.stringify(response.headers, undefined, 4) + '\n');
    };

    page.onResourceRequested = function (req) {
        console.log('Request URL ' + JSON.stringify(req.url, undefined, 4) + "\n");

    }

    page.onResourceError = function(resourceError) {
        console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
        console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
    };

    page.open(address);


}

However, when I try to run it, using ./phantomjs getRequests.js www.google.com (or any other webpage), it gives the error:

The url entered is www.google.com

Request URL "file:///home/myusername/scripts/www.google.com"

Unable to load resource (#1URL:file:///home/myusername/scripts/www.google.com)
Error code: 203. Description: Error opening /home/myusername/scripts/www.google.com: No such file or directory
Received Request: []

Basically, the url that I am entering on the terminal is being prepended with the path of the script file. The input that I am getting inside the address variable is perfectly fine i.e. google.com

Could anyone please help with this issue? I cannot understand why phantomjs maybe doing this. I am running Ubuntu 14.04 on VirtualBox. And the script was running fine before when I was just trying to output the requests for a particular page. Thanks.

Upvotes: 0

Views: 223

Answers (1)

Vaviloff
Vaviloff

Reputation: 16838

You'll probably laugh, but the correct way to use the script is

 ./phantomjs getRequests.js http://www.google.com

Upvotes: 1

Related Questions