Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fake Referrer with Phantomjs
var system = require('system');
// Exit in case of wrong parameter count.
if (system.args.length !== 3) {
console.log('Usage: scriptname targetUrl referrer');
console.log('example: $> phantomjs fake-referrer.phantom.js http://example.com http://referrer.example.com');
phantom.exit();
}
// Set the important pieces
var targetUrl = system.args[1];
var referrer = system.args[2];
console.log('Going to open '+targetUrl+' with the referrer '+referrer);
var page = require('webpage').create();
// set our custom referer [sic]
page.customHeaders = {
"Referer" : referrer
};
page.onLoadFinished = function(status){
page.customHeaders = {};
// get the currentUrl
var currentUrl = page.evaluate(function() {
return document.location.href;
});
// get the referrer
var currentReferrer = page.evaluate(function() {
return document.referrer;
});
console.log('Loading ' + currentUrl + ' finished with status: ' + status+'. document.referrer is: '+currentReferrer);
// Only once do
if ( page.firstLoad ) {
page.firstLoad = false;
console.log('Injecting the Link.');
// Inject and Click a Link to our target
page.evaluate(function (href) {
// Create and append the link
var link = document.createElement('a');
link.setAttribute('href', href);
document.body.appendChild(link);
// Dispatch Click Event on the link
var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, window, 1, 1, 1, 1, 1, false, false, false, false, 0, link);
link.dispatchEvent(evt);
}, targetUrl);
} else {
console.log('Exiting');
phantom.exit();
};
};
page.firstLoad = true;
page.open(referrer);
@remeiberlin

This comment has been minimized.

Show comment
Hide comment
@remeiberlin

remeiberlin Jun 30, 2014

Thnx for the script! At least one modification should be made: after resetting the firstLoad indicator (line 39) the customHeaders should be cleared with "page.customHeaders = {};". Otherwise the referrer will be also send to every object (including images) loaded by the page. Some servers don't accept requests to elements from different hosts.

remeiberlin commented Jun 30, 2014

Thnx for the script! At least one modification should be made: after resetting the firstLoad indicator (line 39) the customHeaders should be cleared with "page.customHeaders = {};". Otherwise the referrer will be also send to every object (including images) loaded by the page. Some servers don't accept requests to elements from different hosts.

@robcolburn

This comment has been minimized.

Show comment
Hide comment
@robcolburn

robcolburn Jul 22, 2015

Well, at least I know it's not just me…

$ phantomjs -v
2.0.0
$ phantomjs fake-referrer.phantom.js http://robcolburn.com http://referrer.example.com
Going to open http://robcolburn.com with the referrer http://referrer.example.com
Loading about:blank finished with status: fail. document.referrer is:
Injecting the Link.
Loading http://robcolburn.com/ finished with status: success. document.referrer is:
Exiting

robcolburn commented Jul 22, 2015

Well, at least I know it's not just me…

$ phantomjs -v
2.0.0
$ phantomjs fake-referrer.phantom.js http://robcolburn.com http://referrer.example.com
Going to open http://robcolburn.com with the referrer http://referrer.example.com
Loading about:blank finished with status: fail. document.referrer is:
Injecting the Link.
Loading http://robcolburn.com/ finished with status: success. document.referrer is:
Exiting
@Eldair

This comment has been minimized.

Show comment
Hide comment
@Eldair

Eldair Jun 29, 2016

Unfortunately not working :|

Eldair commented Jun 29, 2016

Unfortunately not working :|

@satyendradv956

This comment has been minimized.

Show comment
Hide comment
@satyendradv956

satyendradv956 Jul 29, 2016

yes it is not working

satyendradv956 commented Jul 29, 2016

yes it is not working

@antsanchez

This comment has been minimized.

Show comment
Hide comment
@antsanchez

antsanchez Dec 11, 2016

Use Settings:

var settings = {
  headers: {
   "Referer": "referer_url"
  }
};

And pass it as the second argument on function page.open, like that:

page.open(url, settings, function(status) {
...
});

antsanchez commented Dec 11, 2016

Use Settings:

var settings = {
  headers: {
   "Referer": "referer_url"
  }
};

And pass it as the second argument on function page.open, like that:

page.open(url, settings, function(status) {
...
});
@dpkcse

This comment has been minimized.

Show comment
Hide comment
@dpkcse

dpkcse Jan 22, 2017

how it will use?

dpkcse commented Jan 22, 2017

how it will use?

@FlorianBador

This comment has been minimized.

Show comment
Hide comment
@FlorianBador

FlorianBador Feb 27, 2017

`

page.onResourceRequested = function(r,n){

	page.customHeaders = {};

};

`
This is where the customHeaders should be cleared, and NOT in onLoadFinished because all elements called by that first page would still have the referer instead of the page itself.
And you can always put a condition to call it only once for neatness but I doubt there would be any performance improvement.

FlorianBador commented Feb 27, 2017

`

page.onResourceRequested = function(r,n){

	page.customHeaders = {};

};

`
This is where the customHeaders should be cleared, and NOT in onLoadFinished because all elements called by that first page would still have the referer instead of the page itself.
And you can always put a condition to call it only once for neatness but I doubt there would be any performance improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment