使用Firefox插件SDK中的请求模块接收二进制数据 [英] Receiving binary data using request module in Firefox Add-on SDK

查看:114
本文介绍了使用Firefox插件SDK中的请求模块接收二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用附加组件生成器,我需要接收二进制数据(图像)。我想用请求模块来做到这一点,但你可以从文档中看到:

https://addons.mozilla。 org / en-US / developers / docs / sdk / latest / packages / addon-kit / docs / request.html b
$ b只有 text json 属性和 raw 不存在。



如何在附加脚本中接收二进制数据?

解决方案

使用请求模块来执行此操作,您将不得不通过 XMLHttpRequest /addons.mozilla.org/en-US/developers/docs/sdk/1.5/dev-guide/module-development/chrome.htmlrel =nofollow> chrome权限。这样的东西应该工作:


$ b

  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()
{
//如果扩展被禁用,请确保中止请求
try
{
request.abort();
}
catch(e){}
}
};
require(unload)。ensure(onUnload);

如果您的扩展突然被禁用,确保请求被中止的机制相当尴尬,主要原因是请求模块存在,而不是简单地给你 XMLHttpRequest 。请注意,一旦请求完成,调用 onUnload.unload()是非常重要的,否则附加SDK将保留在要在unload上调用的方法列表中内存泄漏)。请参阅 unload 模块


I am using the Add-on builder and I need to receive binary data (image). I would like to do this using the request module but as you can see from the documentation:

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

There are only text and json properties and raw is absent.

How should I receive binary data in the add-on script?

解决方案

You cannot do this using the request module, you will have to use the regular XMLHttpRequest via chrome authority. Something like this should work:

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

The mechanism to ensure that the request is aborted if your extension is suddenly disabled is rather awkward, that's the main reason the request module exists rather than simply giving you XMLHttpRequest. Note that it is important to call onUnload.unload() once the request finishes, otherwise the Add-on SDK will keep it in the list of methods to be called on unload (a memory leak). See documentation of unload module.

这篇关于使用Firefox插件SDK中的请求模块接收二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆