WebReference.com - Part 3 of chapter 5 from Creating Applications with Mozilla. From O'Reilly (3/4). | WebReference

WebReference.com - Part 3 of chapter 5 from Creating Applications with Mozilla. From O'Reilly (3/4).

To page 1To page 2current pageTo page 4
[previous] [next]

Creating Applications with Mozilla, Chapter 5: Scripting Mozilla

JavaScript Application Code

There are two ways to use JavaScript in the third, deepest level of application programming. The first is to organize your JavaScript into libraries so your functions can be reused, distributed, and perhaps collaborated upon.

The second way is to write a JavaScript component, create a separate interface for that component, and compile it as an XPCOM component whose methods and data can be accessed from XPConnect (using JavaScript). This kind of application programming is described in Chapter 8, which includes examples of creating new interfaces, implementing them in JavaScript or C++, and compiling, testing, and using the resulting component in the Mozilla interface.

This section introduces the library organization method of JavaScript application programming. The JSLib code discussed here is a group of JavaScript libraries currently being developed by Mozilla contributors and is especially useful for working with the XPFE and other aspects of the Mozilla application/package programming model. When you include the right source files at the top of your JavaScript and/or XUL file, you can use the functions defined in JSLib libraries as you would use any third-party library or built-in functions. You may even want to contribute to the JSLib project yourself if you think functionality is missing and as your Mozilla programming skills grow.

JavaScript Libraries

The open source JSLib project makes life easier for developers. The JSLib package implements some of the key XPCOM components just discussed and wraps them in simpler, JavaScript interfaces, which means that you can use the services of common XPCOM components without having to do any of the instantiation, interface selection, or glue code yourself. Collectively, these interfaces are intended to provide a general-purpose library for Mozilla application developers. To understand what JSLib does, consider the following short snippet from the JSLib source file jslib/io/file.js, which implements a close( ) function for open file objects and provides a handy way to clean up things when you finish editing a file in the filesystem.

/********************* CLOSE ********************************
* void close( )                                              *
*                                                           *
* void file close                                           *
* return type void(null)                                    *
* takes no arguments closes an open file stream and         *
* deletes member var instances of objects                   *
*   Ex:                                                     *
*     var p='/tmp/foo.dat';                                 *
*     var f=new File(p);                                    *
*     fopen( );                                              *
*     f.close( );                                            *
*                                                           *
*   outputs: void(null)                                     *
************************************************************/
File.prototype.close = function( )
{
/***************** Destroy Instances *********************/
if(this.mFileChannel)   delete this.mFileChannel;
if(this.mInputStream)   delete this.mInputStream;
if(this.mTransport)     delete this.mTransport;
if(this.mMode)          this.mMode=null;
if(this.mOutStream) {
this.mOutStream.close( );
delete this.mOutStream;
}
if(this.mLineBuffer)     this.mLineBuffer=null;
this.mPosition           = 0;
/***************** Destroy Instances *********************/
return;
}

To use the close method as it's defined here, import the file.js source file into your JavaScript, create a file object (as shown in the examples below), and call its close( ) method.


xpcshell

Most examples in this section are in xpcshell, but using these libraries in your user interface JavaScript is just as easy. You can access these libraries from a XUL file, as the section "Using the DirUtils class," later in this chapter, demonstrates.

xpcshell is the command-line interpreter to JavaScript and XPConnect. This shell that uses XPConnect to call and instantiate scriptable XPCOM interfaces. It is used primarily for debugging and testing scripts.

To run xpcshell, you need to go to the Mozilla bin directory or have that folder in your PATH. For each platform, enter:

Windows:

xpcshell.exe

Unix:

./run-mozilla.sh ./xpcshell

To run xpcshell on Unix, you need to supply environment variables that the interpreter needs. You can use the run-mozilla.sh shell script that resides in the Mozilla bin directory.

$ ./run-mozilla.sh ./xpcshell

To see the available options for xpcshell, type this:

$ ./run-mozilla.sh ./xpcshell --help
JavaScript-C 1.5 pre-release 4a 2002-03-21
usage: xpcshell [-s] [-w] [-W] [-v version] [-f scriptfile] [scriptfile] [scriptarg...]

The two most important parameters here are -w, which enables warnings output, and -s, which turns on strict mode.

The source files for JSLib are well annotated and easy to read. JSLib provide easy-to-use interfaces for creating instances of components (e.g., File objects), performing necessary error checking, and ensuring proper usage. To use a function like the one just shown, simply include the source file you need in your XUL:

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

Then you can include the specific library files you need in your JavaScript code by using the include method:

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

Installing JSLib

To use the JavaScript libraries, install the JSLib package in Mozilla. The package is available as a tarball, a zip file, or as CVS sources. The easiest way to obtain it is to install it from the Web using Mozilla's XPInstall technology, described in Chapter 6.

Using your Mozilla browser, go to https://jslib.mozdev.org/installation.html and click the installation hyperlink. The link uses XPInstall to install JSLIB and make it available to you in Mozilla. To test whether it is installed properly, type the following code in your shell:

./mozilla -chrome chrome://jslib/content/

You should see a simple window that says "welcome to jslib."


To page 1To page 2current pageTo page 4
[previous] [next]

Created: October 3, 2002
Revised: October 3, 2002

URL: https://webreference.com/programming/javascript/mozillaapps/chap5/3/3.html