最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change navigator.platform on Chrome, Firefox, or IE to test OS detection code - Stack Overflow

programmeradmin2浏览0评论

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 /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 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"
}
Share Improve this question asked Aug 6, 2016 at 21:43 g491g491 3811 gold badge4 silver badges13 bronze badges 2
  • 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 – woxxom Commented Aug 6, 2016 at 21:49
  • 1 @wOxxOm Thanks - I followed your suggestion and it works. I posted the complete code as an answer. – g491 Commented Aug 6, 2016 at 22:00
Add a comment  | 

2 Answers 2

Reset to default 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);

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论