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
答案 0 :(得分:3)
SDK的file
模块非常有限。打开文件进行写入时,它将始终被截断(code)。此外,它在主线程上使用完全同步的I / O,这不是一件好事,因为它会在I / O期间阻塞整个UI。
您应该通过chrome
模块使用其他机制。请参阅OS.File和/或MDN File I/O snippets。