Using components

XPConnect works transparently in Mozilla and xpcshell to give you access to XPCOM components.

Commonly, we start our scripts like so:

var Cc = Components.classes;
var Ci = Components.interfaces;

If we want to get a hold of a component, we then do something like:

var rc = Cc["@mozilla.org/registry;1"];
var rs = rc.getService(Ci.nsIRegistry);

See also:

  • xpcshell -- how to get a command line interface to JavaScript

More Info

As was already stated, it is common to start addon scripts like:

var Cc = Components.classes;
var Ci = Components.interfaces;

There is also another way to start, which is exactly equivalent to the above.

var {Cc: classes, Ci: interfaces} = Components;

There are many more than just classes and interfaces.

var {
    Cu: utils,
    Ci: interfaces,
    Cc: classes,
    Cr: results,
    Cs: stack,
    Cm: manager,
    Ce: Exception,
} = Components;

Here is a full breakdown of what is contained in Components. Any of the below can be accessed by Components.BLAH (IE: Components.isSuccessCode)

utils=[object nsXPCComponents_Utils]
interfaces=[object nsXPCComponents_Interfaces]
classes=[object nsXPCComponents_Classes]
results=[object nsXPCComponents_Results]
isSuccessCode=function isSuccessCode() {
[native code]
}
Constructor=[object nsXPCComponents_Constructor]
QueryInterface=function QueryInterface() {
[native code]
}
interfacesByID=[object nsXPCComponents_InterfacesByID]
classesByID=[object nsXPCComponents_ClassesByID]
stack=JS frame :: Scratchpad/4 :: cDump :: line 8
manager=[xpconnect wrapped nsIComponentManager]
ID=[object nsXPCComponents_ID]
Exception=[object nsXPCComponents_Exception]
reportError=function reportError() {
[native code]
}
canCreateWrapper=function canCreateWrapper() {
[native code]
}
canCallMethod=function canCallMethod() {
[native code]
}
canGetProperty=function canGetProperty() {
[native code]
}
canSetProperty=function canSetProperty() {
[native code]
}

Document Tags and Contributors

 Last updated by: Sheppy,