Reading contents of files inside the XPI.

Add-ons for Pale Moon and other applications
General discussion, compatibility, contributed extensions, themes, plugins, and more.

Moderators: FranklinDM, Lootyhoof

BobbyBonsaimind

Reading contents of files inside the XPI.

Unread post by BobbyBonsaimind » 2017-12-08, 18:03

I'm currently in the process of recreating my extensions for Palemoon but have hit a wall so far. What I'd like to do is to provide the user with the ability to export all settings into an easily readable JSON file which can also be imported. Additionally I want to provide different defaults which can be loaded at will (and are embedded inside the XPI).

The exporting/importing works by utilizing the FileUtils methods, but for being able to provide different defaults I want to embed the exported JSON files inside the XPI (which means that I can easily change these default settings, I just need to import, tweak, and export them again). I'm unable to find a way to read the content of an embedded file which is accessible through a chrome URI.

Does somebody know how I can do that?

JustOff

Re: Reading contents of files inside the XPI.

Unread post by JustOff » 2017-12-08, 20:36

You can try something like this:

Code: Select all

var file = Services.io.newURI("chrome://my-add-on/content/myfile.json", null, null);

NetUtil.asyncFetch(file, function(inputStream) {
  var data = NetUtil.readInputStreamToString(inputStream, inputStream.available());
});

BobbyBonsaimind

Re: Reading contents of files inside the XPI.

Unread post by BobbyBonsaimind » 2017-12-08, 22:25

That works, thank you very much.

I remember seeing both of these functions, but for some reason never managed to connect the dots that I could use them to read the file.

Locked