问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/28715514/how-to-find-out-browsers-user-agent-in-firefox-add-on