6

My Chrome extension saves some data in localStorage, and it needs to do this on a per-tab basis. Using the tab id I get from the API allows me to save the data in a way I need. Unfortunately, if the browser is restarted, tabs seem to get different ids, and my scheme falls apart. Is there a way to identify tabs in a way that survives restarts? The URL is not sufficient, as the same URL can appear in different tabs and should not cause those tabs to be confused.

What's the right pattern for this?

Gene Golovchinsky
  • 6,101
  • 7
  • 53
  • 81
  • What makes these tabs distinct? Would it be possible to make a page recognizable by some content of the page, which you could read out by your content script? There must be at least one thing to do this, otherwise there's no way to get the old order back. – dan-lee Sep 30 '12 at 03:31
  • What distinguishes the tabs is the browsing history within each tab. I suppose that I can enumerate the history within each tab, concatenate all the URLS, hash that, as use the hash value as the tab identity. I would need to do that on every new link that was followed. – Gene Golovchinsky Sep 30 '12 at 03:39
  • Looking at the history API, however, I am not seeing a way to associate it with tabs. – Gene Golovchinsky Sep 30 '12 at 03:42
  • 3
    That's a very interesting topic. I found a related issue to this, maybe it helps: [**Persistent unique ID for Chrome tabs that lasts between browser sessions**](http://stackoverflow.com/questions/11005258/persistent-unique-id-for-chrome-tabs-that-lasts-between-browser-sessions). Seems like you have to track every action to guarantee persistance. – dan-lee Sep 30 '12 at 03:49
  • Thanks @Dan -- it is indeed a very similar request. I think for my purposes, a way to enumerate the last few urls in a tab's history might work fine, but I don't see how to do that from the Chrome API – Gene Golovchinsky Sep 30 '12 at 03:59
  • You should be able to track these changes with [`chrome.tabs.onUpdated()`](http://developer.chrome.com/extensions/tabs.html#event-onUpdated) – dan-lee Sep 30 '12 at 04:06
  • The documentation seems silent on whether this handler is called at the time the browser is started. Without that, I am not sure how I would get at a tab's history after restart. – Gene Golovchinsky Sep 30 '12 at 05:02
  • This might help you: http://stackoverflow.com/questions/11005258/persistent-unique-id-for-chrome-tabs-that-lasts-between-browser-sessions – AntonNiklasson Oct 26 '12 at 09:22
  • That's the same question that @DanLee refers to, I think. – Gene Golovchinsky Oct 29 '12 at 23:12
  • Since the question is a duplicate I didn't want to re-answer it here, but I've added a [new answer](http://stackoverflow.com/a/14518800/1017611) on the question linked by @Dan which might provide some more detail. – drzax Jan 25 '13 at 09:32
  • This probably won't work but have you tried adding the `chrome.tabs.onUpdated.addListener`? See if on restart it fires this and tells you the new tabid. – Don Rhummy Jun 08 '13 at 16:26
  • Not sure how I would know which tab it is. – Gene Golovchinsky Jun 10 '13 at 05:03

1 Answers1

0

I had big problems with this too. Finally I resolved it by setting a listener for Chrome creating a new tab. See the code below:

chrome.tabs.onCreated.addListener(function(tab) {
    //Callback function gives you the tab that was created.
    var tabId = tab.id;
    //Or, in my case I just added it here to an array of tab ids.
});

I personally kept an array of tab ids that I needed and added and removed them as needed. See this documentation for help with everything to do with tabs.

John Woodruff
  • 1,608
  • 16
  • 29