Skip to content

Instantly share code, notes, and snippets.

@thorsten
Created May 9, 2016 15:12
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thorsten/148812e9cc4fb6a19215ce22afd4e5a8 to your computer and use it in GitHub Desktop.
Save thorsten/148812e9cc4fb6a19215ce22afd4e5a8 to your computer and use it in GitHub Desktop.
Override user agent on all browsers
function setUserAgent(window, userAgent) {
// Works on Firefox, Chrome, Opera and IE9+
if (navigator.__defineGetter__) {
navigator.__defineGetter__('userAgent', function () {
return userAgent;
});
} else if (Object.defineProperty) {
Object.defineProperty(navigator, 'userAgent', {
get: function () {
return userAgent;
}
});
}
// Works on Safari
if (window.navigator.userAgent !== userAgent) {
var userAgentProp = {
get: function () {
return userAgent;
}
};
try {
Object.defineProperty(window.navigator, 'userAgent', userAgentProp);
} catch (e) {
window.navigator = Object.create(navigator, {
userAgent: userAgentProp
});
}
}
}
@polychroma-tv
Copy link

Silly question - but wondering where I'd put the custom useragent?

@NickPiscitelli
Copy link

It's passed as an argument to the function.

setUserAgent(window, "My Custom User Agent");

@odebroqueville
Copy link

It doesn't seem to be working in firefox any longer as navigatorID.userAgent is read only property. I got the following error message:
TypeError: "setting getter-only property "navigator""
I would suppose that the property general.useragent.override would have to be set instead of navigator.useragent.

@sd101
Copy link

sd101 commented Jan 13, 2020

so what parts are you supposed to change if you want to set it to IE10?

@methodbox
Copy link

so what parts are you supposed to change if you want to set it to IE10?

You would change the userAgent value to one that is compatible where IE10 is not. The point is to fool, in your case, IE10, into saying it's Chrome, for example.

Unless you're trying to fool Chrome into saying it's IE10 (for what reason, I have no idea) in which case you'd pass the UA of Chrome.

@reed123
Copy link

reed123 commented Mar 21, 2020

Thank you this helped alot!

@himanshugpt3
Copy link

sorry but can someone help me that where and how to use this code.

@myjobistobehappy
Copy link

@himanshugpt3, What do you need help with? You can copy and paste this code into your <script type="application/javascript"></script>. Then you can call it when you like with

setUserAgent(window, "My Custom User Agent");

@userbox020
Copy link

hello, im running this script and so many others to modify the user agent with javascript and when running the "return navigator.userAgent" it returns my custom user agent but when I load any website to test my user agent it detects my original user agent.
Does anyone could tell me what im doing wrong?
thanks

@Noext
Copy link

Noext commented Dec 21, 2021

not working, navigator.defineGetter dont exist

@avalanche1
Copy link

Since long time ago browsers have prohibited spoofing User-Agent header in http requests.
No matter what you do, the browse still gonna send the real UA header.
Two methods to overcome that situation:

  1. Use browser extension, that modifies outgoing http request headers (webRequest persmission)
  2. Send the request from a server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment