6

我目前正在研究 ffsniff 扩展代码。因为我必须将包含密码信息的数据保存到本地系统中的文件中。我已经编写了代码,但它甚至没有在我的本地系统中创建文件。(在 Mozilla Firefox 中工作)

这是我的代码,请帮助我。

//// here data variable contains all the information
var fso = new ActiveXObject("Scripting.FileSystemObject");
varFileObject = fso.OpenTextFile("C:\\logs.txt", 2, true,0);
varFileObject.write(data);
varFileObject.close();

在此之后我尝试了不同的代码:

Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");

var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("Desk", Components.interfaces.nsIFile);
 file.append("logs.txt");


var ostream = FileUtils.openSafeFileOutputStream(file)

var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
            createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
var istream = converter.convertToInputStream(data);

  }
});

但他们都没有工作..

4

4 回答 4

3

这是一个工作片段,它在必要时创建目标目录并写入(覆盖)到文件(在本例中为d:\temp-directory\temp-file.txt):

var {Cc,Ci,Cu}=require("chrome");  //for jetpack sdk.
Cu.import("resource://gre/modules/NetUtil.jsm");  
Cu.import("resource://gre/modules/FileUtils.jsm"); 
var localFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
var data="test file content";

  //localFile.initWithPath("D:\\temp-directory\\temp-file.txt");  //full path is okay if directory exists

localFile.initWithPath("D:\\temp-directory\\");                 //otherwise specifiy directory, create it if necessary, and append leaf.
if(!localFile.exists()){
    localFile.create(localFile.DIRECTORY_TYPE,FileUtils.PERMS_DIRECTORY);
}
localFile.append("temp-file.txt");

  //localFile.createUnique(localFile.NORMAL_FILE_TYPE,FileUtils.PERMS_FILE);  //optional: create a new unique file.

asyncSave(localFile,data,onDone);

function asyncSave(file,data,callbackDone){    
      // file is nsIFile, data is a string, optional: callbackDone(path,leafName,statusCode)    
      // default flags:  FileUtils.openSafeFileOutputStream(file, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE);  
    var ostream = FileUtils.openSafeFileOutputStream(file); 
    var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);  
    converter.charset = "UTF-8";  
    var istream = converter.convertToInputStream(data);  

      // optional: callbackSaved(status).  
    NetUtil.asyncCopy(istream, ostream, callbackSaved); 
    function callbackSaved (status) {     
        if(callbackDone){
            if(status===0)callbackDone( file.path, file.leafName, status);  //sucess.
            else callbackDone( null, null, status); //failure.
        }; 
    }
}
function onDone(path,leafName,statusCode){
    console.log([statusCode===0?"OK":"error",path,leafName].join("\n"));
}

更多信息:

于 2013-06-04T19:16:35.993 回答
1

一个简单的例子,说明如何使用 Firefox 扩展从 Windows 中的文件系统读取/写入文件:

// Write File to filesystem
Components.utils.import("resource://gre/modules/osfile.jsm");    // load the OS module
var encoder = new TextEncoder();    // This encoder can be reused for several writes
var array = encoder.encode("just some text");   // Convert the text to an array
var promise = OS.File.writeAtomic("C:\\foo.txt", array,{tmpPath: "foo.txt.tmp"});   // Write the array atomically to "file.txt", using as temporary

alert("URL HOST has been saved");

//Read File from filesystem
var decoder = new TextDecoder();        // This decoder can be reused for several reads
var promise = OS.File.read("C:\\foo.txt"); // Read the complete file as an array
promise = promise.then(
    function onSuccess(array) {
        alert(decoder.decode(array));        // Convert this array to a text
    }
);
于 2014-05-07T20:27:41.840 回答
1

此解决方案用于在 ubuntu 中制作文件,希望对其他人有所帮助:

var file = Components.classes["@mozilla.org/file/directory_service;1"].  
               getService(Components.interfaces.nsIProperties).  
               get("ProfD", Components.interfaces.nsIFile);  
file.append("trick_new");  
if( !file.exists() || !file.isDirectory() ) {  // if it doesn't exist, create  
    file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);  
}  
this.log_file = file.path + "/newlog.html";
于 2014-01-14T05:14:59.520 回答
0

您还可以使用 text-stream 写入本地文件。

function writeTextToFile(text, filename) {
  var fileIO = require("sdk/io/file");
  var TextWriter = fileIO.open(filename, "w");
  if (!TextWriter.closed) {
    TextWriter.write(text);
    TextWriter.close();
  }
}
于 2016-01-21T06:49:57.413 回答