Managing files in your FF extension Directory

Below are the code snippets that I used to manipulate files in FF extension using XPCom. Hope these will be helpful to you. As I worked on Binary data so these snippets are capable of handling binary data as well as simple text data.

Getting path of your extension directory

var getWorkingDir= Components.classes[";1"]
.get("UChrm", Components.interfaces.nsIFile);

var currPath = getWorkingDir.path.substr(0, getWorkingDir.path.lastIndexOf('\\'));
currPath += '\\extensions\\extensionname\\chrome\\content\\';

Create a file and copy data to it

var inputFile = Components.classes[";1"]
inputFile.initWithPath( currPath +  'files\\' + randomNum + 'input.bin');

var stream = Components.classes[";1"]
stream.init(inputFile, 0x04 | 0x08 | 0x20, 0600, 0); // write, create, truncate

stream.write(someData, someData.length);

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

Run a Executable file

// create an nsILocalFile for the executable
var file = Components.classes[";1"]

file.initWithPath(currPath + "program.exe");
// create an nsIProcess
var process = Components.classes[";1"]


var args = ["-arg1", "arg2"];, args, args.length);

Copy the data to file

var outputFile = Components.classes[";1"]

outputFile.initWithPath( currPath +  'files\\' + randomNum + 'output.bin');

var istream = Components.classes[";1"]
istream.init(outputFile, 0x04 | 0x08, 0664, 0);

var bstream = Components.classes[";1"].

var bytes = bstream.readBytes(bstream.available());

I am a web developer working @ Gaditek

Tagged with:
Posted in Browser Extension Development, Computer And Internet, Programming & Development

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About me
A geek trying to make world a better place to live.

I work on LAMP Stack, SRE and resilience engineering. I also worked on Python, GAE and Netsuite Customization. Currently working @ Cloudways as Software Architect

Profile Links Linkedin | Github | Stackoverflow
Subscribe to this blog
%d bloggers like this: