AddEventListener unload to tab

597 views
Skip to first unread message

daPhyre

unread,
Aug 31, 2011, 5:48:41 PM8/31/11
to Chromium-extensions
What I'm trying to do is to update the info of a badge when the tab
created by it is unloaded (close). Until now, I'be been unsuccessful
to assign such kind of EventListener to that tab.

Which is the best way to do so?

Boris Smus

unread,
Aug 31, 2011, 7:42:58 PM8/31/11
to daPhyre, Chromium-extensions
Hi,

Does the http://code.google.com/chrome/extensions/tabs.html#event-onRemoved event not fire as expected? You should be able to update the browserAction badge (assuming that's what you're updating) in that callback.

- Boris


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


daPhyre

unread,
Sep 2, 2011, 12:12:41 AM9/2/11
to Chromium-extensions
chrome.tabs.onRemoved is assigned to all tabs in the browser. How can
I know if the closed tab is the one I opened with the badge?

On 31 ago, 18:42, Boris Smus <s...@chromium.org> wrote:
> Hi,
>
> Does thehttp://code.google.com/chrome/extensions/tabs.html#event-onRemovedevent
> not fire as expected? You should be able to update the browserAction
> badge (assuming that's what you're updating) in that callback.
>
> - Boris
>
>
>
>
>
>
>
> On Wed, Aug 31, 2011 at 2:48 PM, daPhyre <daphyr...@gmail.com> wrote:
> > What I'm trying to do is to update the info of a badge when the tab
> > created by it is unloaded (close). Until now, I'be been unsuccessful
> > to assign such kind of EventListener to that tab.
>
> > Which is the best way to do so?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

timo

unread,
Sep 2, 2011, 2:51:37 PM9/2/11
to Chromium-extensions
Hi there,

I had this problem as well,... I did a lot of research back then and
remeber that someone of the chrome developers wrote that a popup would
barely fires a onunload event... I guess it's the same with tabs!? I
tryed then window.onblur and it worked.
It's not exactly what you need, but it might help you out and be a
workaround if you do some extra things... (whatever ;o)

Good luck and cheers

timo

unread,
Sep 2, 2011, 3:05:46 PM9/2/11
to Chromium-extensions
Me again,

by the way, to your question: 'How can I know if the closed tab is the
one I opened with the badge?'

If you open a tab you probably use chrome.tabs.create and ... probably
didn't see all the features this method delivers:
chrome.tabs.create(object createProperties, function callback)

It's about the callback function: function(Tab tab) {...};
If you also define this function it deleivers you details about the
created tab. It also contains the ID of the new tab. So you could go
like:
function(tab) {
myOpenedTab = tab.id;
}
...inside your chrome.tabs.create methode and you're done...

So, see: http://code.google.com/chrome/extensions/tabs.html#method-create
for creating tabs and: http://code.google.com/chrome/extensions/tabs.html#type-Tab
for what Tab can deliver.

Cheers

On Aug 31, 11:48 pm, daPhyre <daphyr...@gmail.com> wrote:

daPhyre

unread,
Sep 4, 2011, 12:15:20 AM9/4/11
to Chromium-extensions
So simple, yet so complex... I finally resolved my problem with this
little function:

function tabCallback(tab){
myId=tab.id;
chrome.tabs.onRemoved.addListener( function(tabId) { if (myId==tabId)
doStuff(); } );
}

Thank you both! You made my path clear with your comments!!

timo

unread,
Sep 5, 2011, 6:56:41 AM9/5/11
to Chromium-extensions
Excelent ;o)

One more tip:
with chrome.tabs.onRemoved.addListener( function(tabId) ...
you make a closure anyhow and in the scope of the function
tabCallback's you have 2 variables... tab and myId. So you actualy
don't need myId.
The statement if(myId==tabId) also works as if(tab.id==tabId) because
you new function also sees tab...

I hope I'm not too confusing here ;o)
Reply all
Reply to author
Forward
0 new messages