【问题标题】:How to Append to a file in a Firefox add-on?如何附加到 Firefox 插件中的文件?
【发布时间】:2013-10-25 07:29:21
【问题描述】:
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(); });

'打开''w'中的文件,这会截断我现有的文件!我如何在“附加”模式下打开然后“搜索”? https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/io/file.htm

【问题讨论】:

  • "w+"模式吗?
  • 不在文档中,也不是在我尝试时.. 它接受 'w+' 但在关闭后,当写入下一行时,它会从文件开头写入

标签: javascript file-io firefox-addon firefox-addon-sdk


【解决方案1】:

SDK 的file 模块非常有限。打开文件进行写入时,它总是会被截断(code)。此外,它在主线程上使用完全同步的 I/O,这并不是一件好事,因为它会在 I/O 期间阻塞整个 UI。

您可能应该通过chrome 模块使用另一种机制。请参阅OS.File 和/或MDN File I/O snippets

【讨论】:

  • 我在哪里可以找到关于“chrome”的文档 - 每次我用谷歌搜索时,我都会得到“Google Chrome”而不是 Firefox Chrome。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2017-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多