mozdev.org

Hello World Javascript Component

Mission Control: "Ok you are a go"
Ship: "Yea, we see the earth now!"




Intro
Here is a quick and dirty example of how to create a Hello World javascript component.
It is using the nsISample code posted here.
This example shows how it is done on unix.
Windows and Mac will be added.


Sample Hello interface source file
Create a file called nsIHello.idl then put the code below inside.
You can use the uuid(IID) below but what you need to do for your
own interfaces is generate a unique number.
On windows you can use

guidgen.exe.
On unix:
$ uuidgen
Using irc, connect to irc.mozilla.org and type in
/join #mozilla
/msg mozbot uuid


nsIHello.idl
#include "nsISupports.idl"

[scriptable, uuid(2484a682-3630-4bf5-95f1-4ba6353a0874)]
interface nsIHello : nsISupports
{
    attribute string yourName;
    void write();
    void change(in string aValue);
};


Sample JS implementation source file
Then create a file called nsHello.js and put this inside.

nsHello.js:


function MyComponent() {}

MyComponent.prototype = {

    get yourName()        { return this.value; },
    set yourName(aName) { return this.value = aName; },

    write: function () { dump("Hello " + this.value + "\n"); },

    change: function (aValue) { this.value = aValue; },

    QueryInterface: function (iid) {

        if (!iid.equals(Components.interfaces.nsIHello) 
				&& !iid.equals(Components.interfaces.nsISupports))
            throw Components.results.NS_ERROR_NO_INTERFACE;
        return this;
    },

    value : "a default value"
}

/******************************************************************************/

var myModule = {

    firstTime  : true,

    registerSelf: function (compMgr, fileSpec, location, type) {

        if (this.firstTime)
        {
            dump("*** first time registration of Hello World JS component\n");
            this.firstTime = false;
            throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
        }

        dump(" ***** Registering: HELLO WORLD JS component! ****\n");
        compMgr.registerComponentWithType(this.myCID,
                                          "My JS Component",
                                          this.myProgID, fileSpec,
                                          location, true, true,
                                          type);
    },

    getClassObject: function (compMgr, cid, iid) {

        if (!cid.equals(this.myCID))
            throw Components.results.NS_ERROR_NO_INTERFACE;

        if (!iid.equals(Components.interfaces.nsIFactory))
            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

        return this.myFactory;
    },

    myCID: Components.ID("{2484a682-3630-4bf5-95f1-4ba6353a0874}"),

    myProgID: "hello_world_component", // the proper format for this would be @mozilla.org/js_hello_world;1

    myFactory: {

        createInstance: function (outer, iid) {
            dump("CI: " + iid + "\n");
            if (outer != null)
                throw Components.results.NS_ERROR_NO_AGGREGATION;


            return (new MyComponent()).QueryInterface(iid);
        }
    },

    canUnload: function(compMgr) {
        dump("****** Unloading: HELLO WORLD JS component! ****** \n");
        return true;
    }
};

function NSGetModule(compMgr, fileSpec) { return myModule; }

/******************************************************************************/






Compiling an xpidl interface

First you will need to set the path of your xpidl compiler in your environment.
Which is located in your mozilla source tree.

mozilla/xpcom/typelib/xpidl

Here is the output of my unix session starting with these two source files from above:

$ ls
nsIHello.idl  nsHello.js


$ PATH=$PATH:/home/petejc/MOZILLA/mozilla/xpcom/typelib/xpidl

$ echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/bin:/usr/X11R6/bin:/root/bin:/home/petejc/MOZILLA/mozilla/xpcom/typelib/xpidl

$ xpidl --help
ERROR: must specify output mode
Usage: xpidl [-m mode] [-w] [-v] [-I path] [-o basename] filename.idl
       -a emit annotations to typelib
       -w turn on warnings (recommended)
       -v verbose mode (NYI)
       -I add entry to start of include path for ``#include "nsIThing.idl"''
       -o use basename (e.g. ``/tmp/nsIThing'') for output
       -m specify output mode:
          header        Generate C++ header            (.h)
          typelib       Generate XPConnect typelib     (.xpt)
          doc           Generate HTML documentation    (.html)
          java          Generate Java interface        (.java)

$ XPIDL_INC=/home/petejc/MOZILLA/mozilla/xpcom/base export XPIDL_INC
$ echo $XPIDL_INC
/home/petejc/MOZILLA/mozilla/xpcom/base

$ xpidl -m typelib -w -v -I $XPIDL_INC \
> -o nsIHello nsIHello.idl

$ ls
nsIHello.idl  nsIHello.xpt  nsHello.js

$ cp nsIHello.xpt nsHello.js \
> /home/petejc/MOZILLA/mozilla/dist/bin/components/


Testing your interface
When you run xpcshell the result should look something like this:

$ cd /home/petejc/MOZILLA/mozilla/dist/bin/
$ js
Type Manifest File: /home/petejc/MOZILLA/mozilla/dist/bin/components/xpti.dat
nsNativeComponentLoader: autoregistering begins.
nsNativeComponentLoader: autoregistering succeeded
 ***** Registering: HELLO WORLD JS component! ****
nNCL: registering deferred (0)
js> const Hello=new Components.Constructor("hello_world_component", "nsIHello");
js> var hello=new Hello();
CI: {2484a682-3630-4bf5-95f1-4ba6353a0874}
js> for(var list in hello)
print(list);
QueryInterface
yourName
write
change
js> hello.yourName;
a default value
js> hello.yourName="Pete";
Pete
js> hello.write();
Hello Pete
null
js> hello.change("Brian");
null
js> hello.write();
Hello Brian
null
js>
js> hello.yourName;
Brian
js>quit();

CanUnload_enumerate: skipping native
From a web page you can do this: <script language="javascript"> netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); var Hello=new Components.Constructor("hello_world_component", "nsIHello"); var h=new Hello(); for(var list in h) document.write(list+"<br>\n"); </script> Enjoy!

--pete

User Notes: [?]

If you do not get a response to a question posted in this forum, please try sending a message to the project's mailing list or to the project owner directly.

[1] Submitted by: Dan Parent on Monday February 12th 2001

In order for the last javascript part to work properly Mozilla had to be restarted.

[2] Submitted by: pete collins on Monday February 12th 2001

Yes. The components are registered when mozilla launches. If there is any new files added to the components dir, they are registered and made available when started or restarted.

--pete

[3] Submitted by: Scott on Saturday March 16th 2002

why the hell did you make it so complex.

[4] Submitted by: dirk on Friday January 3rd 2003

great work! - dl /

[5] Submitted by: Diseño on Monday March 17th 2003

Hi from Spain!!

[6] Submitted by: buscadores on Friday March 21st 2003

the hell? ... it's worse than hell

[7] Submitted by: jonh on Friday April 11th 2003

compared with that hell is heaven ;)

[8] Submitted by: Diseño web on Friday April 18th 2003

This is a very nice site, pleasant layout and design. My compliments to your designer.

[9] Submitted by: Paul on Friday April 18th 2003

Thanks you for the web, it is what I needed to complete my work.
You do a very good work and you must feel proud of it.

[11] Submitted by: complejo on Tuesday May 13th 2003

good site

[12] Submitted by: motilla on Tuesday May 13th 2003

hi from spain

[13] Submitted by: adwod on Tuesday May 13th 2003

corious

[14] Submitted by: Harald on Thursday May 15th 2003

hello world

http://www.suchmaschinenoptimierung-webpromotion.de

[15] Submitted by: complejo on Friday May 16th 2003

good site

[16] Submitted by: motilla on Friday May 16th 2003

hi from spain

[17] Submitted by: adwod on Friday May 16th 2003

corious

[18] Submitted by: shilmar on Monday June 30th 2003

i think is corious, too

[19] Submitted by: no spam please on Wednesday July 9th 2003

l
we can't believe that there is no better spam protection at the notes-pages. you should give tools like a chance, otherwise you should not use url conversion any longer or you have to implement a redirect service that intercepts backlink inheritance.

[20] Submitted by: rx2dz on Wednesday July 16th 2003

I second that

http://rx2dz.tripod.com/2dz/

[21] Submitted by: Diseño web on Friday August 1st 2003

Congratulations for your site

Hector gomis diseño web

[22] Submitted by: Diseño web español on Thursday August 21st 2003

Congratulations for your great job.

Héctor Gomis diseño web estudio

[23] Submitted by: ok on Saturday August 30th 2003

http://www.spain-directory.com

[24] Submitted by: Alex on Sunday August 31st 2003

Nice project!

[25] Submitted by: Gloryhole on Tuesday September 2nd 2003

Cool Project.

[26] Submitted by: David on Thursday September 4th 2003

Nice idea


-----

[27] Submitted by: Thalea on Friday September 5th 2003

Hi, I like the contents of your site,
enjoying to surf within your site.
til then

[28] Submitted by: Sammy Jones on Friday September 5th 2003

Hi, thanks a lot for this site...
Go on like this and enjoy !

[29] Submitted by: Mike on Sunday September 7th 2003

Very helpful comments here, thanks for posting this info.

[30] Submitted by: Diseño web on Monday September 8th 2003

This site its very cool! /

[31] Submitted by: Jose on Thursday September 11th 2003

Very interesting site

[32] Submitted by: Locual on Thursday September 11th 2003

http://locual.com/

[33] Submitted by: JB on Monday September 15th 2003

Very interesting information.
/

[34] Submitted by: Marta on Wednesday September 17th 2003

Congratulations for your site!

[35] Submitted by: Albert on Wednesday September 17th 2003

I just surfed in and found your site, I really enjoyed the visit and hope to come back soon. nice Site!

[36] Submitted by: Urbe on Wednesday September 17th 2003

Hello, I just wanted to say you have a very informative site which really made me think, Thanks !

[37] Submitted by: Martha on Thursday September 18th 2003

This site is interesting as well as informative. Enjoyed browsing through the site. Keep up the good work. Greetings..

[38] Submitted by: Marco on Friday September 19th 2003

Congratulations to your fantastic Site. Thanks

[39] Submitted by: Berberecho on Saturday September 20th 2003

Great Site! There was some interesting Informations on this Page! Really nice! Thank you!

[40] Submitted by: Altaweb on Saturday September 20th 2003

Hello, a really interesting experience to visit your website. For sure i will come back soon. greets to all !

[41] Submitted by: wefw on Tuesday September 23rd 2003

/ /

/ /

/

[42] Submitted by: jims on Tuesday September 23rd 2003

Your work is very interesting.

[43] Submitted by: María on Wednesday September 24th 2003

Hello, a really interesting experience to visit your website. For sure i will come back soon. greets to all !

Questions or comments not answered in the FAQ can be submitted from our feedback page.
Copyright © 2000-2019. All rights reserved. Terms of Use & Privacy Policy.