使用Chrome扩展程序更改navigator.userAgent [英] Changing navigator.userAgent using Chrome Extension

查看:2814
本文介绍了使用Chrome扩展程序更改navigator.userAgent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用简单的Chrome扩展来重载navigator.userAgent。由于内容脚本在独立的env中运行,我试图创建一个脚本元素并将逻辑写入该元素。这是从扩展的后台页面发生的。

  chrome.tabs.query({
active:!0
),函数(标签){
var x =window.navigator .__ defineGetter __('userAgent',function(){+
return'Mozilla / 5.0(Linux; Android 4.2.1; +
AppleWebKit / 535.19(KHTML,如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);';

//注入选项 - 当前所有选项卡。 $ b code:code
});
}
});

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



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



那么,我在这里错过了什么?

解决方案

navigator.userAgent 是一个只读属性。如果你想改变 navigator.userAgent ,那么你需要创建一个新对象并复制这些属性,或者创建一个新对象并继承导航器并分配一个新的getter / setter。



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


$ b

contentscript.js



  var actualCode ='('+ function(){
'use strict';
var navigator = window.navigator;
var modifiedNavigator;
if(Navigator.prototype中的'userAgent'){
// Chrome 43+将所有属性从导航器移动到原型,
//所以我们必须修改原型而不是导航器
modifiedNavigator = Navigator.prototype;

} else {
// Chrome 42-在导航器上定义了属性
modifiedNavigator = Object.create(navigator) ;
Object.defineProperty(window,'navigator',{
value:modifiedNavigator,
可配置:false,
enumerable:false,
可写:false
});
}
//假装为Windows XP
Object.defineProperties(modifiedNavigator,{
userAgent:{
value:navigator.userAgent.replace(/ \([^)] + \)/,'Windows NT 5.1'),
可配置:false,
enumerable:true,
可写:false
},
appVersion:{
值:navigator.appVersion .replace(/ \([^)] + \)/,'Windows NT 5.1'),
可配置:false,
enumerable:true,
可写:false
$,b $ b平台:{
值:'Win32',
可配置:false,
enumerable:true,
可写:false
},
});
} +')();';

var s = document.createElement('script');
s.textContent = actualCode;
document.documentElement.appendChild(s);
s.remove();



manifest.json



  {
name:navigator.userAgent,
description:在Chrome的下载页面上将navigator.userAgent更改为Windows。,
version:1,
manifest_version:2,
content_scripts:[{
run_at:document_start,
js :[contentscript.js],
匹配:[
*://www.google.com/intl/*/chrome/browser/*
]


code


$ b

正如你所看到的,我是声明内容脚本,而不是动态插入它,以确保我的代码在页面之前运行加载。此外,我使用的技巧之一在此答案中描述更改页面的导航器而不是其他一些导航器在独立的内容脚本世界中。



请注意,这只会修改从JavaScript中看到的userAgent。如果您想修改发送到服务器的用户代理,请查看将自定义用户代理关联到特定的Google Chrome页面/标签


I am trying to overload the navigator.userAgent using a simple chrome extension. As the content scripts operate in isolated env, I tried to create a script element and write the logic into this one. This happens from background page of the extension

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

The script gets appended for the head element and I can see that the UA string as the one that is spoofed by trying navigator.userAgent in the console of the chrome and so I believe that the navigator object was overloaded.

But this seems to be not the effective way or not happening at all as the navigator object was not updated which I found out via - http://www.quirksmode.org/js/detect.html which still shows the UA for Mac.

So, what exactly am I missing here?

解决方案

navigator.userAgent is a read-only property. If you want to change navigator.userAgent, then you need to either create a new object and copy the properties, or create a new object and inherit from navigator and assign a new getter/setter.

I've recently created such an extension. I'm on Linux, though I occasionally download the Chrome for Windows. The following extension changes the user agent to Windows XP on Chrome's download page:

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

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

As you can see, I'm declaring the content script instead of dynamically inserting it, to make sure that my code runs before the page is loaded. Further, I'm using one of the tricks described in this answer to change the page's navigator instead of some other navigator in the isolated content script world.

Note that this only modifies the userAgent as seen from JavaScript. If you want to modify the user agent that's sent to the server, take a look at Associate a custom user agent to a specific Google Chrome page/tab.

这篇关于使用Chrome扩展程序更改navigator.userAgent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆