简体   繁体   English

识别在Firefox插件SDK中发出请求的选项卡

[英]Identify tab that made request in Firefox Addon SDK

I'm using the Firefox Addon SDK to build something that monitors and displays the HTTP traffic in the browser. 我正在使用Firefox插件SDK来构建一些东西来监视和显示浏览器中的HTTP流量。 Similar to HTTPFox or Live HTTP Headers . 类似于HTTPFoxLive HTTP标头 I am interested in identifying which tab in the browser (if any) generated the request 我有兴趣确定浏览器中的哪个标签(如果有)产生了请求

Using the observer-service I am monitoring for "http-on-examine-response" events. 使用观察者服务,我正在监视“ http-on-examine-response”事件。 I have code like the following to identify the nsIDomWindow that generated the request: 我有类似以下的代码来标识生成请求的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);

Mostly cribbed from the documentation for how to identify the browser that generated the request . 大多是在文档中寻找如何识别生成请求的浏览器 Some is also taken from the Google PageSpeed Firefox addon. 其中一些也来自Google PageSpeed Firefox插件。

Is there a recommended or preferred way to go from the nsIDOMWindow object domWin to a tab element in the SDK tabs module? 是否存在从nsIDOMWindow对象domWin转到SDK选项卡模块中的选项卡元素的推荐或首选方法?

I've considered something hacky like scanning the tabs list for one with a URL that matches the URL for domWin, but then I have to worry about multiple tabs having the same URL. 我已经考虑过一些骇人听闻的事情,例如扫描选项卡列表中的URL与domWin的URL匹配的URL,但是我不得不担心多个选项卡具有相同的URL。

自最初提出/回答以来,API发生了变化...现在(自1.15开始)应为:

return require("sdk/tabs/utils").getTabForWindow(domWin.top);

You have to keep using the internal packages. 您必须继续使用内部软件包。 From what I can tell, getTabForWindow() function in api-utils/lib/tabs/tab.js package does exactly what you want. 据我所知, api-utils/lib/tabs/tab.js包中的getTabForWindow()函数完全api-utils/lib/tabs/tab.js您的要求。 Untested code: 未经测试的代码:

var tabsLib = require("sdk/tabs/tab.js");
return tabsLib.getTabForWindow(domWin.top);

As of Addon SDK version 1.13 change: 从Addon SDK版本1.13开始更改:

var tabsLib = require("tabs/tab.js");

to

var tabsLib = require("sdk/tabs/helpers.js");

If anyone still cares about this: 如果仍然有人在乎这个:

Although the Addon SDK is being deprecated in support of the newer WebExtensions API, I want to point out that 尽管不赞成使用Addon SDK来支持更新的WebExtensions API,但我想指出一点

var a_tab = require("sdk/tabs/utils").getTabForContentWindow(window) var a_tab = require(“ sdk / tabs / utils”)。getTabForContentWindow(window)

returns a different 'tab' object than the one you would typically get by using 返回与您通常使用的对象不同的“标签”对象

worker.tab in a PageMod. PageMod中的worker.tab。

For example, a_tab will not have the 'id' attribute, but would have linkedPanel property that's similar to the 'id' attribute. 例如,a_tab将不具有'id'属性,但具有与'id'属性相似的linkedPanel属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM