Active tab changes (tabs.onActivated fires). How to get the previous active tab?


#1

Hello,

I am working on a Addon again.
I use tabs.onActivated to monitore if the user changes the active tab.
If so, I want to send a message to the previous active tab.
How do I get the tabID? tabs.onActivated only gives me the ID of the tab that has become active. Do I have to log the previous tabID myself?

Thank you.


(Michel Gutierrez) #2

Since you added a listener on tabs.onActivated, saving the last actived tab id to a addon-global variable is quite easy.


(Baptiste Thémine) #3

Notice that active tab is different than focused tab. Take into consideration that there could be more than one active tab because there could be more than one window. If you want to send a message to the previous focused tab, you will also need to listen to windows.onFocusChanged.


#4

Add listener to tabs.onUpdate.

browser.tabs.onUpdate.AddListener((tabId, info, tabsTab) => {
  if (info.hasOwnProperty("active")) {
    console.log(info.active);
  }
});

#5

Thats a good point. I wanted to prevent to log it myself, because of all the special cases like moving a tab from one window to another. Or missing an event.
But I think I give it a try.


#6

I dont think tabs.onUpdated has advantages over tabs.onActivated in my case.

tabs.onActivated works good:

browser.tabs.onActivated.addListener((activetab) => {
	console.log("Tab " + activetab.tabId + " was activated");
});

PS Its browser.tabs.onUpdated.addListener, but it still dont work.


#8

Sorry for the typo.
Yeah, your right.
active was not included in info.
And sorry for the noise.