prongs
prongs

Reputation: 9606

firefox addon packet sniffer

I want to sniff the packets with my addon. I am using this Stack overflow question as hint. My full code is this:

// This is an active module of the Add on
exports.main = function() {
var {Cc, Ci,Cu} = require("chrome");
var widgets = require("widget");
var windows = require("windows").browserWindows;

Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 

XPCOMUtils.defineLazyServiceGetter(this, "activityDistributor",
                                       "@mozilla.org/network/http-activity-distributor;1",
                                       "nsIHttpActivityDistributor");

    let httpTrafficObserver = {

      /**
       * Begin observing HTTP traffic that we care about,
       * namely traffic that originates inside any context that a Heads Up Display
       * is active for.
       */
      startHTTPObservation: function httpObserverFactory()
      {
        // creates an observer for http traffic
        var self = this;
        var httpObserver = {
          observeActivity :
          function observeActivity(aChannel,
                                   aActivityType,
                                   aActivitySubtype,
                                   aTimestamp,
                                   aExtraSizeData,
                                   aExtraStringData)
          {
            if (aActivityType ==
                  activityDistributor.ACTIVITY_TYPE_HTTP_TRANSACTION ||
                aActivityType ==
                  activityDistributor.ACTIVITY_TYPE_SOCKET_TRANSPORT) {

              aChannel = aChannel.QueryInterface(Ci.nsIHttpChannel);

              let transCodes = this.httpTransactionCodes;

              if (aActivitySubtype ==
                  activityDistributor.ACTIVITY_SUBTYPE_REQUEST_HEADER ) {

                let httpActivity = {
                  url: aChannel.URI.spec,
                  method: aChannel.requestMethod,
                  channel: aChannel
                };

              }
            }
          },

          httpTransactionCodes: {
            0x5001: "REQUEST_HEADER",
            0x5002: "REQUEST_BODY_SENT",
            0x5003: "RESPONSE_START",
            0x5004: "RESPONSE_HEADER",
            0x5005: "RESPONSE_COMPLETE",
            0x5006: "TRANSACTION_CLOSE",

            0x804b0003: "STATUS_RESOLVING",
            0x804b0007: "STATUS_CONNECTING_TO",
            0x804b0004: "STATUS_CONNECTED_TO",
            0x804b0005: "STATUS_SENDING_TO",
            0x804b000a: "STATUS_WAITING_FOR",
            0x804b0006: "STATUS_RECEIVING_FROM"
          }
        };

        this.httpObserver = httpObserver;

        activityDistributor.addObserver(httpObserver);
      }

    };


var example = windows.open("http://www.example.com");

var widget = widgets.Widget({
  id: "close-window",
  label: "Close window",
  contentURL: "http://www.mozilla.org/favicon.ico",
  onClick: function() {
    example.close();
  }
});
};

The browser console tells me Reference Error: XPCOMUtils is not defined. Why's that?

UPDATE: I applied Wladimir Palant's fix but now there's another error:

An exception occurred.
Traceback (most recent call last):
 File ".../XPCOMUtils.jsm", line 231, in XPCU_defineLazyServiceGetter
 File ".../XPCOMUtils.jsm", line 208, in XPCU_defineLazyGetter
TypeError: aObject.__defineGetter__(aName, function()
 {delete aObject[name];return aObject[name] == aLambda.apply(aObject);})
 is not extensible

Upvotes: 1

Views: 3336

Answers (2)

Noitidart
Noitidart

Reputation: 37228

this comes up on bing top search when searching how to lazy load.

but i didnt learn how to use till i read this article. so for all those users coming across this topic read this it makes it very easy to understand. i now lazy load succesfully. http://mike.kaply.com/2011/02/08/adding-services-to-your-firefox-add-on/

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57651

When you use Cu.import() in an Add-on SDK extension the variables aren't being added automatically (bug 683217). The work-around is simple:

var {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm");

Concerning your update, I'm not sure what this is that you try to define a getter on but it is definitely not the global object - and Object.freeze() has been applied to it so that no new properties can be added (that's what is causing "not extensible" error). Instead it would make sense to define your lazy getter on the httpTrafficObserver object:

XPCOMUtils.defineLazyServiceGetter(httpTrafficObserver, "activityDistributor",
                                   "@mozilla.org/network/http-activity-distributor;1",
                                   "nsIHttpActivityDistributor");

Of course, in startHTTPObservation you would use self.activityDistributor instead of assuming that activityDistributor is a global variable.

Upvotes: 1

Related Questions