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

commented Oct 28, 2018

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

@NickPiscitelli

This comment has been minimized.

Copy link

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

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.

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.