Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change navigator.platform on Chrome, Firefox, or IE to test OS detection code

How can I spoof the value of navigator.platform on Chrome, Firefox, or Internet Explorer (preferably Chrome)? It looks like it used to be possible on Firefox natively but that support was dropped for that.

This is to test some code on a site which runs in a conditional JavaScript check which tests the navigator.platform property. Unfortunately it's not testing userAgent which would be easy to change.

I tried writing a simple chrome extension per the suggestion in the second post on https://groups.google.com/a/chromium.org/forum/#!topic/chromium-discuss/8cCllrVX4kI but it doesn't work (I included the code I tried below). If I do console.log(navigator.platform) in the extension, it prints out "MacIntel" as desired but if I type navigator.platform in the console after page load it says "Win32" (i.e. the actual OS which I'm not wanting it to say).

//navigator_change.js
Object.defineProperty(navigator,"platform", {
  get: function () { return "MacIntel"; },
  set: function (a) {}
 });

//manifest.json
{
    "manifest_version": 2,
    "content_scripts": [ {
        "js":        [ "navigator_change.js" ],
        "matches":   [ "<all_urls>"],
        "run_at":    "document_start"
    } ],
    "converted_from_user_script": true,
    "description":  "Fake navigator.platform",
    "name":         "MacFaker",
    "version":      "1"
}
like image 209
g491 Avatar asked Aug 06 '16 21:08

g491


People also ask

How can we detect the client browser name?

How do I detect the browser name ? You can use the navigator. appName and navigator. userAgent properties.

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 a navigator platform?

The Navigator platform property is used for returning the platform for which the browser is compiled. It returns a string representing the platform of the browser.


Video Answer


2 Answers

Credit @wOxxOm and https://stackoverflow.com/a/9517879/4811197 - I updated the navigator_change.js code in the question to the following and it works.

var codeToInject = 'Object.defineProperty(navigator,"platform", { \
  get: function () { return "MacIntel"; }, \
  set: function (a) {} \
 });';
var script = document.createElement('script');
script.appendChild(document.createTextNode(codeToInject));
(document.head || document.documentElement).appendChild(script);
script.parentNode.removeChild(script);
like image 85
g491 Avatar answered Oct 14 '22 04:10

g491


Although this is an old question, the property can be overridden since webdriver version 85 for Chromium-based browsers.

If you're trying to override the navigator.platform value for automation through (in)direct usage of webdriver, you can send the Emulation.setUserAgentOverride command with proper values to do so.

As an example, I have implemented the following code using the php-webdriver package:

/**
 * @param string $userAgent
 * @param array|null $acceptLanguage
 * @param string|null $platform
 * @return mixed|null
 */
function setClientUserAgent(string $userAgent, ?array $acceptLanguage, ?string $platform)
{
    $params = ['userAgent' => $userAgent];

    if ($acceptLanguage) $params['acceptLanguage'] = join(',', $acceptLanguage);
    if ($platform) $params['platform'] = $platform;

    return $browser->executeCustomCommand(
        '/session/:sessionId/goog/cdp/execute',
        'POST',
        [
            'cmd' => 'Emulation.setUserAgentOverride',
            'params' => $params
        ]
    );
}

In this code, $browser is a reference to the RemoteWebDriver instance created to operate the automation.

Complete documentation for the Chrome DevTools Protocol can be found here, and for more information regarding to the php-webdriver package, you can visit their GitHub repository here.

like image 38
Farsad Avatar answered Oct 14 '22 05:10

Farsad