我正在使用 Firefox Addon SDK构建一些东西来监视和显示浏览器中的 HTTP 流量。类似于 HTTPFox或 Live HTTP Headers .我有兴趣确定浏览器中的哪个选项卡(如果有)生成了请求
使用 observer-service我正在监视“http-on-examine-response”事件。我有如下代码来识别生成请求的 nsIDomWindow:
const observer = require("observer-service"),
{Ci} = require("chrome");
function getTabFromChannel(channel) {
try {
var noteCB= channel.notificationCallbacks ? channel.notificationCallbacks : channel.loadGroup.notificationCallbacks;
if (!noteCB) { return null; }
var domWin = noteCB.getInterface(Ci.nsIDOMWindow);
return domWin.top;
} catch (e) {
dump(e + "\n");
return null;
}
}
function logHTTPTraffic(sub, data) {
sub.QueryInterface(Ci.nsIHttpChannel);
var ab = getTabFromChannel(sub);
console.log(tab);
}
observer.add("http-on-examine-response", logHTTPTraffic);
大部分摘自 documentation for how to identify the browser that generated the request .一些也取自Google PageSpeed Firefox 插件。
从 nsIDOMWindow 是否有推荐或首选的方式?对象 domWin
到 SDK tabs 中的选项卡元素模块?
我曾考虑过一些骇人听闻的事情,比如扫描选项卡列表,寻找一个 URL 与 domWin 的 URL 相匹配的选项卡,但我不得不担心多个选项卡具有相同的 URL。
最佳答案
您必须继续使用内部包。据我所知,api-utils/lib/tabs/tab.js
包中的 getTabForWindow()
函数完全符合您的要求。未经测试的代码:
var tabsLib = require("sdk/tabs/tab.js");
return tabsLib.getTabForWindow(domWin.top);
关于javascript - 识别在 Firefox Addon SDK 中发出请求的选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8098580/