Firefox插件SDK中的HTML5文件API [英] HTML5 File API in Firefox Addon SDK

查看:168
本文介绍了Firefox插件SDK中的HTML5文件API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过内容脚本在Fire Fox插件sdk中访问Html5文件api?

这是存储用户添加的单词及其含义所必需的.数据可能会很大,因此无法选择本地存储.

 window.requestFileSystem3 = window.requestFileSystem || window.webkitRequestFileSystem;

给我错误 TypeError:window.requestFileSystem3不是函数.

我之所以问这个问题,是因为我正在从Google Chrome扩展程序中移植此代码,从而可以访问内容脚本中的文件api.

其他问题

1)如果不允许HTML5 File API,那么我应该使用解决方案

Firefox尚不支持通过File API编写文件,即使将其添加,它也可能仅可用于网页,而不能用于扩展名.换句话说:是的,如果您绝对需要写入文件,则应使用低级API.您想将数据存储在用户配置文件目录中(没有扩展目录,您的扩展通常作为单个打包文件安装).这样的事情应该可以写文件:

 var file = require("sdk/io/file");
var profilePath = require("sdk/system").pathFor("ProfD");
var filePath = file.join(profilePath, "foo.txt");
var writer = file.open(filePath, "w");
writer.writeAsync("foo!", function(error)
{
  if (error)
    console.log("Error: " + error);
  else
    console.log("Success!");
});
 

供参考: sdk/io/file

您可以使用TextReader.read()file.read()来读取文件.不幸的是,附加SDK似乎不支持异步文件读取,因此读取将阻止Firefox UI.唯一的选择是导入 NetUtil var {components, Cu} = require("chrome"); var {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", null); var {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null); NetUtil.asyncFetch(new FileUtils.File(filePath), function(stream, result) { if (components.isSuccessCode(result)) { var data = NetUtil.readInputStreamToString(stream, stream.available()); console.log("Success: " + data); } else console.log("Error: " + result); });

Is there a way to access Html5 file api in Fire Fox addon sdk in the content script?

This is needed in order to store user added words and their meanings. The data can grow large and so local storage isn't an option.

 window.requestFileSystem3 = window.requestFileSystem || window.webkitRequestFileSystem;

gives me the error TypeError: window.requestFileSystem3 is not a function.

I am asking this because i am porting this code from a Google Chrome Extension which allows accessing the file api in a content script.

Additional Questions

1) If HTML5 File API is not allowed then should i use file module?

2) Does the file module allow access to any file on the file system as opposed to the Html5 file api which only access to a sandboxed access to file system?

3) Assuming i have to use file module what would be the best location to store my files ( like the user profile directory or extension directory ) and how would i get this path in code.

I apologize for so many sub questions inside this questions. Google wasn't very helpful regarding this topic.

Any sample code would be very helpful.

解决方案

Firefox doesn't support writing files via File API yet and even when this will be added it will probably be accessible to web pages only and not extensions. In other words: yes, if you absolutely need to write to files then you should use low-level APIs. You want to store your data in the user profile directory (there is no extension directory, your extension is usually installed as a single packed file). Something like this should work to write a file:

var file = require("sdk/io/file");
var profilePath = require("sdk/system").pathFor("ProfD");
var filePath = file.join(profilePath, "foo.txt");
var writer = file.open(filePath, "w");
writer.writeAsync("foo!", function(error)
{
  if (error)
    console.log("Error: " + error);
  else
    console.log("Success!");
});

For reference: sdk/io/file, sdk/system

You could use TextReader.read() or file.read() to read the file. Unfortunately, Add-on SDK doesn't seem to support asynchronous file reading so the read will block the Firefox UI. The only alternative would be importing NetUtil and FileUtils via chrome authority, something like this:

var {components, Cu} = require("chrome");
var {NetUtil} = Cu.import("resource://gre/modules/NetUtil.jsm", null);
var {FileUtils} = Cu.import("resource://gre/modules/FileUtils.jsm", null);
NetUtil.asyncFetch(new FileUtils.File(filePath), function(stream, result)
{
  if (components.isSuccessCode(result))
  {
    var data = NetUtil.readInputStreamToString(stream, stream.available());
    console.log("Success: " + data);
  }
  else
    console.log("Error: " + result);
});

这篇关于Firefox插件SDK中的HTML5文件API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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