MozNET Tutorial - Javascript XPCOM Component
EDIT: As of R18, the component created in this tutorial is no longer required for taking screen shots with MozNET but, it still has value in that it will show you how to create a Javascript based XPCom component.
Note that if you are here looking for a tutorial that will work with another XulRunner wrapper or with FireFox, itself, you are looking in the wrong place.
This tutorial will walk you through the steps required to create and register a Javascript based XPCom component to be used with MozNet.
Instead of building the normal 'Hello World' introductory tutorial I'm going to show you how to build something that can actually be useful.
We're going to build a component that will allow us to take a full page screen shot of a given web site. It's not going to have a bunch of bells
or whistles but, it'll be enough to get your hands dirty and at the same time build something usable.
First thing first. We need a decent, light-weight, text editor. While Visual Studio is great for working with code files it's a bit overkill for what we're doing here.
A good editor to use would be something such as Programmer's Notepad or Nodepad++ or something similar. I use Programmer's Notepad,
myself. It's free, light-weight, and has quite a few features. Whenever I'm working with IDL files this is my editor of choice. That being said, I'm sure you have you're own preference for text editors so
feel free to use your favorite.
Let's get started...
The second thing we need is a base, something to start building our component from. Below you'll find the skeletal block of code that we'll use as a template
for building our XPCOM component.
Open your favorite text editor and paste the code below into a new, empty, document. Save it as 'ExampleComponent.js'.
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cc = Components.classes;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function ExampleComponent() { }
ExampleComponent.prototype = {
classDescription: "Example Javascript XPCOM Component",
classID: Components.ID("{12345678-9012-3456-7890-ABCDEF123456}"),
contractID: "@example.com/examplecomponent;1",
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIExampleComponent]),
};
var components = [ExampleComponent];
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(components);
}
Not a whole lot to it, eh? We'll, that's about as simplified as it can get actually. Now, let's do something with it.
I said we were going to build something useful so let's get started. MozNet lacks a good way of grabbing a full page screen
shot of a web site because the interface method provided by XulRunner cannot be used from C# (nor c++). Javascript on the
other hand, can easily access the interface method we need. We're going to use this tutorial to build a component that will
add the missing feature to MozNet.
Press the 'SPACE BAR' to go to the next page..