I was wondering if anyone knew how to read the browser user agent from the AddOn script (not content script). I tried reading it using the preferences/service object:
var prefs = require("sdk/preferences/service");
var userAgent = prefs.get("general.useragent");
//userAgent is undefined
but I can’t seem to find a preference value that will serve up that information.
window.navigator.userAgent is the user agent, with caveats.
-
It is considered non-standard and the recommendation is not to use it. It is still supported by all the main browsers, but you may get deprecation warnings.
-
It may not be the user agent sent in http headers. The preference general.useragent.override will be used in http headers, but not in window.navigator.userAgent.
nsIHttpProtocolHandler defines a userAgent getter which will give you the one that goes in the http header. Most of the time! The general.useragent.override preference takes precedence.
There is an interface nsISiteSpecificUserAgent which has a method GetUserAgentForURIAndWindow(). You might try calling this yourself, which is what the navigator.userAgent getter does if there is a specific window and URI. If there is no window or URI, or no site-specific override, then the protocol handler is used.
1 Like