0

So I am working on a project that access a discord bot using a token in web browsers But recently, discord has changed their policy of bot requests, that make us have to manually change the user agent setting in the developer tools. Now I am wondering that is there any way that I can override the user agent setting of browser in JavaScript or HTML?

I have tried the method at here
For instance, after pasting the code:

setUserAgent(window, "BDP (http://example.com), v0.0.1)");

And it seems that it no loger work so I tried again with this
For instance:

Object.defineProperty(navigator, 'userAgent', {
    get: function () { return 'BDP (http://example.com), v0.0.1)'; }
});

But none of the method above helps me. Is there any way that I can override the user agent of browser?

Thanks!

YueYue
  • 13
  • 4
  • Does this answer your question? [How to change browser User-Agent Header with javaScript?](https://stackoverflow.com/questions/58005954/how-to-change-browser-user-agent-header-with-javascript) – Ivar Jan 18 '22 at 11:33
  • [navigator.userAgent](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgent) is a `read-only` property. – Nalin Ranjan Jan 18 '22 at 11:39
  • @Ivar I tried `XMLHttpRequest.setRequestHeader('userAgent', customUserAgent)` and its a no-go - or am I using it the wrong way? If so, can u give an example for that? Thanks! – YueYue Jan 18 '22 at 11:43
  • 1
    @YueYue What browser are you using? Did you check the note on [this page](https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name)? – Ivar Jan 18 '22 at 11:55
  • @Ivar I am using Opera, and firefox wont work too... – YueYue Jan 18 '22 at 12:04
  • 1
    After reading the MDN page @Ivar has linked, it looks like you could set `User-Agent` header with XHR/Fetch requests, notice the spelling. There's also a mention of a Chromium bug, and it's still listed as Forbidden header name at [its documentation page](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent), though. – Teemu Jan 18 '22 at 12:05

1 Answers1

-3

Please try using this code.. navigator.useragent returns the browser name..

if((navigator.userAgent.indexOf("BrowserName"))
{
    //write some code
}