0

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();">
asked Jan 5, 2010 at 8:38
2
  • Are you getting any error messages? Commented Jan 5, 2010 at 8:39
  • No. When I click the button, and after that I open my local file, there's nothing inside the file. my localfile is at /home/user/Desktop/test.txt What am I doing wrong here... Commented Jan 5, 2010 at 9:11

3 Answers 3

1

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

JMax
26.7k12 gold badges74 silver badges89 bronze badges
answered Jan 5, 2010 at 16:53
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Well Karkari I don't have much idea about linux but this link gives some details for Javascript on Linux using EJScript stackoverflow.com/questions/303275/…
0

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.

answered Jan 5, 2010 at 16:58

4 Comments

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 : )
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.
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?
I'm not sure. It wouldn't be hard to find that information on the 'net or you could open a new question.
0

Hi try this example still if you have the problem:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window
 id="mywindow"
 title="Find Files"
 orient="horizontal"
 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<grid flex="1">
 <columns>
 <column flex="1"/>
 <column/>
 </columns>
 <rows>
 <row>
 <box height="80">
 <label value="PDE-Identity"/>
 <textbox id="pde" value="" multiline="true"/>
 <row>
 <label value="FirstName"/>
 <textbox id="fname" value="" multiline="true"/>
 </row>
 <button id="save" label="save" oncommand="save();"/>
 </box>
 </row>
 </rows>
</grid>
<!--<script>
read();
</script>-->
<script type="application/x-javascript">
<![CDATA[
var savefile = "c:\\mozdata.xml";
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( "File is created " );
 file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
 }
 var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
 .createInstance( Components.interfaces.nsIFileOutputStream );
 /* Open flags
 #define PR_RDONLY 0x01
 #define PR_WRONLY 0x02
 #define PR_RDWR 0x04
 #define PR_CREATE_FILE 0x08
 #define PR_APPEND 0x10
 #define PR_TRUNCATE 0x20
 #define PR_SYNC 0x40
 #define PR_EXCL 0x80
 */
 /*
 ** File modes ....
 **
 ** CAVEAT: 'mode' is currently only applicable on UNIX platforms.
 ** The 'mode' argument may be ignored by PR_Open on other platforms.
 **
 ** 00400 Read by owner.
 ** 00200 Write by owner.
 ** 00100 Execute (search if a directory) by owner.
 ** 00040 Read by group.
 ** 00020 Write by group.
 ** 00010 Execute by group.
 ** 00004 Read by others.
 ** 00002 Write by others
 ** 00001 Execute by others.
 **
 */
 outputStream.init( file, 0x04 | 0x08 | 0x20, 420, 0 );
 var output = document.getElementById('pde').value;
 //var out = document.getElementById('fanme').value;
 var result = outputStream.write( output, output.length );
 outputStream.close();
 alert( "File is updated" );
}
<!--
function read() {
 try {
 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 } catch (e) {
 alert("Permission to read file was denied.");
 }
 var file = Components.classes["@mozilla.org/file/local;1"]
 .createInstance(Components.interfaces.nsILocalFile);
 file.initWithPath( savefile );
 if ( file.exists() == false ) {
 alert("File does not exist");
 }
 var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
 .createInstance( Components.interfaces.nsIFileInputStream );
 is.init( file,0x01, 00004, null);
 var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
 .createInstance( Components.interfaces.nsIScriptableInputStream );
 sis.init( is );
 var output = sis.read( sis.available() );
 document.getElementById('blog').value = output;
}
-->
]]>
</script>
</window>
answered Jul 3, 2011 at 5:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.