在尝试下载页面时,为什么不对此代码进行警报测试?

时间:2009-11-28 18:03:07

标签: javascript firefox-addon xpcom

我必须下载myURLString(http://www.google.com/search?q=http://www.google.com/&btnG=Search+Directory&hl=en&cat=gwd%2FTop)。

function getcontents(myURLString) {
    var gChannel;
    var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
    var uri = ioService.newURI(myURLString, null, null);
    gChannel = ioService.newChannelFromURI(uri);
    var listener = new StreamListener(callbackFunc);
    gChannel.notificationCallbacks = listener;
    gChannel.asyncOpen(listener, null);
    function StreamListener(aCallbackFunc) {
      this.mCallbackFunc = aCallbackFunc;
    }
    StreamListener.prototype = {
      mData: "",
      onStartRequest: function (aRequest, aContext) {
        this.mData = "";
      },
      onDataAvailable: function (aRequest, aContext, aStream, aSourceOffset, aLength) {
        var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
        scriptableInputStream.init(aStream);
        this.mData += scriptableInputStream.read(aLength);
      },
      onStopRequest: function (aRequest, aContext, aStatus) {
        if (Components.isSuccessCode(aStatus)) {
          this.mCallbackFunc(this.mData);
          alert('test');
        } else {
          this.mCallbackFunc(null);
        }
        gChannel = null;
      },
      onChannelRedirect: function (aOldChannel, aNewChannel, aFlags) {
        gChannel = aNewChannel;
      },
      getInterface: function (aIID) {
        try {
          return this.QueryInterface(aIID);
        } catch (e) {
          throw Components.results.NS_NOINTERFACE;
        }
      },
      onProgress : function (aRequest, aContext, aProgress, aProgressMax) { },
      onStatus : function (aRequest, aContext, aStatus, aStatusArg) { },
      onRedirect : function (aOldChannel, aNewChannel) { },
      QueryInterface : function(aIID) {
        if (aIID.equals(Components.interfaces.nsISupports) ||
            aIID.equals(Components.interfaces.nsIInterfaceRequestor) ||
            aIID.equals(Components.interfaces.nsIChannelEventSink) || 
            aIID.equals(Components.interfaces.nsIProgressEventSink) ||
            aIID.equals(Components.interfaces.nsIHttpEventSink) ||
            aIID.equals(Components.interfaces.nsIStreamListener))
          return this;
        throw Components.results.NS_NOINTERFACE;
      }
    };
}

我在想这个.mData应该有页面的内容,但是我无法提醒它,所以我首先尝试提醒测试。有什么问题?

更新:我现在正在尝试......

function callbackFunc(pagecontents) {
    alert(pagecontents);
}

......但没有被召唤。为什么呢?

1 个答案:

答案 0 :(得分:0)

我怀疑您在致电StreamListener后定义new StreamListener(...)后收到错误。您是否设置了proper developer preferences