MozNet Tutorial - Javascript XPCOM Component
Now we're just a few steps away from having a working XPCOM component. So far we've taken the skeleton and filled
it in with some code that will do something more than say 'Hello World!'. I'd say we're doing pretty good so far
but, we can't count our chickens before they hatch. We've got more work ahead of us so don't say 'Look what I can do!' just yet.
Since we've got our javascript file taken care of we need a way to tell XPCom that we've got a component that we
want to call methods on. This is done by creating a XPT file. But, before we can create that we need to make an IDL.
IDL stands for Interface Definition Language and is the basic way of defining interfaces in XulRunner. To write your own
components, like extending MozNet, you'll want to have the SDK version of XulRunner. Since the current version of
MozNet is targeting XulRunner 1.9.2.17 you'll need the SDK for that build.
You can download it here.
Let's define our IDL file. We'll use this file in the next step, creating the XPT file. In your text editor open a new,
blank, document and paste or type the code below. Save the document next to the 'ExampleComponent.js' file we created
earlier and name it 'nsIExampleComponent.idl'.
#include "nsISupports.idl"
#include "nsIDOMHTMLCanvasElement.idl"
/* Note that the uuid (Guid in c#) is the same as we defined in ExampleComponent.js */
[scriptable, uuid(12345678-9012-3456-7890-ABCDEF123456)]
interface nsIExampleComponent : nsISupports
{
/* This is the method that we defined in ExampleComponent.js
* Also, note the use of the 'in' keyword. All params must be marked as 'in' or 'out' or '[in,out]' as needed.*/
string getPageImage(in nsIDOMWindow wnd);
};
Our IDL file must retain the same structure and syntax as other XulRunner IDL files that you'll find in the SDK. The tool
that will create the XPT from your IDL file expects a proper input file and is not forgiving of any mistakes.
Press the 'SPACE BAR' to go to the next page..