Rob
Rob

Reputation: 3746

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. 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.

Upvotes: 5

Views: 1719

Answers (4)

Yuchen Zhou
Yuchen Zhou

Reputation: 1649

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.

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57691

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);

Upvotes: 3

rednoyz
rednoyz

Reputation: 1327

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);

Upvotes: 3

esgy
esgy

Reputation: 392

As of Addon SDK version 1.13 change:

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

to

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

Upvotes: 0

Related Questions