简体   繁体   English

在 Chrome、Firefox 或 IE 上更改 navigator.platform 以测试操作系统检测代码

[英]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)?如何在 Chrome、Firefox 或 Internet Explorer(最好是 Chrome)上欺骗 navigator.platform 的值? It looks like it used to be possible on Firefox natively but that support was dropped for that.看起来它曾经可以在 Firefox 本机上使用,但为此放弃了支持。

This is to test some code on a site which runs in a conditional JavaScript check which tests the navigator.platform property.这是为了测试站点上的一些代码,该站点在测试 navigator.platform 属性的条件 JavaScript 检查中运行。 Unfortunately it's not testing userAgent which would be easy to change.不幸的是,它没有测试易于更改的 userAgent。

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).我尝试根据https://groups.google.com/a/chromium.org/forum/#!topic/chromium-discuss/8cCllrVX4kI上第二篇文章中的建议编写一个简单的 chrome 扩展,但它不起作用(我包括我在下面尝试的代码)。 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" (ie the actual OS which I'm not wanting it to say).如果我在扩展中执行 console.log(navigator.platform),它会根据需要打印出“MacIntel”,但是如果我在页面加载后在控制台中输入 navigator.platform,它会显示“Win32”(即我使用的实际操作系统)不想说)。

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

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.归功于@wOxxOm 和https://stackoverflow.com/a/9517879/4811197 - 我将问题中的 navigator_change.js 代码更新为以下内容并且它有效。

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.尽管这是一个老问题,但自基于 Chromium 的浏览器的 webdriver 版本 85 以来,该属性可以被覆盖。

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.如果您尝试通过(in)直接使用 webdriver 来覆盖navigator.platform的自动化值,您可以发送具有适当值的Emulation.setUserAgentOverride命令来执行此操作。

As an example, I have implemented the following code using the php-webdriver package:例如,我使用php-webdriver包实现了以下代码:

/**
 * @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.在此代码中, $browser是对为操作自动化而创建的RemoteWebDriver实例的引用。

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 .可以在此处找到 Chrome DevTools 协议的完整文档,有关 php-webdriver 包的更多信息,您可以在此处访问其 GitHub 存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM