Pergunta

A scenario where in a background page JS (which is persistent) I keep track of currentSelected TabId. But if

  1. I am on 'window_1' which has 'tab_w_1' selected (i.e. currentTabId = 'tab_w_1' )
  2. Then from page inside 'tab_w_1' I click on a link which opens another window 'window_2'
  3. Which means now 'tab_w_2' from 'window_2' is selected (i.e. currentTabId = 'tab_w_2' )
  4. Now If I just go back to 'window_1' (It will be in exact same state as we left it in i.e. showing 'tab_w_1')
  5. I should be able to find out 'tab_w_1' is selected from window_1
  6. But for tabId current selection still says 'tab_w_2' as selected, Which is wrong :(

I keep track of current selected Tab using chrome.tabs.onUpdated, chrome.tabs.onActivated. But in Step-4 these events do not get invoked.

Is there any way to get current selected tab while switching between multiple windows ?

Foi útil?

Solução

You can detect step 4 with the event chrome.windows.onFocusChanged:

chrome.windows.onFocusChanged.addListener(function(integer windowId) {
    if( windowId !== chrome.windows.WINDOW_ID_NONE )
        chrome.tabs.query({active: true, windowId: windowId}, function( tabs ) {
            currentSelected = tabs[0].id;
        });
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top