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!

6
  • Does this answer your question? How to change browser User-Agent Header with javaScript? Commented Jan 18, 2022 at 11:33
  • navigator.userAgent is a read-only property. Commented Jan 18, 2022 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! Commented Jan 18, 2022 at 11:43
  • 1
    @YueYue What browser are you using? Did you check the note on this page? Commented Jan 18, 2022 at 11:55
  • 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, though. Commented Jan 18, 2022 at 12:05

1 Answer 1

-4

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

You seem to have confused "set" with "read"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.