RPGDXThe center of Indie-RPG gaming
Not logged in. [log in] [register]
 
 
Post new topic Reply to topic  
View previous topic - View next topic  
Author Message
tcaudilllg
Dragonmaster


Joined: 20 Jun 2002
Posts: 1731
Location: Cedar Bluff, VA

PostPosted: Mon Aug 09, 2010 4:16 pm    Post subject: How to read/write files in Firefox. [quote]

I don't have to mention how awesome the topic of this post is, so I won't.

The first step is to get the necessary permissions from Firefox. You do this by interfacing with XPCOM, Mozilla's answer to Microsoft's COM. ("Component Object Model")

Code:

this.netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');


Sometimes you have to change a flag in about:config first, depending on whether or not you've previously locked down your Firefox install. Oh, and deactivate NoScript while you're at it.


Firefox read/write is definitely NSFW -- you could be fired if a security compromise caused by read/write permissions is found to have originated on your terminal.

Without further ado, the code.

Code:

<html>

<head>

<script>

// IO.JS - file input/output for Firefox
// ...And every bit of it ripped from MDC's examples. Seriously though, I don't think they could have made it any more complicated. :P

this.netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');


function writeFile (fileData, filePath)
{

   // "fileData" is a string containing the data to write. "filePath" is a string containing the complete path for the file.
   // This function writes a string to a file, erasing its previous content.

   var file = Components.classes["@mozilla.org/file/local;1"] 
   .createInstance(Components.interfaces.nsILocalFile);
 
   file.initWithPath( filePath );    
   file.createUnique( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 600);
           
   var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"]
   .createInstance(Components.interfaces.nsIFileOutputStream);
   
   stream.init(file, 0x04 | 0x08 | 0x20, 0600, 0); // readwrite, create, truncate
           
   stream.write(fileData, fileData.length);

   if (stream instanceof Components.interfaces.nsISafeOutputStream) {
      stream.finish();
   } else {
      stream.close();
   }

}


function readFile (filePath)
{

   // "filePath" is a string containing the complete path of the file to be read.
   // This function returns a string containing the data of the read file.

   var file = filePath;
      
   var incoming = Components.classes["@mozilla.org/network/file-input-stream;1"]
   .createInstance(Components.interfaces.nsIFileInputStream);
   incoming.init(file, -1, -1, false);

   var cache = Components.classes["@mozilla.org/binaryinputstream;1"]
   .createInstance(Components.interfaces.nsIBinaryInputStream);
   cache.setInputStream(incoming);

   return cache.readBytes(cache.available());

}


function saveFile (fileData, filter)
{

   // "fileData" is a string containing the data to write. "filter" is the file's default extension.
   // This function allows the user to save a file with the save-as dialogue box.
   
   // This first segment allows that a file be picked with the open/save dialogue window.
   const nsIFilePicker = Components.interfaces.nsIFilePicker;

   var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
   filePicker.init(window, "", nsIFilePicker.modeSave);
   if (filter == "")
   {
      filePicker.appendFilters(nsIFilePicker.filterAll);
   }
   else
   {
      filePicker.appendFilter("*." + filter, "*." + filter);
   }

   var selection = filePicker.show();
   if (selection == nsIFilePicker.returnOK || selection == nsIFilePicker.returnReplace)
   {
   
      // This is the actual code to write the file.
   
      var file = Components.classes["@mozilla.org/file/local;1"] 
      .createInstance(Components.interfaces.nsILocalFile); 
 
      file.initWithPath( filePicker.file.path );    
      file.createUnique( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 600);
           
      var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"]
      .createInstance(Components.interfaces.nsIFileOutputStream);
   
      stream.init(file, 0x04 | 0x08 | 0x20, 0600, 0); // readwrite, create, truncate
           
      stream.write(fileData, fileData.length);

      if (stream instanceof Components.interfaces.nsISafeOutputStream) {
         stream.finish();
      } else {
         stream.close();
      }
   }

}


function openFile (filter)
{

   // "filter" is a string specifying which files extensions to show in the open dialogue box.
   // This function allows the user to open a file with the open dialogue box. It returns a string with the file's contents.
   
   // This first segment allows that a file be picked with the open/save dialogue window.
   this.netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

   const nsIFilePicker = Components.interfaces.nsIFilePicker;

   var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
   filePicker.init(window, "", nsIFilePicker.modeOpen);
   if (filter == "")
   {
      filePicker.appendFilters(nsIFilePicker.filterAll);
   }
   else
   {
      filePicker.appendFilter("*." + filter, "*." + filter);
   }

   var selection = filePicker.show();
   if (selection == nsIFilePicker.returnOK || selection == nsIFilePicker.returnReplace)
   {

      // This is the actual code to read the file.
   
      var file = filePicker.file;
      
      var incoming = Components.classes["@mozilla.org/network/file-input-stream;1"]
      .createInstance(Components.interfaces.nsIFileInputStream);
      incoming.init(file, -1, -1, false);

      var cache = Components.classes["@mozilla.org/binaryinputstream;1"]
      .createInstance(Components.interfaces.nsIBinaryInputStream);
      cache.setInputStream(incoming);

      return cache.readBytes(cache.available());
   }
   
}

</script>

</head>

<textarea></textarea>

<input>
<input>

</html>


Users may be asked to affirm whether they want to allow your script access to their file system. Obviously, you want them to click "yes".
Back to top  
Post new topic Reply to topic Page 1 of 1 All times are GMT
 



Display posts from previous:   
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum