firefox插件包嗅探器

时间:2012-03-15 10:04:31

标签: firefox firefox-addon firebug xpcom firefox-addon-sdk

我想用我的插件嗅探数据包。我使用此Stack overflow question作为提示。我的完整代码是:

// 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();
  }
});
};

浏览器控制台告诉我Reference Error: XPCOMUtils is not defined。为什么?

更新 :我已应用Wladimir Palant修正,但现在又出现了另一个错误:

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

2 个答案:

答案 0 :(得分:1)

在附加SDK扩展中使用Cu.import()时,不会自动添加变量(bug 683217)。解决方法很简单:

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

关于您的更新,我不确定您尝试定义getter的this是什么,但它绝对不是全局对象 - 并且Object.freeze()已应用于它,因此没有可以添加新属性(这是导致“不可扩展”错误的原因)。相反,在httpTrafficObserver对象上定义懒惰的getter是有意义的:

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

当然,在startHTTPObservation中,您会使用self.activityDistributor而不是假设activityDistributor是全局变量。

答案 1 :(得分:0)

在搜索如何延迟加载时会出现顶部搜索。

但我没有学习如何使用,直到我读到这篇文章。因此,对于遇到此主题的所有用户而言,阅读本文使其易于理解。我现在懒得加载成功。 http://mike.kaply.com/2011/02/08/adding-services-to-your-firefox-add-on/