3

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...

Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67

3 Answers3

4

Apparently Firefox changed how this was done in version 4. Have you tried:

alert(window.navigator.userAgent);
Anthony
  • 36,459
  • 25
  • 97
  • 163
3

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;
Matthew Gertner
  • 4,487
  • 2
  • 32
  • 54
  • Same works in C++ of course: `nsCOMPtr httpHandler(do_GetService("@mozilla.org/network/protocol;1?name=http")); httpHandler->GetUserAgent(userAgent);` – Matthew Gertner Jul 26 '11 at 12:43
  • happen to know what do_GetService does? I've got this for now but throws an unsupported interface: `NS_GetServiceManager(SvcMgr);SvcMgr.GetServiceByContractID(NS_IHTTPPROTOCOLHANDLER_CONTRACT,nsIHttpProtocolHandler,h);` – Stijn Sanders Aug 13 '11 at 13:24
  • Are you defining NS_IHTTPPROTOCOLHAND‌​LER_CONTRACT yourself? What is the value? – Matthew Gertner Aug 14 '11 at 05:59
  • `@mozilla.org/network/protocol;1?name=http` in the mean time I've discovered nsIHttpProtocolHandler has been changing IID's in recent versions. I'll try to pick up the right one from http://mxr.mozilla.org/ ... – Stijn Sanders Aug 17 '11 at 20:43
  • yes! got this one to work: http://mxr.mozilla.org/mozilla-beta/source/netwerk/protocol/http/nsIHttpProtocolHandler.idl Don't know if this one is going to keep changing with each release? perhaps I should propose a nsIUserAgentBuilder or something. – Stijn Sanders Aug 17 '11 at 20:51
  • They change the IID if the interface changes. Best thing is to use JavaScript if at all possible. – Matthew Gertner Aug 18 '11 at 05:54
  • I guess I'm stuck with testing (and updating the rdf) with each FF release, so I'll check the IID each time. I'll fallback to the firefox.exe version info just in case, it's not that important really. Not sure how to call javascript from XPCOM, though, but that's a totally different crusade... – Stijn Sanders Aug 18 '11 at 16:26
  • https://developer.mozilla.org/en/How_to_Build_an_XPCOM_Component_in_Javascript. But frankly I'd be surprised if you can't get by with writing everything is JS and using js-ctypes to call into native code. – Matthew Gertner Aug 18 '11 at 17:00
1

If for some reason you actaully do need to use NPAPI like you suggest in your tags, you can use NPN_UserAgent to get it; however, I would be shocked if you actually needed to do that just for an extension. Most likely Anthony's answer is more what you're looking for.

taxilian
  • 14,229
  • 4
  • 34
  • 73