开发者

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 开发者_StackOverflow中文版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...


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

alert(window.navigator.userAgent);


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜