Search code examples
firefoxfirefox-addonxpcomnpapi

How to obtain firefox user agent string?


I'm building an add-on for FireFox that simulates a website, but running from a local library. (If you want to know more, look here)

I'm looking for a way to get a hold of the user-agent string that FireFox would send if it were doing plain http. I'm doing the nsIProtocolHandler myself and serve my own implementation of nsIHttpChannel, so if I have a peek at the source, it looks like I'll have to do all the work myself.

Unless there's a contract/object-id on nsHttpHandler I could use to create an instance just for a brief moment to get the UserAgent? (Though I notice I'll need to call Init() because it does InitUserAgentComponents() and hope it'll get to there... And I guess the http protocol handler does the channels and handlers so there won't be a contract to nsHttpHandler directly.)

If I have a little peek over the wall I notice this globally available call ObtainUserAgentString which does just this in that parallel dimension...


Solution

  • You can get it via XPCOM like this:

    var httpHandler = Cc["@mozilla.org/network/protocol;1?name=http"].
      getService(Ci.nsIHttpProtocolHandler);
    var userAgent = httpHandler.userAgent;