admin管理员组

文章数量:1394526

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    /
// @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.

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/
// @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.

Share Improve this question edited Jan 19, 2021 at 16:12 Swangie asked Jan 19, 2021 at 14:02 SwangieSwangie 2053 silver badges8 bronze badges 5
  • 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 Commented Jan 19, 2021 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 Commented Jan 19, 2021 at 14:18
  • May this can help? superuser./questions/445869/… – evolutionxbox Commented Jan 19, 2021 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 Commented Jan 19, 2021 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 mon 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 Commented Jan 19, 2021 at 14:34
Add a ment  | 

1 Answer 1

Reset to default 8

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 munication 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 mercial 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.

本文标签: javascriptHow can I spoof my userAgent using a userscriptStack Overflow