KaA6438
KaA6438

Reputation: 108

How to find out browser's user agent in Firefox Add On

I'm creating (developing) a Firefox add-on and I need to find browser's user agent. The navigator.userAgent is not working. It shows that the navigator is not defined.

What Firefox Add-on module do I need to get user agent or is there another way to find it out in an add-on?

Upvotes: 4

Views: 662

Answers (1)

nmaier
nmaier

Reputation: 33202

In the SDK, first you need the chrome authority for Cc and Ci:

const {Cc, Ci} = require("chrome");

The you can use the nsIHttpProtocolHandler to get the user agent from there:

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

console.log(httpproto.userAgent);

Using the hidden window and hacks like that will work too, for now, but that's somewhat messy and might be problematic in the multi-process future.

Upvotes: 5

Related Questions