-
-
Save thorsten/148812e9cc4fb6a19215ce22afd4e5a8 to your computer and use it in GitHub Desktop.
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 | |
}); | |
} | |
} | |
} |
It's passed as an argument to the function.
setUserAgent(window, "My Custom User Agent");
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.
so what parts are you supposed to change if you want to set it to IE10?
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.
Thank you this helped alot!
sorry but can someone help me that where and how to use this code.
@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");
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
not working, navigator.defineGetter dont exist
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:
- Use browser extension, that modifies outgoing http request headers (
webRequest
persmission) - Send the request from a server
Silly question - but wondering where I'd put the custom useragent?