Changes

Jump to: navigation, search

Real World Mozilla First XPCOM Component

1,682 bytes added, 13:02, 19 March 2007
Testing FirstXpcom
= Testing FirstXpcom =
 
== Checking the Add-on Manager ==
We'll write formal tests and code to use our component later. For now, make sure it gets loaded into Firefox and is visible in the Addon Manager. Run Firefox and make sure you can see your extension in the addon manager:
$ export MOZ_DEBUG_BREAK=warn
$ firefox.exe -Profilemanager --no-remote
 
== Accessing FirstXpcom from the JavaScript Shell ==
 
Now let's try and access this from JavaScript in the browser. If you haven't done so already, download and install the [http://ted.mielczarek.org/code/mozilla/extensiondev/index.html Extension Developer's extension]. This will allow you to use the [http://developer.mozilla.org/en/docs/Introduction_to_the_JavaScript_shell JavaScript Shell] inside the browser, making it easy to try out the firstxpcom component.
 
Launch the JS Shell ('''Tools > Extension Developer > Javascript Shell''') and write some code to access your XPCOM component. You can work interactively without having to define functions, write a complete extension, etc.:
 
* Define our component's ID so we can create an instance of it below.
 
const cid = "@senecac.on.ca/firstxpcom;1"
print(cid)
* Now create an instance of our component. The value of obj will be nsISupports at this point (i.e., we can't call IFirstXpcom's methods yet).
 
var obj = Components.classes[cid].createInstance()
* Next, take the the nsISupports object returned above and change it to IFirstXpcom, often referred to as QI (e.g., ''"...you need to QI it to IFirstXpcom..."'').
 
obj = obj.QueryInterface(Components.interfaces.IFirstXpcom)
* At this point we have the ability to use the IFirstXpcom methods and attributes:
 
var sum
sum = obj.add(4,5)
var name
name = "FirstXpcom!"
obj.name = name
print(obj.name)
alert(obj.name)
 
When you run this code, also notice how your C++ printf statements are causing messages to stdout in your console window (you may need to scrollback through all the other messages to find them).

Navigation menu