tags:

views:

420

answers:

3

Under Firefox, I want to do something like this :

I have a .htm file, that has a button on it. This button, when I click it, the action will write a text inside a local .txt file. By the way, my .htm file is run locally too.

I have tried multiple times using this code, but still cant make my .htm file write to my textfile:

function save() {
try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
} catch (e) {
    alert("Permission to save file was denied.");
}
var file = Components.classes["@mozilla.org/file/local;1"]
    .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( savefile );
if ( file.exists() == false ) {
    alert( "Creating file... " );
    file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
}
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    .createInstance( Components.interfaces.nsIFileOutputStream );

outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
var output = 'test test test test';
var result = outputStream.write( output, output.length );
outputStream.close();

}

This part is for the button:

<input type="button" value="write to file2" onClick="save();">
A: 

What kind of content are you trying to write out? Would a cookie be a better option for your program?

Jason Too Cool Webs
i just to write something like this:test_1 = 0 or test_1 = 1Somehow, it's just a sort of a flag inside a textfile on Linux.About the cookie,I dont think about it yet. But it is ok as long as I can make it work as I want.
karikari
Ok is this a requirement you've been given. Seems to me if this is all you want to do just set a js cookie. Wouldn't have to go through the hassle of browser permissions. http://www.w3schools.com/js/js_cookies.asp
Jason Too Cool Webs
where does firefox stores its cookies on Linux?
karikari
A: 

Javascript is not allowed to access hard drive but you can use ActiveXObject to create or write to text file using Javascript.

function writeToDisk(writeString) {

var fso = new ActiveXObject("Scripting.FileSystemObject"); var a = fso.CreateTextFile(theFile, true); a.WriteLine(writeString); a.Close(); }

Happy coding

Ravia
Hi Ravia, I've tried it before. I couldnot work. By the way, can I make activex work on Firefox on Linux? Because I'm doing this for Firefox on Linux platform.
karikari
Well Karkari I don't have much idea about linux but this link gives some details for Javascript on Linux using EJScript http://stackoverflow.com/questions/303275/javascript-spidermonkey-how-does-one-run-a-linux-command-from-js-shell/303323
Ravia
A: 

Even if you run it locally, I doubt that Firefox will let you access the filesystem. However, if you create an extension it would be able to access the filesystem.

stealthdragon
I see. May be this is one reason why I still cant make it work. If I need to do Firefox extension, can an extension work just only for a button on a particular page? I dont want it to target other page too. I'm doing all this on a local machine, and the button page and the textfile also is on a local machine. No webserver involve here. Just Firefox and a .htm file that has a button that can write to a textfile. Enlighten me : )
karikari
Yeah, it would be easy to create an extension that only applied to a certain page. Firefox provides access to the url of the current page and you could just compare it to the url of your local file. If they match, you could run whatever code you want. Creating the extension itself can be slightly tricky unless it has drastically changed since I tried a few years ago. The tricky part was figuring out how to properly organize and package up the files so that Firefox was happy.
stealthdragon
I'm currently testing a sample extension. But now my problem is, how to make the button on the page to interact with the extension?
karikari
I'm not sure. It wouldn't be hard to find that information on the 'net or you could open a new question.
stealthdragon