使用Chrome扩展程序更改navigator.userAgent

Sri*_*agi 7 html javascript google-chrome google-chrome-extension

我试图使用简单的chrome扩展名重载navigator.userAgent.由于内容脚本在隔离的env中运行,我尝试创建一个脚本元素并将逻辑写入此脚本.这发生在扩展的后台页面

chrome.tabs.query({
  active:!0
}, function(tabs) {
    var x = "window.navigator.__defineGetter__('userAgent', function() {" +
            "return 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D)" +
            " AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile " + 
            "Safari/535.19'; });console.log(navigator.userAgent);";

    for (var i = 0;i < tabs.length;i++) {
      var code = 'var s = document.createElement("script"); s.text = "' + x +
                 '"; document.head.insertBefore(s, document.head.firstChild);' + 
                 'navigator.userAgent ="s"; console.log(navigator.userAgent);';

      // Inject into the tabs of choice - currently everything.
      chrome.tabs.executeScript(tabs[i].id, {
        code: code
      });
    }
  });
Run Code Online (Sandbox Code Playgroud)

脚本附加了head元素,我可以看到UA字符串是通过在chrome的控制台中尝试navigator.userAgent而被欺骗的字符串,因此我相信导航器对象已经过载.

但这似乎不是有效的方式或根本没有发生,因为导航器对象没有更新,我发现 - http://www.quirksmode.org/js/detect.html仍然显示UA for Mac.

那么,我到底错过了什么?

Rob*_*b W 12

navigator.userAgent是一个只读属性.如果要更改navigator.userAgent,则需要创建新对象并复制属性,或者创建新对象并继承navigator并分配新的getter/setter.

我最近创建了这样一个扩展.我在Linux上,但我偶尔会下载Chrome for Windows.以下扩展程序会在Chrome的下载页面上将用户代理更改为Windows XP:

contentscript.js

var actualCode =  '(' + function() {
    'use strict';
    var navigator = window.navigator;
    var modifiedNavigator;
    if ('userAgent' in Navigator.prototype) {
        // Chrome 43+ moved all properties from navigator to the prototype,
        // so we have to modify the prototype instead of navigator.
        modifiedNavigator = Navigator.prototype;

    } else {
        // Chrome 42- defined the property on navigator.
        modifiedNavigator = Object.create(navigator);
        Object.defineProperty(window, 'navigator', {
            value: modifiedNavigator,
            configurable: false,
            enumerable: false,
            writable: false
        });
    }
    // Pretend to be Windows XP
    Object.defineProperties(modifiedNavigator, {
        userAgent: {
            value: navigator.userAgent.replace(/\([^)]+\)/, 'Windows NT 5.1'),
            configurable: false,
            enumerable: true,
            writable: false
        },
        appVersion: {
            value: navigator.appVersion.replace(/\([^)]+\)/, 'Windows NT 5.1'),
            configurable: false,
            enumerable: true,
            writable: false
        },
        platform: {
            value: 'Win32',
            configurable: false,
            enumerable: true,
            writable: false
        },
    });
} + ')();';

var s = document.createElement('script');
s.textContent = actualCode;
document.documentElement.appendChild(s);
s.remove();
Run Code Online (Sandbox Code Playgroud)

的manifest.json

{
    "name": "navigator.userAgent",
    "description": "Change navigator.userAgent to Windows on Chrome's download page.",
    "version": "1",
    "manifest_version": 2,
    "content_scripts": [{
        "run_at": "document_start",
        "js": ["contentscript.js"],
        "matches": [
            "*://www.google.com/intl/*/chrome/browser/*"
        ]
    }]
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在声明内容脚本而不是动态插入它,以确保我的代码在页面加载之前运行.此外,我正在使用此答案中描述的其中一个技巧来更改页面,navigator而不是navigator在隔离的内容脚本世界中更改其他页面.

请注意,这只会修改从JavaScript看到的userAgent.如果要修改发送到服务器的用户代理,请查看将自定义用户代理与特定Google Chrome页面/选项卡关联.