Howto set or overwrite document.referrer?

9,159 views
Skip to first unread message

Paul Porzky

unread,
Aug 24, 2012, 9:54:19 AM8/24/12
to phan...@googlegroups.com
Hi there,
i am working on some test cases for our piwik installation and ran into a problem with custom referrers. Maybe you can point me into the right direction...
While it is possible to set a custom Referrer using page.customHeaders (verified using wireshark), it is not accessible in the context of the web page using "document.referrer".

Here is a simple test case. Is it possible to manually overwrite "document.referrer" from within the phantomjs script ?

var page = require('webpage').create();
var url = "http://example.com/";

console.log('Loading ' + url);

page.customHeaders = {
        "Referer" : "http://referrer.example.com/"
};

page.onLoadFinished = function(status){
var referrer = page.evaluate(function () {
       return document.referrer;
    });
    console.log('document.referrer is ' + referrer);
    phantom.exit();
};

page.open(url);


Thanks a lot and
Best Regards,
Paul

Paul Porzky

unread,
Aug 25, 2012, 5:31:11 PM8/25/12
to phan...@googlegroups.com
i found at least a workaround to fake the referrer by injecting a link into the page i want to show as the referrer and then simulate a click on it. I've shared the script here: https://gist.github.com/3453137

This however involves loading one more page and can lead to difficulties in case of errors while loading.

Best Regards,
Paul

Ariya Hidayat

unread,
Aug 25, 2012, 11:58:37 PM8/25/12
to phan...@googlegroups.com
I am not an expert with the referrer but you might want to use
onInitialized trick to override its value. Check the latest
mailing-list posts for details.

Regards,


--
Ariya Hidayat, http://ariya.ofilabs.com
http://twitter.com/ariyahidayat

Paul Porzky

unread,
Aug 26, 2012, 1:29:31 PM8/26/12
to phan...@googlegroups.com
Hi Ariya,

thanks for your quick reply & hint! I tried the onInitialized trick but window.document.referrer does not want to play with me [1].
I am currently browsing the source and it looks like it boils down to the use of QNetworkRequest instead of ResourceRequest in WebPage::openUrl [2]. 

String Document::referrer() const [3] relies on the FrameLoaders ResourceRequest's function httpReferrer to return the referrer:

String httpReferrer() const { return httpHeaderField("Referer"); }[4]

It seems to me that QNetworkRequest's request.setRawHeader() does not have an impact on these Fields. I am currently trying to replace QNetworkRequest with ResourceRequest in webpage.cpp.

Regards,
Paul
--

[1] Try to overwrite window.document.referrer: https://gist.github.com/3481673
[2] webpage.cpp line 538
[3] Document.cpp line 3669
[4] ResourceRequestBase.h line 109
 

Paul Porzky

unread,
Aug 26, 2012, 5:25:36 PM8/26/12
to phan...@googlegroups.com
Hi again,

my above assumption was wrong. Instead, Webpage::openUrl just did not pass the custom header data (as set by WebPage::setCustomHeaders) to the request object.

Let me know if you want me to send a pull request or open an issue.

Thanks,
Paul

Pierre M.

unread,
Apr 30, 2014, 11:54:43 AM4/30/14
to phan...@googlegroups.com
Hi Paul,

I am interested by the patch that you created (https://github.com/papoms/phantomjs/compare/set-custom-headers-within-request), but at this day no pull request was created on the main phantomJS repository.
As it is your creation, I wanted to ask if you could send a pull request ?
Or may I fork the project myself and ask for this request ?

Thanks a lot,
Regards,
Pierre

Sam Attah

unread,
Apr 15, 2016, 5:42:04 AM4/15/16
to phantomjs
Hi, I need some help with this. Would you mind helping? 

Paul Porzky

unread,
Apr 15, 2016, 5:56:50 AM4/15/16
to phantomjs

Sure, what is it ?

Mahesh

unread,
Apr 2, 2017, 1:41:10 AM4/2/17
to phantomjs
Hi Paul,

Could you please help me to change the referrer manually to  some other with php or javascript ?
Reply all
Reply to author
Forward
0 new messages