Created
May 9, 2016 15:12
-
-
Save thorsten/148812e9cc4fb6a19215ce22afd4e5a8 to your computer and use it in GitHub Desktop.
Override user agent on all browsers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
webRequest
persmission)