Identify tab that made request in Firefox Addon SDK

只愿长相守 提交于 2019-12-30 02:27:06

问题


I'm using the Firefox Addon SDK to build something that monitors and displays the HTTP traffic in the browser. Similar to HTTPFox or Live HTTP Headers. 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. I have code like the following to identify the nsIDomWindow that generated the request:


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.

Is there a recommended or preferred way to go from the nsIDOMWindow object domWin to a tab element in the SDK tabs module?

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.


回答1:


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. Untested code:

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



回答2:


The API has changed since this was originally asked/answered... It should now (as of 1.15) be:

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



回答3:


As of Addon SDK version 1.13 change:

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

to

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




回答4:


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

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.

For example, a_tab will not have the 'id' attribute, but would have linkedPanel property that's similar to the 'id' attribute.



来源:https://stackoverflow.com/questions/8098580/identify-tab-that-made-request-in-firefox-addon-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!