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.

polychroma-co commented Oct 28, 2018

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

@NickPiscitelli

This comment has been minimized.

NickPiscitelli commented Nov 24, 2018

It's passed as an argument to the function.

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

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