WebReference.com - Part 3 of chapter 5 from Creating Applications with Mozilla. From O'Reilly (2/4).
[previous] [next] |
Creating Applications with Mozilla, Chapter 5: Scripting Mozilla
Creating XPCOM objects in script
Example 5-10 demonstrates the creation and use of an XPCOM component in JavaScript. In this example, the script instantiates the filepicker
object and then uses it to display a file picker dialog with all of the file filters selected. To run this example, add the function to your xfly.js file and call it from an event handler on the "New" menu item you added in Example 3-5.
Example 5-10: Scriptable component example
// chooseApp: Open file picker and prompt user for application.
chooseApp: function( ) {
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp =
Components.classes["@mozilla.org/filepicker;1"].
createInstance( nsIFilePicker );
fp.init( this.mDialog,
this.getString( "chooseAppFilePickerTitle" ),
nsIFilePicker.modeOpen );
fp.appendFilters( nsIFilePicker.filterAll );
if ( fp.show( ) == nsIFilePicker.returnOK && fp.file ) {
this.choseApp = true;
this.chosenApp = fp.file;
// Update dialog.
this.updateApplicationName(this.chosenApp.unicodePath);
}
Note the first two lines in the function and the way they work together to create the fp
filepicker
object. The first line in the function assigns the name of the nsFile-picker interface to the nsIFilePicker
variable in JavaScript. This variable is used in the second line, where the instance is created from the component to specify which interface on that component should be used. Discovering and using library interfaces is an important aspect of XPCOM, where components always implement at least two interfaces.
In Example 5-11, an HTML file (stored locally, since it wouldn't have the required XPConnect access as a remote file because of security boundaries) loaded in Mozilla instantiates a Mozilla sound component and plays a sound with it. Go ahead and try it.
Example 5-11: Scripting components from HTML
<html>
<head>
<title>Sound Service Play Example</title>
</head>
<body>
<script>
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var url = Components.classes["@mozilla.org/network/standard-
url;1"].createInstance( );
url = url.QueryInterface(Components.interfaces.nsIURL);
url.spec = "resource:/res/samples/test.wav";
var sample = Components.classes["@mozilla.org/sound;1"].createInstance( );
sample = sample.QueryInterface(Components.interfaces.nsISound);
</script>
<form name="form">
<input type="button" value="Play Sound" onclick="sample.play(url);">
<form>
</body>
</html>
As in Example 5-10, the classes[ ]
array on the special Mozilla Components
object refers to a particular component-in this case, the sound
component-by contract ID. All XPCOM objects must have a contract ID that uniquely identifies them with the domain, the component name, and a version number ["@mozilla.org/sound;1"], respectively. See the "XPCOM Identifiers" section in Chapter 8 for more information about this.
Finding components and interfaces
Most components are scripted in Mozilla. In fact, the challenge is not to find cases when this scripting occurs (which you can learn by searching LXR for the Components
), but to find Mozilla components that don't use scriptable components. Finding components and interfaces in Mozilla and seeing how they are used can be useful when writing your own application.
The Mozilla Component Viewer is a great tool for discovering components and provides a convenient UI for seeing components and looking at their interfaces from within Mozilla. The Component Viewer can be built as an extension to Mozilla (see "cview" in the extensions directory of the Mozilla source), or it can be downloaded and installed as a separate XPI from https://www.hacksrus.com/~ginda/cview/. Appendix B describes the Component Viewer in more detail.
Commonly used XPCOM objects in the browser and other Mozilla applications include file objects, RDF services, URL objects, and category managers.
Selecting the appropriate interface from the component
In all cases, the way to get the object into script is to instantiate it with the special classes
object and use the createInstance( )
method on the class to select the interface you want to use. These two steps are often done together, as in the following example, which gets the component with the contract ID ldap-connection;1
, instantiates an object from the nsILDAPConnection interface, and then calls a method on that object:
var connection = Components.classes
["@mozilla.org/network/ldap-connection;1"].
createInstance(Components.interfaces.nsILDAPConnection);
connection.init(queryURL.host, queryURL.port, null,
generateGetTargetsBoundCallback( ));
These two common processes-getting a component and selecting one of its interfaces to assign to an object-can also be separated into two different statements:
// get the ldap connection component
var connection = Components.classes
["@mozilla.org/network/ldap-connection;1";
// create an object from the nsILDAPConnection interface;
connection.createInstance(Components.interfaces.nsILDAPConnection);
// call the init( ) method on that object
connection.init(queryURL.host, queryURL.port, null,
generateGetTargetsBoundCallback( ));
Mozilla constantly uses these processes. Wherever functionality is organized into XPCOM objects (and most of it is), these two statements bring that functionality into JavaScript as high-level and user-friendly JavaScript objects.
[previous] [next] |
Created: October 3, 2002
Revised: October 3, 2002
URL: https://webreference.com/programming/javascript/mozillaapps/chap5/3/2.html