Changing navigator.userAgent using Chrome Extension

后端 未结 2 885
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 12:18

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

相关标签:
2条回答
  • 2020-12-05 12:53

    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.

    0 讨论(0)
  • 2020-12-05 13:13

    Are you trying to change the User-Agent header that gets sent in requests? You'll have to use the declarativeWebRequest or webRequest APIs.

    Running content scripts in the page only occurs after the requests have been sent.

    0 讨论(0)
提交回复
热议问题