Click here to Skip to main content
13,457,738 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 28 Sep 2007

A C++ Firefox component intercepting/operating HTML DOM

, 10 Oct 2007
Rate this:
Please Sign up or sign in to vote.
This article uses a simple example to show how to build a Firefox component intercepting/operating DOM


I have been working on BHO for a few months now. It's neat to develop BHO components. On the contrary, Firefox seems really frustrating. I guess the first reason is that there are never enough examples and codes for you to follow. Secondly, Mozilla does not provide enough documents like Microsoft does. Well, I don't blame them for that. It's a small company and it's free source after all.

In the following sections, I will use an example to show you how to intercept Firefox's DOM elements and operate them like in BHO.

There are two articles I strongly recommend you to read before you scroll down this page.

The first one teaches you how to build a component; the second teaches you how to make an extension.

The example here is based on the example in the first article.

It took me two whole weeks to learn how Firefox as well as XPCOM operate. I will use this simple graph to demonstrate the structure:

UI--> XPConnect--> XPCOM

Most of the add-ons you have seen and you can download are extensions such as a toolbar etc. The majority of them are written with JavaScript. It's alright and neat if you don't want to touch some advanced components; for instance, a toolbar only navigates the window, etc. But if you want to go further, you have to call components built under XPConnect or XPCOM.

The following code is a simple cookie manager from Mozilla's official tutorial.

var cookiemanager = Components.classes[";1"].getService();
cookiemanager = cookiemanager.QueryInterface(Components.interfaces.nsICookieManager);
// called as part of a largerDeleteAllCookies() function 
function FinalizeCookieDeletions() {
  for (var c=0; c<deletedCookies.length; c++) {
    cookiemanager.remove(deletedCookies[c].host, deletedCookies[c].name,
  deletedCookies.length = 0;

It is quite easy to understand the procedure.

  • Get Service of Components
  • QI the components
  • Call the methods under the components

There isn't too much technology in this part. But, how the components work is another issue.

Firefox is based on the Gecko SDK. Basically, all those APIs that start with "ns" such as nsIWebBrowser (similar to IWebBrowser in BHO) are from the Gecko SDK. You can find more information on

Let's get to the point. Now, I want to build a FormFiller for Firefox. Meanwhile, I do not want to use JavaScript to do the job due to the following reasons:

  • Javascript will expose your code.
  • Javascript has very limited functionality.

So, here are two solutions:

  • Build a Firefox extension with C++
  • Build a Firefox component with C++

I go for the second. Although registering a component is far more complicated than registering an extension on Firefox. But it's much more secure when calling core components and operates the data.

How to do it

Like BHO, there is a nsIWebBrowser that provides the APIs of any XPCOM based browser such as SeaMonkey or NetScape. Through nsIWebBrowser, we can get nsIDOMWindow->nsIDOMDocument->nsIDOMElement then fill in the information we want. However, this does not work in Firefox. The reason is very simple: Firefox does not use nsIWebBrowser. It will return some random useless stuff when you call nsIWebBrowser via Component Manager.

I have no idea why they do that. Maybe it's because of the multi-tab structure of Firefox. So our problem is here. We must initialize nsIDOMWindow before we move further.

To save your time on Googling all the information, please let me present this simple function prototype:

var res = obj.Nothing(window);

where Nothing is the method called for filling in forms. "window" is the browser window object that JavaScript naturally has.

So inside your code, you have the method declared as:

NS_IMETHOD Nothing(nsIDOMWindow *domWindow); //.h file

NS_IMETHODIMP MyComponent::Nothing(nsIDOMWindow *domWindow) //cpp file

When JavaScript passed the window into the method, it will be automatically converted into nsIDOMWindow.

OK, half of the job has been done.

The other half is easy. Look at the following code and you will understand.

NS_IMETHODIMP MyComponent::Nothing(nsIDOMWindow *domWindow)
    nsEmbedString temp(L"test");
    nsEmbedString attribute(L"value");
    nsEmbedString id(L"id");
    nsEmbedString value(L"value");

    //The above declare some strings. 
    Please note that the string in XPCOM is declared as either nsAString 
    or nsString or nsEmbedString Please refer to for more information

    nsIDOMWindow* window; //you can use the parameter directly
    nsCOMPtr<nsIDOMDocument> domDocument; 
    nsCOMPtr<nsIDOMElement> element;
    window->GetDocument(getter_AddRefs(domDocument)); //Get document
    //Find target element, you also can use GetElementByTagName
    //to get a node list. Then loop through the node list.
    //Please refer to XULPlanet for more info
    //about nsIDOMDocument
    //I get the value of "id" attribute
    //and fill it into "value attribute"
    return NS_OK;

OK, above is the all the code you will need.

How to build the project

I strongly recommend I learned most of the things about XPCOM components there.

To start your project, you need to have a .idl file.

#include "nsISupports.idl"
#include "nsIDOMWindow.idl"
[scriptable, uuid(_YOUR_INTERFACE_GUID_)]
interface IMyComponent : nsISupports
  void Nothing(in nsIDOMWindow domWindow);

Then download XPIDL.exe to generate the .h file:

Quote from the article:

  • xpidl -m header -I_DIR_ IMyComponent.idl will create the IMyComponent.h header file.
  • xpidl -m typelib -I_DIR_ IMyComponent.idl will create the IMyComponent.xpt typelib file.

Then follow

Calling the component

There are two ways to call the component:

  • Through an extension
  • Call from JavaScript on a page

It's the same. An extension is a XUL with JavaScript. Please refer to to find out how it works.

The following code on the HTML page calls the component:

<SCRIPT  type="text/javascript">
function MyComponentTestGo() {
    try {"UniversalXPConnect");
        const cid = ";1";
        obj = Components.classes[cid].createInstance();
        obj = obj.QueryInterface(Components.interfaces.IMyComponent);
    } catch (err) {
    var res = obj.Nothing(window);}
<BUTTON ONCLICK="MyComponentTestGo();" id="go">Go</BUTTON>
<input id="test" value="" />


The biggest problem of developing Firefox components is the lack of documents and code. Even XPCOM itself has the same problem. But I think the situation will get better and better. Usually, there are some skilled people on Mozillzine you can ask for help.

I hope this simple article is helpful to those who are frustrated developing for Firefox. If you have any comments or questions, please leave them here. I will reply to you as much as I can.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer Horizon Ideas
United States United States
My name is Jia Chen and I want you tell you about my childhood dream: being a problem-solver. My mom told me it was silly because it wasn't really a profession. Through the last decade, I have been a software engineer, a product manager, a repetitive student, a management consult and an entrepreneur. They appear far from my childhood dream. But I still think I am living it. Because the essence of it is to find problems and solve problems. Some times I may not solve new problems, but I always want to solve old problems in a new way.

You may also be interested in...

Comments and Discussions

GeneralRe: Help with XPCOM Pin
Jia.C10-Oct-07 5:00
memberJia.C10-Oct-07 5:00 
GeneralRe: Help with XPCOM Pin
Anil K P10-Oct-07 18:49
memberAnil K P10-Oct-07 18:49 
GeneralRe: Help with XPCOM [modified] Pin
Anil K P10-Oct-07 23:59
memberAnil K P10-Oct-07 23:59 
GeneralRe: Help with XPCOM Pin
Jia.C11-Oct-07 8:25
memberJia.C11-Oct-07 8:25 
GeneralRe: Help with XPCOM Pin
Anil K P14-Oct-07 21:34
memberAnil K P14-Oct-07 21:34 
GeneralRe: Help with XPCOM Pin
Jia.C15-Oct-07 5:17
memberJia.C15-Oct-07 5:17 
GeneralRe: Help with XPCOM Pin
Anil K P15-Oct-07 18:37
memberAnil K P15-Oct-07 18:37 
GeneralRe: Help with XPCOM Pin
Jia.C15-Oct-07 18:50
memberJia.C15-Oct-07 18:50 
GeneralRe: Help with XPCOM Pin
Anil K P15-Oct-07 19:36
memberAnil K P15-Oct-07 19:36 
GeneralRe: Help with XPCOM Pin
Jia.C15-Oct-07 20:24
memberJia.C15-Oct-07 20:24 
GeneralRe: Help with XPCOM Pin
Anil K P15-Oct-07 21:19
memberAnil K P15-Oct-07 21:19 
GeneralRe: Help with XPCOM Pin
Anil K P18-Oct-07 2:29
memberAnil K P18-Oct-07 2:29 
Generalfatal error C1083: Cannot open include file: 'mozilla-config.h': No such file or directory Pin
michael. zhao3-Oct-07 14:25
membermichael. zhao3-Oct-07 14:25 
GeneralRe: fatal error C1083: Cannot open include file: 'mozilla-config.h': No such file or directory Pin
Jia.C3-Oct-07 14:30
memberJia.C3-Oct-07 14:30 
GeneralIt's really hard... Pin
__Tazz__1-Oct-07 3:31
member__Tazz__1-Oct-07 3:31 
GeneralRe: It's really hard... Pin
Jia.C1-Oct-07 5:13
memberJia.C1-Oct-07 5:13 
GeneralVery good Pin
Silvio Reis Jr1-Oct-07 3:03
memberSilvio Reis Jr1-Oct-07 3:03 
GeneralRe: Very good Pin
Jia.C1-Oct-07 5:10
memberJia.C1-Oct-07 5:10 
GeneralThanks Pin
sameeraperera28-Sep-07 15:27
membersameeraperera28-Sep-07 15:27 
GeneralRe: Thanks Pin
Jia.C28-Sep-07 18:56
memberJia.C28-Sep-07 18:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180323.1 | Last Updated 10 Oct 2007
Article Copyright 2007 by Jia.Chen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid