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"
}
How do I detect the browser name ? You can use the navigator. appName and navigator. userAgent properties.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With