jslib and appending files

Talk about add-ons and extension development.
Post Reply
dangbat
Posts: 14
Joined: December 18th, 2002, 1:00 am

jslib and appending files

Post by dangbat »

Hello all,
This is actually a repost from a question I posted in the Phoenix forums, but apparently it's more Mozilla-related. In case anyone wants to know the original URL, it was http://www.mozillazine.org/forums/viewtopic.php?t=4499

----

Here is the code I'm trying out (the file test.txt already exists and file.js from jslib is being properly included):

function testAppendO() {
var f=new File('c:\\test.txt');
f.open('a');
f.write('This is a new line #1\n');
f.write('this is a new line #2\n');
f.close();
f.open('a');
f.write('This is a new line #3\n');
f.write('this is a new line #4\n');
f.close();
}

I just put this so that it happens when the extension is loaded (so, once the browser starts). The contents of test.txt will end up being

This is a new line #3
this is a new line #4

Am I doing something seriously wrong here? If I understand the concept of appending a file, the contents should remain unchanged, correct? It seems that this is reacting as if I used f.open('w'); instead. One workaround that I have found is to f.open('r'), then contents=f.read(), then open it up again, and write(contents). However, that doesn't always work (sometimes it thinks the contents is null, even though if I do an alert, they show up as the file contents). This workaround does not work for onload events, for instance, just for the initial browser load.

I am not stuck on using jslib, but I really need to be able to append files. Honestly, I have seen very little documentation on this, but if there is a tutorial (other than the ONE XUL tutorial) for creating/writing files with javaScript, it would be great to know about it. Just understanding this particular bug/issue would be enough, though.

-D

System: windows 2000, Phoenix 0.5, jslib build from 1/15/03
joebob2406
Posts: 26
Joined: January 7th, 2003, 8:04 am

Same Boat

Post by joebob2406 »

Hello.

I'm actually try to work with the jslib as well but I can't even get it embedded in the HTML page itself.

I want to be able to create files and manipulate them with javascript inside an html or xul page.

My simple test page is this:

<?xml version="1.0"?>
<!-- Sample XUL file -->
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<script type="application/x-javascript" src="chrome://jslib/content/jslib.js"/>
<script>
include('chrome://jslib/content/io/file.js');
var f = new File('test.txt');
function writeFile() {
f.create;
alert('it worked');
}
</script>

<box align="center">
<button label="hell xfly" onclick="writeFile();"/>
</box>
</window>


When I run it in mozilla, I get an error in the javascript console saying:

Error: f has no properties
Source File: file:///C:/Documents%20and%20Settings/Jason/My%20Documents/Mozilla%20XUL%20examples/hello.xul
Line: 10

Error: uncaught exception: Permission denied to create wrapper for object

Don't know what I'm doing wrong. Please help me.

Also, when you don't specify a full path like I didn't above (ie not c:\test.txt) where is the default?

Thanx
dangbat
Posts: 14
Joined: December 18th, 2002, 1:00 am

Post by dangbat »

Hey joebob. Here's what the problem is:

f.create doesn't exist. It's f.create().

Also, try specifying the entire path (as c:\\test.txt) for testing purposes. I tried it your way, but I couldn't find the file anywhere. It might be a cause of error as well.

Finally, if changing those two things didn't help, then try adding the following line to the top of your script:

netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

I managed to get your code working without it, but I'm not sure if that line is executed by any other extensions I'm running. Just something to keep in the back of your mind.

I'm amazed no one here (or in Mozilla General, I just saw your post there) has managed to give you a straight answer: your code is wrong.

Oh, and to access files inside the chrome directory, take a look at the FileUtils from jslib.

Cheers, and hey, if you can figure out my append problem, we'll call it even ;)

-D

PS: I tried this as a XUL page, I'm not entirely certain if it would work embedded in an HTML page. It might not, due to permissions.
User avatar
byron
Posts: 48
Joined: November 4th, 2002, 7:08 pm
Location: perth, australia
Contact:

Post by byron »

be aware that the checkin for <a href="http://bugzilla.mozilla.org/show_bug.cgi?id=176919">bug 176919</a> make cause jslib file o/i breakage.
joebob2406
Posts: 26
Joined: January 7th, 2003, 8:04 am

Why won't it work.

Post by joebob2406 »

Hi Dangbat.

You said you that you got my code working? Please help? haha. I did everything you said and even tried upgrading my privileges in netscape and it didn't work. This is my file that I'm trying to open in mozilla.

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window
id="test-window"
title="Test"
orient="horizontal"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

<script type="application/x-javascript" src="chrome://jslib/content/jslib.js"/>

<script>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");

include('chrome://jslib/content/io/file.js');

function writeFile() {
include('chrome://jslib/content/io/file.js');
var f = new File('c:\\jason.txt');
f.create();
alert('it worked');
}

</script>

<box align="center">
<button label="Write" onclick="writeFile();"/>
</box>
</window>

I saved that file as test.xul in the following folder:

/chrome/test/content/test/test.xul

I added the following line to the installed-chrome.txt:

content,install,url,resource:/chrome/test/content/test/

I installed jslib from the jslib.mozdev.org site via an xpi file. When it installed it didn't add anything to the installed-chrome.txt file and it installed to:

/chrome/jslib/ and not /chrome/jslib/content/jslib

I tried leaving everything the way it was and executing my xul file and I also tried changing the path to the jslib to

/chrome/jslib/content/jslib rather than just /chrome/jslib/

The reason I did this is because in my file above this line made me wonder:

<script type="application/x-javascript" src="chrome://jslib/content/jslib.js"/> cause jslib didn't have a content subfolder in it.

Finally, when I open this folder in mozilla i go file|open and then open the test.xul file. Is this the way I'm supposed to be doing this? also, when i save the test.xul file after making changes, I just reload the page in mozilla. Does this work?

I have not clue why this is not working so if anyone can offer any suggestions that would be great. I'm kinda thinking jslib didn't install correctly??

Thanx in advance.
cdn
Posts: 999
Joined: November 4th, 2002, 5:47 pm
Location: UK
Contact:

working example (under Moz 1.0.2/1.2.1 anyway)

Post by cdn »

mozilla/chrome:

installed-chrome.txt ;

Code: Select all

content,install,url,jar:resource:/chrome/jslib.jar!/
content,install,url,resource:/chrome/chromedit/content/

-=-=-

under mozilla/chrome/chromedit/content/:

contents.rdf ;

Code: Select all

<?xml version="1.0"?>

<RDF:RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:chrome="http://www.mozilla.org/rdf/chrome#">

  <!-- List all the packages being supplied by this XPI -->
  <RDF:Seq about="urn:mozilla:package:root">
    <RDF:li resource="urn:mozilla:package:chromedit" />
  </RDF:Seq>

  <!-- Package information -->
  <RDF:Description about="urn:mozilla:package:chromedit"
        chrome:displayName="Chromedit"
        chrome:author="Andrew Wooldridge, Chris Neale"
        chrome:description="A Simple User Stylesheet Editor; Prerequisite jslib"
        chrome:name="chromedit">
  </RDF:Description>
</RDF:RDF>


chromedit.js ;

Code: Select all

// globals

function loadMe(){

        alert("Welcome to ChromEdit. Remember to LOAD pages first, unless you want them overwritten."+"And you must restart for changes to take effect.")

}

function getTextareaText(tNodeID){
        var tat = document.getElementById(tNodeID).value
        return tat

}

function setTextareaText(thetext,tNodeID){

        document.getElementById(tNodeID).value= thetext


}

function loadIt(fileName,targetNodeID){
var d = new DirUtils;
var p                   = d.getUserChromeDir(); //H UserChr
var fName               = fileName;
var f                           = new File(p);
var target      = f.nsIFile;
target.append(fName);
//alert(target.path)
f=new File(target.path);

f.open()
var stuff=f.read()
f.close()
        setTextareaText(stuff,targetNodeID);

}

function saveIt(fileName,targetNodeID){
var data = getTextareaText(targetNodeID);
var d = new DirUtils;
var p                   = d.getUserChromeDir(); // getHomeDir();
var fName               = fileName;
var f                           = new File(p);
var target      = f.nsIFile;
target.append(fName);
f=new File(target.path);

f.open('w')
f.write(data)
f.close()

}


chromedit.xul ;

Code: Select all

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="main-window" xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  onload="loadMe()"
  onunload=""
  title="defaultWindow"
  viewsourcetitlepreface="sourceview"
  windowtype="navigator:browser"
  align="vertical"
  width="640" height="480"
  screenX="10" screenY="10"
  persist="screenX screenY width height sizemode">

<script src="chrome://jslib/content/jslib.js" />
<script src="chrome://jslib/content/io/io.js" />
<!-- file.js & dirUtils.js -->

<script src="chrome://chromedit/content/chromedit.js" />

<tabbox id="tabbox" flex="1">
  <tabs orient="horizontal">
      <tab id="generalTab" label="userChrome.css"
           accesskey="u"/>
      <tab id="formsTab"   label="userContent.css"
           accesskey="r"/>
      <tab id="deformedTab"   label="user.js"
           accesskey="d"/>
    </tabs>
    <tabpanels id="tabpanels" flex="1">
      <vbox id="userChromeTab" orient="vertical">
        <hbox autostretch="never" style="border:2px solid red;">
                <textbox id="editChrome" class="" multiline="true" value="" rows="30" cols="80" />
        </hbox>

        <hbox  id="" class="" orient="horizontal">
                <button id="" class="" value="load" label="load" oncommand="loadIt('userChrome.css','editChrome')" />
                <button id="" class="" value="save" label="save" oncommand="saveIt('userChrome.css','editChrome')" />
        </hbox>
      </vbox>

      <vbox id="userContentTab" orient="vertical">
      <hbox autostretch="never" style="border:2px solid green;">
        <textbox id="editContent" class="" multiline="true" value="" rows="30" cols="80" />
</hbox>
<hbox id="" class="" orient="horizontal">
        <button id="" class="" label="load" value="load" oncommand="loadIt('userContent.css','editContent')" />
        <button id="" class="" label="save" value="save" oncommand="saveIt('userContent.css','editContent')" />
</hbox>

      </vbox>

      <vbox id="chromeTab" orient="vertical">
        <hbox autostretch="never" style="border:2px solid red;">
                <textbox id="editchromeRdf" class="" multiline="true" value="" rows="30" cols="80" />
        </hbox>

        <hbox  id="" class="" orient="horizontal">
                <button id="" class="" value="load" label="load" oncommand="loadIt('user.js','editchromeRdf')" />
                <button id="" class="" value="save" label="save" oncommand="saveIt('user.js','editchromeRdf')" />
        </hbox>
      </vbox>

    </tabpanels>
    </tabbox>
</window>


-=-=-

enter chrome://chromedit/content/chromedit.xul in urlbar

edit user.js userChrome.css userContent.css
Post Reply