creating a file into local system in firefox plugin

You can access/read/write/files on your local system while creating Firefox plugin. Below is the code snippet for the same:

//savemode 1 to create a file, 2 is edit existing
var pathSystemFile = function(saveDirectory, fileName, selectedText, saveMode) {

	    var fileSeparator ="/";
		if (navigator.appVersion.indexOf("Win")!=-1) fileSeparator = "\\"

		var fullPathToFile = saveDirectory + fileSeparator + fileName;
		var file = Components.classes["@mozilla.org/file/local;1"]
				                      .createInstance(Components.interfaces.nsILocalFile);
		// Check file is being stored with a valid directory and name
		try{
		  file.initWithPath(fullPathToFile);
		  if (file.exists() == false)
		    file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420);

		  var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
				  							    .createInstance(Components.interfaces.nsIFileOutputStream);
		  var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
                                             .createInstance(Components.interfaces.nsIConverterOutputStream);
                            
          if (saveMode == 1){
            outputStream.init(file, 0x02 | 0x08 | 0x20, 420, 0);
		  }else if (saveMode == 2){
            outputStream.init(file, 0x02 | 0x10, 420, 0);
          }
	      
          converter.init(outputStream, "UTF-8", 0, 0);
          
             
          converter.writeString('\n' + selectedText);
          converter.close();
					
		  return true;
		} catch (e){
			return false;
		  
		}
	  };
	  
	   var FileManager = {
      // @returns string - Path to saved file
	  getPathToFile: function() {
			
		// check if a path to saved file has been set in user preferences
		var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
		                                     .getService(Components.interfaces.nsIPrefBranch);
//below line is variable which is being set in options.js
		var userPrefPathToFile = prefManager.getComplexValue("extensions.contentcreator.pathToFile",
						                                      Components.interfaces.nsISupportsString).data;
		
		if (userPrefPathToFile === ""){
				
		  // Save file in user's home directory (No preference specified)
		  var dirService = Components.classes["@mozilla.org/file/directory_service;1"]
		                                      .getService(Components.interfaces.nsIProperties);   
		  var homeDirFile = dirService.get("Home", Components.interfaces.nsIFile); // returns an nsIFile object
		  var pathToFile = homeDirFile.path;
		}else{
				
		  //Save file in user's prefered directory
		  pathToFile = userPrefPathToFile;
		}
		return pathToFile;
	  },
	   // @returns string - Name of file which will store the highlighted text
	  createFileName: function() {
		var currentTime = new Date();
		var date = currentTime.getDate() + "-" + (currentTime.getMonth() + 1) + "-" + currentTime.getFullYear();
		var time = currentTime.getHours() + "-" + currentTime.getMinutes() + "-" + currentTime.getSeconds();
		
	   return "CDLCapturedContent--" + date+"--"+time+".html";
		}
	  }
    

Please let me know if you want more information or have any queries on the same. Thanks.

Anoop Sharma

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>