Fake navigator properties
Asked Answered
S

1

19

I would like to fake Navigator platform property of CasperJS(/PhantomJS). I found the easy solutions of overwriting the Navigator Object at page load which is suggested in many other places on the web:

    casper.on('page.initialized', function(){
    this.evaluate(function(){
        (function(oldNav){
            var newNav = {};
            [].forEach.call(Object.getOwnPropertyNames(navigator), function(prop){
                 if (prop === 'platform') {
                    Object.defineProperty(newNav, prop, {
                        value: 'Win64'
                    }); }else {
                    Object.defineProperty(newNav, prop, {
                        get: function(){
                            return oldNav[prop];
                        }
                    });
                }
            });
            window.navigator = newNav;
        })(window.navigator);
    });
});

But the problem is that if we get the Navigator properties from an Iframe, the values are still the original one because, the page.initialized only set it for the main page. So I opted to change it in its source code and build it again. I downloaded Phantomjs from the git repo, and I searched for a hardcoded platform value(Linux x86_64 for my case). I found the hardcoded string in ./phantomjs/src/qt/qtwebkit/Source/WebCore/platform/qt/UserAgentQt.cpp

I changed it to the string I wanted to be returned as the navigator.platform, but it did not affect the navigator.platform. Where should I change it? Is it(platform) a harcoded string or it is created dynamically?

Speos answered 8/7, 2016 at 1:9 Comment(3)
I would suggest to change casperjs or overwrite some methods instead of build an own version of PhantomJS. Perhaps you have more luck asking your question in their Google Group groups.google.com/forum/#!forum/casperjsPotato
Thanks for your comment. I did that.Speos
Perhaps you can post your solution as answer.Potato
S
4

After reviewing the code, I found out that the following file should be changed:

src/qt/qtwebkit/Source/WebCore/page/NavigatorBase.cpp

and NavigatorBase::platform() should be set to the desired string you would like to be returned as the navigator.platform. But I'm not sure if it will mess up other things, please give suggestions if it is not an appropriate solution.

Speos answered 11/7, 2016 at 1:30 Comment(1)
So how did it go?Rackrent

© 2022 - 2024 — McMap. All rights reserved.