Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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-co

This comment has been minimized.

Copy link

polychroma-co commented Oct 28, 2018

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

@NickPiscitelli

This comment has been minimized.

Copy link

NickPiscitelli commented Nov 24, 2018

It's passed as an argument to the function.

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

@odebroqueville

This comment has been minimized.

Copy link

odebroqueville commented Mar 19, 2019

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

This comment has been minimized.

Copy link

sd101 commented Jan 13, 2020

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

@methodbox

This comment has been minimized.

Copy link

methodbox commented Feb 3, 2020

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

This comment has been minimized.

Copy link

reed123 commented Mar 21, 2020

Thank you this helped alot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.