It looks like you are creating a “baby” tab, in which case you should set both index and openerTabId :
function addChildTab(url, parentTab) { chrome.tabs.create({ 'url': url, 'windowId': parentTab.windowId, 'index': parentTab.index + 1,
Setting openerTabId means that the new tab will be correctly associated as a child tab of the parent tab, therefore:
- If you close the child tab while it is active, the parent tab will become the active tab (and not, say, the tab to the right of the child tab). This makes it behave the same way as links that the user opens on new tabs.
- Extensions that display tabs in the tree will work correctly .
See also https://code.google.com/p/chromium/issues/detail?id=67539 who added this.
Note: if you open a tab in the background (passing active:false ), then parentTab.index + 1 not entirely correct, and instead you should insert a new tab after the existing child (and grandson) tab of parentTab :
function addBackgroundChildTab(url, parentTab) { chrome.tabs.query({'windowId': parentTab.windowId}, function(tabs) { var parentAndDescendentIds = {}; parentAndDescendentIds[parentTab.id] = true; var nextIndex = parentTab.index + 1; while (nextIndex < tabs.length) { var tab = tabs[nextIndex]; if (tab.openerTabId in parentAndDescendentIds) { parentAndDescendentIds[tab.id] = true; nextIndex++; } else { break; } } chrome.tabs.create({ 'url': url, 'active': false, 'windowId': parentTab.windowId, 'index': nextIndex, 'openerTabId': parentTab.id }); }); }
But this may be redundant for your purposes, in which case sticking to parentTab.index + 1 , as in my first code example, should be fine.
source share