-
-
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 | |
}); | |
} | |
} | |
} |
@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
sorry but can someone help me that where and how to use this code.