Skip to content Skip to sidebar Skip to footer

Html5 File Api In Firefox Addon Sdk

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 a

Solution 1:

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);
  elseconsole.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(newFileUtils.File(filePath), function(stream, result)
{
  if (components.isSuccessCode(result))
  {
    var data = NetUtil.readInputStreamToString(stream, stream.available());
    console.log("Success: " + data);
  }
  elseconsole.log("Error: " + result);
});

Post a Comment for "Html5 File Api In Firefox Addon Sdk"