How are Tab IDs generate in Chrome?

Are they created incrementally? Basically they are integers, but I'm wondering if they are generated in a particular way. Knowing this would be useful for me for an application that I am writing.

EDIT: I need to keep in memory the IDs and I am thinking if I can order them to be able to do a search. IF they are created with an increasing number is easy to implement a search as a binary search, that's why I am asking

Answers


The only thing I can find in the documentation pertaining to the ID is the following Excerpt:

id ( integer ) The ID of the tab. Tab IDs are unique within a browser session.

So what we can glean from this is that no two tabs will ever get the same ID in the same session. There's no information in the docs about how they're generated.

Ultimately I think any approach that is predictive in this sense is doomed to bugs & failure, you should be building a responsive application vs. a predictive one.

Instead of assuming the next tab will be opened with ID # 12, Capture the creation of the tab, then you'll know the ID and can perform operations based on that.

What you need is the tab onCreated event. When a tab is created you'll get a tab Object which you can get the tabid and windowid from and perform whatever actions you desire.

Since you didn't tell us much about your ultimate goal, I can't give you any specific examples / code to help you out other than pointing you in the right direction.


Continuation from comments:

If you're just looking to look up the tab by ID wouldn't it be easier to store your tab meta-data in a dictionary instead of a list?

var metadataRepository = {};
metadataRepository[tab.id] = tab.metadata;

Then:

var metadata = metadataRepository[tabId];
if (metadata) {
   // Do Stuff
}

Even More Information:

As of my current Chrome (19.xxx...) the V8 Engine Supports the JavaScript 1.6 method of Array.filter(...)

I did a little test here that simulates searching an array using filter and doing a type inspection & string search each iteration (something common).

It took 25ms on my machine (relatively new, i7-2600k) to blast through 10,000 entries. While this isn't fast compared to compiled languages, it's going to be sub millisecond when dealing with 100ish tabs. I dont think you need to worry about performance too much.

IF your app is constantly looking this up, consider a local cache or the dictionary mentioned above.


To track all tabs and solve your problem, create an array in your extension context.

window.CurrentTabs = {};

Then, simply hook up removals and additions, in order to track all active tabs.

chrome.tabs.onCreated.addListener(function(tab) { window.CurrentTabs[tab.id]=tab  });
chrome.tabs.onRemoved.addListener(function(tabID){ delete window.CurrentTabs[tabID]; });

Some mentions:

  1. you also need to run a one-time chrome.tabs.query(...) when your extension starts, because there are several reasons for which tabs can exists prior to your extension's activation point

  2. you also need to research other scenarios, such as tabs being dragged across windows and that kind of stuff (I have no idea what happens really, but there are event handlers)

  3. tab IDs, yes, are incremental, however they're not "perfectly" incremental. You might call chrome.tabs.create() and wind up having tab ids 51 and 58 with no valid tabs in the 52-57 range. That's Chrome behavior.

  4. for your search function, simply use this

    chrome.tabs.query({ /* EMPTY SEARCH PARAMETERS */ }, function(tabs){ /* DO WHAT YOU WANT HERE */ })
    

Need Your Help

Returning a pointer

c++ pointers

I don't understand how this example can possibly work:

Add line break to ::after or ::before pseudo-element content

css

I do not have access to the HTML or PHP for a page and can only edit via CSS. I've been doing modifications on a site and adding text via the ::after or ::before pseudo-elements and have found that