javascript - Changing navigator.userAgent using Chrome Extension -


I am trying to overload Navigator.User Agent using a simple Chrome extension. As content scripts work in different EVs, I tried to create a script element and write arguments in this one. It comes from the background page of the extension

  chrome.tabs.query ({active:! 0}, function (tab) {var x = "window.navigator .__ definedgator___ ('useragent "," ("+" Back) Mozilla / 5.0 (Linux; Android 4.2.1; N-US; Nexus 5 Build / Jop 40D) "+" Apple WebKat / 535.19 (KHML, like GECO) Chrome / 18.0 .1025.166 Mobile "+" Safari / 535.19 ';}); console.log (navigator.userAgent); "(var i = 0; i & lt; tabs.length; i ++) {var code =' var s = document.createElement ("script"); s.text = "'+ x +'"; document.head.insertBefore (s, document.head.firstChild); '+' Vigator.usergent = "s"; console.log (navigator.userager); '; // Injected in the tab of choice - currently everything in. Chrome.tabs.executeScript (tab [i] .id, {code: code} };}});   

The script gets attached to the head element and I can see that the UA string is trying to navigate the console in Chrome's Chrome console. And so I Believe that the Navigator object was overloaded.

But it does not seem that this is an effective way or not because the Navigator object was not updated through which I came to know - which still shows UA for Mac.

So, what exactly am I missing here?

navigator.user agent is a read-only asset if you Navigator.User Agent , you will either have to create a new object, copy the properties, or create a new object, and make the successor from Navigator and assign a new one Will the Recipient / Setter

I have recently created such an extension. I'm on Linux, though I occasionally download chrome for Windows. On the download page of Chrome, the following extension explores the Windows user agent:

Infected. Js
  var actualCode = '(' + 'function () {' strict use '; var navigator = window; navigator; var revised navigator; if (Navigator. 'UserAgent' in Prototype) {// Chrome 43+ Navigator to Prototype has transferred all properties, // so we have to modify the prototype instead of the Navigator. Modified navigator = Navigator.prototype;} Other {/ / Chrome 42- Definition of Property on Navigator Modified Navigator = Object Create (Navigator) Object.DefinProperty (Window, Navigator, Value: Modified Navigator, Confidential: False, Countable: False, Writable: False}}} // Windows XP Object Diffin Properties (Modified Navigator, {User Agent: {Value: Navigator. EagerEgent.Relest (/ \ ([^)] + \) /, 'Windows NT 5.1', ​​configurable: incorrect, enumerable: true, written : False}, Confirmable: false, enumerable: true, writable: wrong}, platform: {value: navigator.app version. / ([^)] + \) /, 'Wind OS NT 5.1' 'Win32', Confidential: Wrong, Enumerable: True, Writable: Wrong},}); } + ') ();'; Var s = document.createElement ('script'); s.textContent = realCode; document.documentElement.appendChild (s); s.remove ();   

manifest.json
  {"name": "navigator.userAgent", "description": " Change the Navigator for Windows on the download page of Chrome. Change the user's Egent. "," Version ":" 1 "," manifest_version ": 2," content_script ": [{" run_at ":" document_start "," js ": [" Infected .Js "]," Matches ": [" *: //www.google.com/intl/*/chrome/browser/* "]}]}   

As you You can see, instead of dynamically inserting it, to ensure that my code runs before the page is loaded. Also, I'm using a trick to change the page code in Navigator instead of any other navigator in the different content script world.

Note that this only modifies the user agent as seen from Javascript. If you want to modify the user agent sent to the server, take a look.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -