Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firefox SDK错误“;图像包含错误且无法显示";_Javascript_Image_Firefox_Xmlhttprequest_Add On - Fatal编程技术网

Javascript Firefox SDK错误“;图像包含错误且无法显示";

Javascript Firefox SDK错误“;图像包含错误且无法显示";,javascript,image,firefox,xmlhttprequest,add-on,Javascript,Image,Firefox,Xmlhttprequest,Add On,我正在为Mozilla Firefox开发一个扩展,在该扩展中,我使用自己的侦听器覆盖本机侦听器,并监视所有HTTP请求,如本文所示: 我监视驻留在特定域下的那些请求,并使用从自己的XMLHTTPRequest接收的响应体更改它们相应的响应体。对于文本文件,一切正常 然而,我在下载图像时遇到了一个问题。出于某种原因,当我将传入数据写入流,然后尝试打开图像时,我得到一个错误,即图像包含错误,无法显示 我可能做错了什么 更新:为了澄清我的方法,我提供了一些代码 var xmlReque

我正在为Mozilla Firefox开发一个扩展,在该扩展中,我使用自己的侦听器覆盖本机侦听器,并监视所有HTTP请求,如本文所示:

我监视驻留在特定域下的那些请求,并使用从自己的XMLHTTPRequest接收的响应体更改它们相应的响应体。对于文本文件,一切正常

然而,我在下载图像时遇到了一个问题。出于某种原因,当我将传入数据写入流,然后尝试打开图像时,我得到一个错误,即图像包含错误,无法显示

我可能做错了什么

更新:为了澄清我的方法,我提供了一些代码



    var xmlRequest = Cc['@mozilla.org/xmlextras/xmlhttprequest;1'].createInstance(Ci.nsIXMLHttpRequest);
    ...
    xmlRequest.open('GET', xmlRequestURL, false);
    xmlRequest.send(null);

    function TracingListener() {}

    TracingListener.prototype = {
      originalListener: null,

      onStartRequest: function (request, context) {
        httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
        requestURL = httpChannel.URI.spec;

        try {
          this.originalListener.onStartRequest(request, context);
        } 
        catch (ex) {
          request.cancel(ex);
        }
      },

      onDataAvailable: function (request, context, inputStream, offset, count) {
        httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
        requestURL = httpChannel.URI.spec;

        //Read the contents from the stream, but ignore them.
        var binaryInputStream = CCIN('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream');
        binaryInputStream.setInputStream(inputStream);

        var binaryOutputStream = CCIN('@mozilla.org/binaryoutputstream;1', 'nsIBinaryOutputStream');
        var data = binaryInputStream.readBytes(count);

        //Delay the call to the original listener.
      },

      onStopRequest: function (request, context, statusCode) {
        httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
        requestURL = httpChannel.URI.spec;

        //Check if the response is successful.
        if(xmlRequest.status == 200) {
          try {
            var responseLen = xmlRequest.getResponseHeader("Content-Length");
            var response = xmlRequest.response;

            var storageStream = CCIN('@mozilla.org/storagestream;1', 'nsIStorageStream');
            storageStream.init(8192, responseLen, null);

            var binaryOutputStream = CCIN('@mozilla.org/binaryoutputstream;1', 'nsIBinaryOutputStream');
            binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));

            binaryOutputStream.writeBytes(response, responseLen);

            //Make the call to the original listener.
            this.originalListener.onDataAvailable(request, context, storageStream.newInputStream(0), 0, responseLen);
          }
          catch (e) {
            dumpError(e);
          }

          //Pass it to the original listener
          this.originalListener.onStopRequest(request, context, statusCode);
        }
        else {
          console.log('[INFO] onStopRequest not processed, status is ' + xmlRequest.status + ', for URL: ' + requestURL);
        }
      }
    }

    var observer = {
      httpRequestObserver: {
        observe: function (request, aTopic, aData) {
          httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
          requestURL = httpChannel.URI.spec;

          if(mustBeMonitored(requestURL)) {
            console.log('[INFO] Observing URL: ' + requestURL);

            if (aTopic == 'http-on-modify-request') {
              console.log('[INFO] ' + aTopic + ' for URL: ' + requestURL);

              var newListener = new TracingListener();
              request.QueryInterface(Ci.nsITraceableChannel);
              newListener.originalListener = request.setNewListener(newListener);
            }
          }
        },

        register: function () {
          observerService.addObserver(observer.httpRequestObserver, 'http-on-modify-request', false);
        },
        unregister: function () {
          observerService.removeObserver(observer.httpRequestObserver, 'http-on-modify-request');
        },

        QueryInterface: function (aIID) {
          /*if (typeof Cc == "undefined") {
             var Cc = components.classes;
          }
          if (typeof Ci == "undefined") {
             var Ci = components.interfaces;
          }*/

          if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports))
             return this;
          throw components.results.NS_NOINTERFACE;
        }
      }
    };


最后,我发现了这个问题。对于XMLHttpRequest,我必须按如下方式指定其响应类型:

xmlRequest.responseType = 'arraybuffer';
然后,响应被存储在JavaScript
ArrayBuffer
中,我必须将其转换为
Uint8Array
,然后将其存储到流中


此解决方案适用于二进制文件和文本文件。

如果没有代码,就不可能正确地帮助您。我用一些示例代码更新了我的帖子。