user621819
user621819

Reputation:

How to Append to a file in a Firefox add-on?

var tabs = require("sdk/tabs");
var iofile = require("sdk/io/file");
var widgets = require("sdk/widget");
var selection = require("sdk/selection");


function console_log(text) {
    console.log(selection.text);
}

function print(text) {
    console.log(text);
}

function dir_object(object_to_parse) {
    var name = '';
    for (name in object_to_parse) {
        print(name);
    }
}

function write_text(filename, text) {
    var fh = iofile.open(filename, 'w');
    var content = fh.read();

    dir_object(fh);

    selected_text = text + "\n";
    fh.write(selected_text);
    fh.flush();
    fh.close()
}

function select_text_handler() { 
    write_text('/tmp/foo', selection.text);
}

var widget = widgets.Widget({
    id: "scribus-link",
    label: "Scribus website",
    contentURL: "http://www.mozilla.org/favicon.ico",
    onClick: function() {
    }
});



selection.on('select', function () { select_text_handler(); });

'open' the file in 'w' and that truncates my existing file! How do i open in 'append' mode and then 'seek'?? https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/io/file.htm

Upvotes: 3

Views: 1284

Answers (1)

nmaier
nmaier

Reputation: 33162

The file module of the SDK is pretty limited. When opening a file for writing it will always be truncated (code). Also, it is uses entirely synchronous I/O on the main thread, which isn't really a good thing to do, as it will block the entire UI during the I/O.

You should probably use another mechanism via the chrome module. See OS.File and/or the MDN File I/O snippets.

Upvotes: 3

Related Questions