8

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"
}
g491
  • 341
  • 1
  • 3
  • 12
  • 1
    Variables/objects/functions in content scripts are isolated from the webpage, so you need to inject a DOM script element: [Building a Chrome Extension - Inject code in a page using a Content script](https://stackoverflow.com/q/9515704) – wOxxOm Aug 06 '16 at 21:49
  • 1
    @wOxxOm Thanks - I followed your suggestion and it works. I posted the complete code as an answer. – g491 Aug 06 '16 at 22:00

2 Answers2

14

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);
Community
  • 1
  • 1
g491
  • 341
  • 1
  • 3
  • 12
  • are you suggesting to do an extension? – Toolkit Sep 17 '17 at 11:53
  • extensions are unsupported in headless chrome, so I don't think this is sustainable for any large-scale automated testing. Best bet is to somehow modify the DOM when the page is launched, I'm not sure how but this page seems like a good lead - https://intoli.com/blog/making-chrome-headless-undetectable/ – Heartthrob_Rob Jul 18 '20 at 23:08
1

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.

Farsad
  • 501
  • 5
  • 14