Use JavaScript to create the XPCOM component

Source: Internet
Author: User
Author: Builder
PM

WithFirefoxMarket Share continues to rise, cross-platform Component Object Model (Cross Platform Component Object Model) Is also developing rapidly. Here we will give an example to illustrate how to use this object model.

 The Mozilla Browser is opening a world that uses cross-platform Component Object Model (XPCOM. The Mozilla Browser also introduces the xpconnect technology, which allows components to be executed in the browser and can be developed using JavaScript.

The work of the XPCOM component is similar to that of the Microsoft COM component. The communication between the customer and the component is completed through the interface. The basic interface of XPCOM is nsisupports. You can use the QueryInterface method of nsisupports to obtain information about the specified interface (including the methods and attributes supported by the interface ).

[Blocked ads]

The javascript XPCOM component must use the Interface Definition Language (IDL) to implement the nsisupport interface. The IDL file is created and compiled into an xpt library. The component Registrar uses the Mozilla registration component based on the information in the xpt file. Once the component is successfully registered, you can use the XPCOM component in your code.

In this example, I will use JavaScript to create an XPCOM component that exposes the reverselt () method. This method accepts a string type parameter and reverses the character order in the string.

The XPCOM component consists of three layers: the XPCOM Object layer, the nsifactory layer, and the nsimodule layer. The XPCOM Object layer is the core of the system, and the business logic is placed on this layer. The nsifactory layer is responsible for instantiating the XPCOM object. The nsimodule layer registers and provides nsifactory abstraction. All three layers are required, and each layer requires some methods. You can see from the following code:

Const mycomponent_contractid = '@ mozilla.org/mycomponent1_1 ';
Const mycomponent_cid = components. ID ('{f 443406c-961d-4ecc-bf39-
C0e 9943c 72ae }');
Const mycomponent_iid = components. Interfaces. nsimycomponent;

Function nsmycomponent (){}
Nsmycomponent. Prototype = {
Reverseit: function (s ){
VaR A = S. Split ("");
A = A. Reverse ();
Return A. Join ("");
},
QueryInterface: function (IID ){
If (! IID. Equals (components. Interfaces. nsisupports )&&
! IID. Equals (mycomponent_iid ))
Throw components. Results. ns_error_no_interface;
Return this;
}
};

VaR nsmycomponentmodule = {
Registerself: function (compmgr, filespec, location, type ){
Compmgr = compmgr. QueryInterface (components. Interfaces. nsicomponentregistrar );
Compmgr. registerfactorylocation (mycomponent_cid,
"Mycomponent test ",
Mycomponent_contractid,
Filespec,
Location,
Type );
},
Getclassobject: function (compmgr, CID, IID ){
If (! CID. Equals (mycomponent_cid ))
Throw components. Results. ns_error_no_interface;
If (! IID. Equals (components. Interfaces. nsifactory ))
Throw components. Results. ns_error_not_implemented;
Return nsmycomponentfactory;
},
Canunload: function (compmgr) {return true ;}
};
VaR nsmycomponentfactory = {
Createinstance: function (outer, IID ){
If (outer! = NULL)
Throw components. Results. ns_error_no_aggregation;
If (! IID. Equals (mycomponent_iid )&&
! IID. Equals (components. Interfaces. nsisupports ))
Throw components. Results. ns_error_invalid_arg;
Return new nsmycomponent ();
}
};

Function nsgetmodule (commgr, filespec) {return nsmycomponentmodule ;}

The XPCOM component is registered by a person who can understand it. The contract ID of XPCOM is similar to the progid in the Microsoft COM component. In my example, the value is '@ mozilla.org/mycomponent1_1 '.

Contract IDs use the namespace concept to identify themselves. Therefore, it is important to add the '@ resolve illa.org/' prefix before the contract ID. The ID of a 128-bit UUID that uniquely identifies a component. The ID () method of the Component Object accepts a uuid string as a parameter and returns an nsid object for registration and instantiation. (I used Windows guidgen.exe to create UUID ). The component then has an interface ID (IID), which is the name of the interface described in the xpt file. In this example, IID is nsimycomponent.

If you analyze the code, you will find that the actual customer business logic is the reverselt () method of the nsmycomponent class. The rest of the code is used to make the component work in XPCOM. If you want to create your own components from my example code, you need to add your own methods and attributes to the nsmycomponent class in addition to copying everything as is.

To enable the XPCOM system to understand the nsimycomponent interface and export methods, you must also provide an xpt library. The xpt library is provided by first creating an IDL file and then compiling it with xpidl.exe. The following is the IDL of mycomponent:

# Include "nsisupports. IDL"

[Scriptable, UUID (F 443406c-961d-4ecc-bf39-c0e 9943c 72ae)]
Interface nsimycomponent: nsisupports {
String reverseit (in string S );
};

To implement the nsisupport interface on the nsimycomponent interface, the IDL file contains the nsisupport. IDL file. Declare the reverselt () method and specify its return type and its accepted parameters.

I didn't find a normal download of xpidl.exe, so I created it from the fragmented file downloaded by mozilla.org. I have successfully run this program on Windows 2000 and XP. You can find this normal download at http://www.geocities.com/phil_perkins_1/articles/XPIDL/xpidl.zip. If you extract it to a separate directory, you can use the-H option to obtain information about its usage.

To make this example run, you need to save the source code to "nsmycomponent. js" under the "components" directory in your system's Mozilla directory. Then, in the directory where you decompress xpidl.zip, save IDL as "mycomponent. IDL ". Locate the directory from the command line and enter:

Xpidl-M typelib-w-v-e [path to IDL file]/mycomponent. xpt [path to IDL file]/mycomponent. IDL

This shoshould create a file called "mycomponent. xpt". Copy this file to the "components" directory under Mozilla.

In this way, a file named "mycomponent. xpt" will be created. Copy the file to the "components" directory under illiillia.

Locate the Mozilla directory from the command line and find "regxpcom.exe ". Type "regxpcom" and press Enter. In this way, all your components will be registered with Mozilla. Then you can open the "compreg. dat" file to check whether your component has been successfully registered. Do not edit this file-this is an automatically generated file. If your Mozilla window is open, you should close all Mozilla windows before trying to use the new component. To test this component, save the following HTML to a file and open it in Mozilla:

Check the Javascript console to see if there are any errors. You can get the source code of this example here.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.