【问题标题】:Receiving binary data using request module in Firefox Add-on SDK使用 Firefox Add-on SDK 中的请求模块接收二进制数据
【发布时间】:2012-03-25 16:04:45
【问题描述】:

我正在使用插件生成器,我需要接收二进制数据(图像)。我想使用request 模块来执行此操作,但您可以从文档中看到:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/addon-kit/docs/request.html

只有textjson 属性,raw 不存在。

我应该如何在插件脚本中接收二进制数据?

【问题讨论】:

    标签: ajax firefox firefox-addon firefox-addon-sdk


    【解决方案1】:

    您不能使用request 模块来执行此操作,您必须通过chrome authority 使用常规XMLHttpRequest。像这样的东西应该可以工作:

    var {Cc, Ci} = require("chrome");
    var request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
                    .createInstance(Ci.nsIJSXMLHttpRequest);
    request.open("GET", "...");
    request.onload = function()
    {
      onUnload.unload();
    
      var arrayBuffer = request.response;
      if (arrayBuffer)
      {
        var byteArray = new Uint8Array(arrayBuffer);
        ...
      }
    };
    request.onerror = function()
    {
      onUnload.unload();
    }
    request.send(null);
    
    var onUnload = {
      unload: function()
      {
        // Make sure to abort the request if the extension is disabled
        try
        {
          request.abort();
        }
        catch (e) {}
      }
    };
    require("unload").ensure(onUnload);
    

    如果您的扩展程序突然被禁用,确保请求被中止的机制相当尴尬,这是存在request 模块而不是简单地给您XMLHttpRequest 的主要原因。请注意,在请求完成后调用onUnload.unload() 很重要,否则附加组件 SDK 会将其保留在卸载时调用的方法列表中(内存泄漏)。见documentation of unload module

    【讨论】:

    • 如果你确实在你的插件中使用了这个,如果你把这个代码放到一个独立的模块中,它将使审查过程(和测试!)更容易。
    • @canuckistani 这似乎是个好主意,但我实际上如何将代码放入离散模块中?
    • SDK 使用 commonJS 格式,您可以在此处的文档中找到解释:addons.mozilla.org/en-US/developers/docs/sdk/1.5/dev-guide/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多