3

I'm trying to visit a website but its not allowing me to do so because it doesn't support my browser. I believe it is detecting my browser through userAgent detection. So I wanted to create a userScript that would modify my userAgent so that the website wouldn't be able to detect my browser. I tried:

// ==UserScript==
// @name         Change UserAgent
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Spoofs the userAgent
// @author       You
// @include      *
// @run-at       document-start
// ==/UserScript==

Object.defineProperty(navigator, 'userAgent', {
  value: "MyCustomUserAgent",
  configurable: false
});

Even though it shows for me that the userAgent is a custom value, I believe the request for the userAgent is being done before I can spoof it. Is there any way in which I can do so without using an extension? Thank you.

Swangie
  • 175
  • 8
  • 1
    Why not use the built-in browser tools? They allow user-agent spoofing. --- Honestly websites need to stop this "we don't support your browser" bs. – evolutionxbox Jan 19 '21 at 14:05
  • @evolutionbox, I could run the userscript on different websites which don't support my browser, but just disable it for other websites which don't do this sort of thing. – Swangie Jan 19 '21 at 14:18
  • May this can help? https://superuser.com/questions/445869/can-i-switch-the-user-agent-header-for-only-a-single-web-site – evolutionxbox Jan 19 '21 at 14:20
  • @evolutiobox The thing is general.userAgent.override doesn't work since I'm not using firefox and while a userAgent spoofer extension is quite useful, I was hoping there was a simpler way with a userScript. Thanks for the help though! – Swangie Jan 19 '21 at 14:26
  • Well of course, the request is done before you change the useragent. You are probably running this script on the page resulting from that request. There are many browserplugins for most of the common browsers which allow user agent spoofing. Or you could even write one yourself. But executing code an an already loaded page won't help, because 1) it's too late 2) it's gone, once the page is refreshed ... – derpirscher Jan 19 '21 at 14:34

1 Answers1

6

Userscripts are loaded after the initial HTTP/s connections are made and page is about to load.

By that time, the server has already received the user-agent data. Therefore, a userscript can not spoof the server.

Add-ons can intercept & alter initial communication between the browser & server and thus spoof the user-agent.

Websites that use their own servers (e.g. Google, Yahoo, Facebook etc) have access to their server so spoofing them via userscript is less likely (depending on other factors).

Websites that run on commercial servers may not have access to above server data and have to use JavaScript to get the user-agent, therefore there is a possibility to spoof the user-agent.

Similarly, websites that use page JavaScript that is executed later (e.g. on an event when something is clicked) to obtain the user-agent at that time, can be spoofed with a user-script.

erosman
  • 7,094
  • 7
  • 27
  • 46