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!
> 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
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
> > 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
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.
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
> 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!
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
> 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
VERY good point Nickolay, I've added that to the list of things to work
out. :D
> Nickolay