当尝试下载页面时,为什么此代码没有进行警报测试?

发布于 2024-08-13 00:30:57 字数 2845 浏览 6 评论 0原文

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

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;
      }
    };
}

我认为 this.mData 应该包含页面的内容,但我无法提醒它,所以我首先尝试提醒测试。怎么了?

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

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

但它没有被调用。为什么?

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) {
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独闯女儿国 2024-08-20 00:30:57

我怀疑您收到错误,因为 StreamListener 是在调用 new StreamListener(...) 之后定义的。您是否设置了正确的开发人员首选项

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文