开发者

While trying to download a page, why doesn't this code alert test?

I have to download 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) {
 开发者_JAVA技巧       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;
      }
    };
}

I'm thinking this.mData should have the page's contents, but I can't alert it, so I'm trying first to alert test. What is wrong?

UPDATE: I'm trying now...

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

...but it isn't called. Why?


I suspect you are getting an error since StreamListener is defined after you call new StreamListener(...). Have you set the proper developer preferences?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜