本文介绍了在Chrome,Firefox或IE上更改navigator.platform以测试操作系统检测代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Chrome,Firefox或Internet Explorer(最好是Chrome)上欺骗navigator.platform的值?看起来它曾经可以在Firefox上原生使用,但是对此已经放弃了支持.

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.

这是为了在以条件JavaScript检查运行的站点上测试某些代码,该条件检查了navigator.platform属性.不幸的是,它没有测试易于更改的userAgent.

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.

我尝试根据 https://groups.google.com/a/chromium.org/forum/#!topic/chromium-discuss/8cCllrVX4kI ,但是它不起作用(我在代码中包含了尝试以下).如果我在扩展程序中执行console.log(navigator.platform),它会根据需要打印出"MacIntel",但是如果我在页面加载后在控制台中键入navigator.platform,则会显示"Win32"(即我所使用的实际操作系统)不想说).

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

推荐答案

信贷@wOxxOm和 https://stackoverflow.com/a/9517879/4811197 -我将问题中的navigator_change.js代码更新为以下代码,并且可以正常工作.

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

这篇关于在Chrome,Firefox或IE上更改navigator.platform以测试操作系统检测代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 00:10