g491
g491

Reputation: 381

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"
}

Upvotes: 10

Views: 21238

Answers (2)

Farsad
Farsad

Reputation: 521

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.

Upvotes: 1

g491
g491

Reputation: 381

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);

Upvotes: 14

Related Questions