Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

detecting if a specific tab is open

11 views
Skip to first unread message

bann...@gmail.com

unread,
Oct 7, 2006, 6:18:06 PM10/7/06
to
Hi,

I'm developing my first extension and have gotten pretty far along, but
I've hit a bit of a block though.

My extention runs a timer and opens a new tab at a user defined time.
The tab will then be interacted with and closed through a form, or
closed like a regular window, and I don't want to open another tab
untill the previous tab is closed.

In my onload I've called the class:
this.wm =
Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);

Then in another function I create the tab:

//create the tab, populate it, and bring it to the front
var url = "http://mysite/mypage.php";
this.newTab = getBrowser().addTab(url);
getBrowser().selectedTab = this.newTab;

//detect when the tab window is closed
browserWindow = this.wm.getMostRecentWindow("navigator:browser");
browser = browserWindow.getBrowser();
container = browser.mPanelContainer;
container.addEventListener("DOMNodeRemoved", this.tabCloseHandler,
false);

tabCloseHandler: function(event) {
var yourTarget = event.target;
alert("window closed: ");
}


This script successfully fires the tabCloseHandler when ANY window is
closed. Is there a way I can test if the specific window is closed?
I've tried getting the event.target which returns a chromeWindow
object, but I can't match it to the browserWindow variable.

Any help is appreciated!

Nickolay Ponomarev

unread,
Oct 8, 2006, 6:35:05 PM10/8/06
to bann...@gmail.com, dev-ext...@lists.mozilla.org
On 7 Oct 2006 15:18:06 -0700, bann...@gmail.com <bann...@gmail.com> wrote:
> //detect when the tab window is closed
> browserWindow = this.wm.getMostRecentWindow("navigator:browser");
> browser = browserWindow.getBrowser();
> container = browser.mPanelContainer;
You're missing |var| here.

> container.addEventListener("DOMNodeRemoved", this.tabCloseHandler,
> false);
>
> tabCloseHandler: function(event) {
> var yourTarget = event.target;
> alert("window closed: ");
> }
>
>
> This script successfully fires the tabCloseHandler when ANY window is
> closed. Is there a way I can test if the specific window is closed?

You don't have anything to handle *window* close, do you? I don't
understand the question.

Nickolay

bann...@gmail.com

unread,
Oct 8, 2006, 9:13:25 PM10/8/06
to
Nickolay,

The script works fine without the var, but you are right, I should have
it in there. I do have a function to handle a tab close event or a
DOMNodeRemoved... tabCloseHandler.

The problem is, it fires when any tab is closed. I want to know when
the tab I opened is closed, or if it is still open.

I actually got this script working with the window object. All i needed
for it was:
window.closed

Is there anything similar for tabs, or what I'm i missing about my code
that would check for a specific tab?

Thanks!


> > container = browser.mPanelContainer;
> You're missing |var| here.
>
> > container.addEventListener("DOMNodeRemoved", this.tabCloseHandler,
> > false);
> >

I see. Thanks. The script does work fine without the var though.

> You don't have anything to handle *window* close, do you? I don't
> understand the question.
>
> Nickolay

this.tablCloseHandler is the function called when DOMNodeRemoved

Nickolay Ponomarev

unread,
Oct 9, 2006, 1:52:51 AM10/9/06
to bann...@gmail.com, dev-ext...@lists.mozilla.org
On 8 Oct 2006 18:13:25 -0700, bann...@gmail.com <bann...@gmail.com> wrote:
> > > container = browser.mPanelContainer;
> > You're missing |var| here.
> >
> > > container.addEventListener("DOMNodeRemoved", this.tabCloseHandler,
> > > false);
> > >
>
> I see. Thanks. The script does work fine without the var though.
>
No it doesn't. You get a strict warning and |container| becomes a
global variable.

> > You don't have anything to handle *window* close, do you? I don't
> > understand the question.
> >
> > Nickolay
>
> this.tablCloseHandler is the function called when DOMNodeRemoved
>

So you're asking about getting the *tab* that was closed.

This indeed can be determined by examining event.target:
http://developer.mozilla.org/en/docs/Extension_Code_Snippets:Tabbed_Browser#Notification_when_a_tab_is_added_or_removed_.28.3C.3D_Firefox_1.5.29

Nickolay

bann...@gmail.com

unread,
Oct 9, 2006, 3:19:36 PM10/9/06
to
Thanks Nickolay, that's a great resource. Not sure how I missed it with
all my searches on the topic. I'll give this a go and see if I like it
better than the window object I'm using.

bann...@gmail.com

unread,
Oct 11, 2006, 4:45:21 PM10/11/06
to
Thanks for your help Nickolay but I'm still nowhere on this. When using
DOMNodeRemoved in a listener I can't figure out how to test
event.target to see if it equals the tab i created called this.win

I'll restate my problem: How do I test to see if a tab I created:

this.win = getBrowser().addTab(url);

has been closed or not?


my previous implementation with the window object (window.open()) used
tested win.closed and worked fine, but there seems to be no equivilant
for a new browser tab.

Is there some other way?

Sorry about the confusion, but I can't seem to find docs at XULplanet
or the Moz ext. dev resources that will help.

Nickolay Ponomarev

unread,
Oct 11, 2006, 6:12:42 PM10/11/06
to bann...@gmail.com, dev-ext...@lists.mozilla.org
On 11 Oct 2006 13:45:21 -0700, bann...@gmail.com <bann...@gmail.com> wrote:
> Thanks for your help Nickolay but I'm still nowhere on this. When using
> DOMNodeRemoved in a listener I can't figure out how to test
> event.target to see if it equals the tab i created called this.win
>
> I'll restate my problem: How do I test to see if a tab I created:
>
> this.win = getBrowser().addTab(url);
>
> has been closed or not?
>
>
> my previous implementation with the window object (window.open()) used
> tested win.closed and worked fine, but there seems to be no equivilant
> for a new browser tab.
>
There's no such property on tabs. It makes sense - when the tab is
closed, the related objects should be cleaned up to reclaim memory. If
your extension kept references to all tabs ever opened, it would be a
memory leak.

You can capture the even of tab closing. You can get the tab that is
being closed. This should be enough for you.

I can't be more specific, as I don't know /why/ are you trying to do this.

Nickolay

bann...@gmail.com

unread,
Oct 11, 2006, 9:45:33 PM10/11/06
to

> There's no such property on tabs. It makes sense - when the tab is
> closed, the related objects should be cleaned up to reclaim memory. If
> your extension kept references to all tabs ever opened, it would be a
> memory leak.

That makes sense.

> You can get the tab that is being closed. This should be enough for you.

OK! This is exactly what I need then. I'm successfull capturing the tab
closing event for ALL tabs, BUT I need to figure out if the tab that
was closed is the one I created.

This part works fine... create, add the listener etc...

createTab: function(){
//create tab, bring it into focus
var url = "http://mysite.com/quiz.php";
this.win = getBrowser().addTab(url);
getBrowser().selectedTab = this.win;

//detect when a tab is closed
var wm =
Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var browserWindow = wm.getMostRecentWindow("navigator:browser");
var browser = browserWindow.getBrowser();
var container = browser.mPanelContainer;


container.addEventListener("DOMNodeRemoved", this.tabCloseHandler,
false);

},


tabCloseHandler: function(event) {
var yourTarget = event.target;

alert(" created tab:"+myExten.win +" window closed:"+yourTarget);
//what I need is something like....
if(myExten.win == yourTarget){
alert("my tab was closed !!!!!!!!!!!");
myExten.win = "closed";
}
}

So what I need is the above comment in tabCloseHandler. The created tab
and the event.target both return [object XULElement], but they don't
seem to match up in an comparitive statement. How do I do so?

> I can't be more specific, as I don't know /why/ are you trying to do this.

Basically the purpose is to open a window every X minutes at the
user's preference. The user will interact with the window, and close it
when done. I don't want to open a new window or even run my counter
untill they have finished with the window.

I hope this was specific enough. Thanks for all your help!

bann...@gmail.com

unread,
Oct 13, 2006, 3:58:12 PM10/13/06
to
After running around in circles on this thread, and searching and
trying a million different things I got it going.

I suppose my earlier code was going back and forth between the browser
and various containers, never grabbing the specific document I was
looking for. I still don't have a good grasp on the
browser/tab/document tree, and would love a to find a resource
explaining it.

I'm not sure why it works without calling the class that every other
doc I read required, but here it is...

createTab: function(){
//create tab
var url = "myUrl";


this.win = getBrowser().addTab(url);
getBrowser().selectedTab = this.win;

this.cont = document.getElementById("content");
this.cont.tabContainer.addEventListener("DOMNodeRemoved",


this.tabCloseHandler, false);
},
tabCloseHandler: function(event) {
var yourTarget = event.target;

if(myExten.win == yourTarget){
alert("this.win was closed");
myExten.win = "closed";
}
}

If anyone can find fault with this or point out a better way of
determining when a specific tab is closed I'd love to see it.

Also, I still have to figure out how to remove my listener. I've tried
myExten.cont.tabContainer.addEventListener("DOMNodeRemoved",
this.tabCloseHandler, false); from within the listner but that didn't
work.

Nickolay Ponomarev

unread,
Oct 14, 2006, 6:17:36 AM10/14/06
to bann...@gmail.com, dev-ext...@lists.mozilla.org
On 11 Oct 2006 18:45:33 -0700, bann...@gmail.com <bann...@gmail.com> wrote:
> Basically the purpose is to open a window every X minutes at the
> user's preference. The user will interact with the window, and close it
> when done. I don't want to open a new window or even run my counter
> untill they have finished with the window.
>
So if the user navigates to another site in that tab (via links or
just typing in a new address), you still don't want to open another
tab? This makes little sense honestly.

Nickolay

Nickolay Ponomarev

unread,
Oct 14, 2006, 6:29:43 AM10/14/06
to bann...@gmail.com, dev-ext...@lists.mozilla.org
On 13 Oct 2006 12:58:12 -0700, bann...@gmail.com <bann...@gmail.com> wrote:
> I suppose my earlier code was going back and forth between the browser
> and various containers, never grabbing the specific document I was
> looking for. I still don't have a good grasp on the
> browser/tab/document tree, and would love a to find a resource
> explaining it.
>
Open the DOM Inspector, make sure you have anonymous content displayed
and inspect the browser document's tree a bit.

> I'm not sure why it works without calling the class that every other
> doc I read required, but here it is...
>

What "class"?

> this.win = getBrowser().addTab(url);
> getBrowser().selectedTab = this.win;
>
> this.cont = document.getElementById("content");

this.cont == getBrowser() == gBrowser

I'd just use gBrowser everywhere.

> this.cont.tabContainer.addEventListener("DOMNodeRemoved",
> this.tabCloseHandler, false);

Note that your tabCloseHandler has wrong |this| when invoked.

> myExten.win = "closed";
instead do, |myExten.closed = true; myExten.win = null| to let the tab die.

> If anyone can find fault with this or point out a better way of
> determining when a specific tab is closed I'd love to see it.
>
> Also, I still have to figure out how to remove my listener. I've tried
> myExten.cont.tabContainer.addEventListener("DOMNodeRemoved",
> this.tabCloseHandler, false); from within the listner but that didn't
> work.
>

Obviously using removeEventListener.

Nickolay

bann...@gmail.com

unread,
Oct 16, 2006, 2:41:47 PM10/16/06
to

VERY good point Nickolay, I've added that to the list of things to work
out. :D


> Nickolay

0 new messages