Discussion:
Tab & click event registration
Vaibhav
2010-04-23 06:32:06 UTC
Permalink
Hi,

I am writing Firefox extension using C++.
I am confused about where do I register Tab & Click events in C++.
After reading Mozilla's tabbed browser document, I do it in javascript
file.

It is as follows:
/////////////////////////////////////////////////////////////////////////// ////
function Init()
{
var container;
container = gBrowser.tabContainer;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
const cid = '@MyXPCOM/TestComponent';
var obj =
Components.classes[cid].getService(Components.interfaces.iTestComponent);
container.addEventListener("TabOpen", obj, false);
container.addEventListener("TabMove", obj, false);
container.addEventListener("TabClose", obj, false);
container.addEventListener("TabSelect", obj, false);
window.addEventListener("click", obj, false);
}

function DeInit()
{
// Actually I want to unregister Tab & Click events, but till
this
function gets called
// component instance is no more.
}

window.addEventListener('load', Init, false);
window.addEventListener('unload', DeInit, false);
///////////////////////////////////////////////////////////////

But I want to do above things in C++.

Vaibhav.
Jens Sorensen
2010-04-23 20:01:33 UTC
Permalink
I assume you have implemented at “nsIObserver“ and that the “Observe”
method gets invoked at startup with “xpcom-startup” as the topic. This
would be a good point to get a hold of the “nsIObserverService”
(do_GetService(NS_OBSERVERSERVICE_CONTRACTID)) which allows you to add
an observer which observes when a “domwindowopened” event occours.

Example:
observer = do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
observer->AddObserver(this, NS_LITERAL_STRING("domwindowopened"), PR_FALSE);
observer->AddObserver(this, NS_LITERAL_STRING("domwindowclosed"), PR_FALSE);

This will allow you to get notified when a window is being opened
(browser, help->about, tools->options etc). You can add a onload event
handler to the given window and when the 'onload' event comes in then
get the document and check if its a browser window. The browser window
always has the id of “main-window” (you can verify this by using DOM
Inpector).

Now that you have a reference to the browser window (nsiDOMWindow)
then get the nsIDOMEventTarget interface and add the event TabXXX
event handlers for the given window

nsCOMPtr<nsIDOMEventTarget> domTargetEvent = do_QueryInterface(
browserDOMWindow);
domTargetEvent->AddEventListener(NS_LITERAL_STRIN(“TabOpen”, this, PR_TRUE);

Since I pass in the “this” pointer then your class should implement
then “nsIDOMEventListener” interface.

Thanks,
Jens Sorensen
Post by Vaibhav
Hi,
I am writing Firefox extension using C++.
I am confused about where do I register Tab & Click events in C++.
After reading Mozilla's tabbed browser document, I do it in javascript
file.
/////////////////////////////////////////////////////////////////////////// ////
function Init()
{
       var container;
       container = gBrowser.tabContainer;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
       var obj =
Components.classes[cid].getService(Components.interfaces.iTestComponent);
       container.addEventListener("TabOpen", obj, false);
       container.addEventListener("TabMove", obj, false);
       container.addEventListener("TabClose", obj, false);
       container.addEventListener("TabSelect", obj, false);
       window.addEventListener("click", obj, false);
}
function DeInit()
{
   // Actually I want to unregister Tab & Click events, but till
this
function gets called
   // component instance is no more.
}
window.addEventListener('load', Init, false);
window.addEventListener('unload', DeInit, false);
///////////////////////////////////////////////////////////////
But I want to do above things in C++.
Vaibhav.
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
Vaibhav
2010-04-27 03:50:00 UTC
Permalink
Post by Jens Sorensen
I assume you have implemented at “nsIObserver“ and that the “Observe”
method gets invoked at startup with “xpcom-startup” as the topic. This
would be a good point to get a hold of the “nsIObserverService”
(do_GetService(NS_OBSERVERSERVICE_CONTRACTID)) which allows you to add
an observer which observes when a “domwindowopened” event occours.
observer = do_GetService(NS_OBSERVERSERVICE_CONTRACTID);
observer->AddObserver(this, NS_LITERAL_STRING("domwindowopened"), PR_FALSE);
observer->AddObserver(this, NS_LITERAL_STRING("domwindowclosed"), PR_FALSE);
This will allow you to get notified when a window is being opened
(browser, help->about, tools->options etc). You can add a onload event
handler to the given window and when the 'onload' event comes in then
get the document and check if its a browser window. The browser window
always has the id of “main-window” (you can verify this by using DOM
Inpector).
Now that you have a reference to the browser window (nsiDOMWindow)
then get the nsIDOMEventTarget interface and add the event TabXXX
event handlers for the given window
nsCOMPtr<nsIDOMEventTarget> domTargetEvent = do_QueryInterface(
browserDOMWindow);
domTargetEvent->AddEventListener(NS_LITERAL_STRIN(“TabOpen”, this, PR_TRUE);
Since I pass in the “this” pointer then your class should implement
then “nsIDOMEventListener” interface.
Thanks,
Jens Sorensen
Post by Vaibhav
Hi,
I am writing Firefox extension using C++.
I am confused about where do I register Tab & Click events in C++.
After reading Mozilla's tabbed browser document, I do it in javascript
file.
/////////////////////////////////////////////////////////////////////////// ////
function Init()
{
       var container;
       container = gBrowser.tabContainer;
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
       var obj =
Components.classes[cid].getService(Components.interfaces.iTestComponent);
       container.addEventListener("TabOpen", obj, false);
       container.addEventListener("TabMove", obj, false);
       container.addEventListener("TabClose", obj, false);
       container.addEventListener("TabSelect", obj, false);
       window.addEventListener("click", obj, false);
}
function DeInit()
{
   // Actually I want to unregister Tab & Click events, but till
this
function gets called
   // component instance is no more.
}
window.addEventListener('load', Init, false);
window.addEventListener('unload', DeInit, false);
///////////////////////////////////////////////////////////////
But I want to do above things in C++.
Vaibhav.
_______________________________________________
dev-tech-dom mailing list
https://lists.mozilla.org/listinfo/dev-tech-dom
Hello Jens,

Thank u very much for providing answer.

Vaibhav.

Continue reading on narkive:
Loading...