Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fake navigator properties

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?

like image 231
Alex Avatar asked Jul 08 '16 01:07

Alex


People also ask

Can navigator be undefined?

js without a DOM emulation setup you will run into an error because navigator is undefined. You could mock navigator of course before mocking userAgent , but then for your test to pass you would also need to mock navigator. mediaDevices and navigator. mediaDevices.

What does the navigator property do?

Property of JavaScript navigator objectreturns the language. It is supported in Netscape and Firefox only. returns the user language. It is supported in IE only.

What is navigator userAgent?

The Navigator userAgent property is used for returning the user-agent header's value sent to the server by the browser. It returns a string representing values such as the name, version, and platform of the browser.

What is navigator used for in Windows?

The Navigator interface represents the state and the identity of the user agent. It allows scripts to query it and to register themselves to carry on some activities. A Navigator object can be retrieved using the read-only window. navigator property.


1 Answers

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.

like image 85
Alex Avatar answered Oct 11 '22 10:10

Alex