如何添加到Firefox插件中的文件? [英] 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);
}
函数dir_object(object_to_parse){
var name ='';
for(name in object_to_parse){
print(name);
函数write_text(filename,text){
var fh = iofile.open(filename,'w');
var content = fh.read();
dir_object(fh);
selected_text = text +\\\
;
fh.write(selected_text);
fh.flush();
fh.close()
}
函数select_text_handler(){
write_text('/ tmp / foo',selection.text);
var widget = widgets.Widget({
id:scribus-link,
label:Scribus网站,
contentURL: http://www.mozilla.org/favicon.ico,
onClick:function(){
}
});
$ b selection.on('select',function(){select_text_handler();});
'打开'w'中的文件并截断我现有的文件!我如何打开追加模式,然后寻求? https://addons.mozilla .org / en-US / developers / docs / sdk / latest / modules / sdk / io / file.htm
SDK的文件
模块非常有限。当打开一个文件写入它总是会被截断(代码)。而且,它在主线程上使用完全同步的I / O,这不是一件好事,因为它会在I / O期间阻塞整个UI。
您应该使用另一种机制,通过 chrome
模块。请参阅 OS.File 和/或 MDN File I / O snippets 。
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
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.
这篇关于如何添加到Firefox插件中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!