Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create .txt in local file system using Firefox extension

I am currently working on ffsniff extension code. In that I have to save data containing password information into a file in my local system. I have written my code but it is not even creating the file in my local system. (working in mozilla firefox)

Here is my code please help me out.

//// 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();

after this i tried different code:

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);

  }
});

but none of them is working..

like image 433
Shruti Sharma Avatar asked Jun 04 '13 04:06

Shruti Sharma


1 Answers

Here's a working snippet that creates the destination directory if necessary and writes (overwrites) to file (in this case 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"));
}

More information:

  • https://developer.mozilla.org/en-US/docs/Code_snippets/File_I_O
  • https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FileUtils.jsm
    • https://developer.mozilla.org/en-US/docs/PR_Open
  • https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/NetUtil.jsm
  • https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIFile
  • https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsILocalFile
like image 189
jongo45 Avatar answered Oct 02 '22 01:10

jongo45