如何在Firefox中添加浏览器的用户代理 [英] How to find out browser's user agent in Firefox Add On
问题描述
我正在创建(开发)一个Firefox插件,我需要找到浏览器的用户代理。
navigator.userAgent
不起作用。它显示没有定义 navigator
。
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.
我需要什么Firefox附加模块来获取用户代理或者有另一种方法来找到它在附加?
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?
推荐答案
在SDK中,首先你需要 chrome权限 Ci
:
In the SDK, first you need the chrome authority for Cc
and Ci
:
const {Cc, Ci} = require("chrome");
您可以使用 nsIHttpProtocolHandler
用户代理:
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.
这篇关于如何在Firefox中添加浏览器的用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!