views:

77

answers:

1

I have to download myURLString (http://www.google.com/search?q=http%3A//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;
   }
 };
}

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?

A: 

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

sdwilsh
I use Firefox with Extension Developer's Extension, then click Tools -> Extension Developer -> Enable Debugging Preferences.
Delirium tremens