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
1 Answer
Reset to default 8Userscripts 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
版权声明:本文标题:javascript - How can I spoof my userAgent using a userscript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744100834a2590865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论