最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Identify tab that made request in Firefox Addon SDK - Stack Overflow

programmeradmin3浏览0评论

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

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

Share Improve this question edited Nov 12, 2011 at 7:27 Wladimir Palant 57.7k12 gold badges99 silver badges127 bronze badges asked Nov 11, 2011 at 18:52 RobRob 3,7562 gold badges36 silver badges46 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论