2

I was thinking of using the tabid to do this, but it seems to change quite often as I am browsing around within the tab. The API reference notes that the tabid is unique within a "browser session". I took this to mean within the lifetime of the browser process, but it seems more likely now that uniqueness is only guaranteed during a session with a specific website. Is there another way I could uniquely identify a tab for its entire life?

Rahul Gupta-Iwasaki
  • 1,683
  • 2
  • 21
  • 39
  • The question was asked before, but no one came up with an answer yet: http://stackoverflow.com/questions/12658370/re-establishing-tab-identity-in-a-chrome-extension-after-browser-restart – Konrad Dzwinel Dec 19 '12 at 10:22
  • @KonradDzwinel, the question you linked is not the same as OP's one. This question is about tabID identity within a single borwser session, while the linked one is about preserving identity between sessions. So for this current question, in most cases the handle of `Chrome_RenderWidgetHostHWND` remains the same during navigation even after jumping between different sites. So it's possible to make a NPAPI plugin which tracks tabs by this handle. – Stan Dec 19 '12 at 11:03

1 Answers1

1

Due to prerendering and other factors, tabs can be 'replaced'. This means that tabId can change even if you keep the tab open. chrome.webNavigation.onTabReplaced can be used to capture such events. See https://developer.chrome.com/extensions/webNavigation.html#event-onTabReplaced for more information.

To demonstrate this, following these steps may help:

  1. Make sure that you've requested "tabs" and "webNavigation" request for your extension and your extension doesn't use a non-persistent background page (without background.persistent = true in manifest.json). Also make sure Google is your default search engine and Instant Search is enabled.
  2. Open some website (not Google search).
  3. Execute chrome.tabs.query({}, function(tabs){console.log(tabs)}) in the developer tools of your extension's background page. Look at existing tabs and their ids.
  4. Execute chrome.webNavigation.onTabReplaced.addListener(function(details){console.log(details); in the DevTools.
  5. Type something in the omnibox of that tab to trigger Google Instant and click on the body of the page. You should see the onTabReplaced event fired and the old and new tab ids in the console of the DevTools.
  6. Execute chrome.tabs.query({}, function(tabs){console.log(tabs)}) again and you'll find that tab id actually 'changed'.
方 觉
  • 4,042
  • 1
  • 24
  • 28
  • The Tab ID can be treated as constant for a tab in practical cases. The temporary IDs you're talking about are not exposed to the `chrome.tabs` API - see https://developer.chrome.com/extensions/webNavigation.html#tab_ids. – Rob W Dec 19 '12 at 10:31
  • @RobW "Once such a tab is swapped in, an onTabReplaced event is fired and they become accessible via these APIs." To my understanding, this might make an existing tab 'change' its tab id. However, I couldn't reproduce an instance of the event at the moment. – 方 觉 Dec 19 '12 at 13:52