简体   繁体   中英

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.

自最初提出/回答以来,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. Untested code:

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

As of Addon SDK version 1.13 change:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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