Каждая вкладка имеет index
, которая показывает ее положение.Например, третья вкладка будет иметь индекс 2 (начинается с 0).
Следовательно, вкладка справа от любой вкладки означает от tab.index +1
до tabs.length
Например,...
Получение вкладок справа от активной вкладки
// get all the tabs in current window
chrome.tabs.query({currentWindow: true}, tabs => {
let activeIndex;
for (const tab of tabs) {
// set the activeIndex so we wont have to run a loop on the tabs twice
if (tab.active) { activeIndex = tab.index; }
// tabs to the right of the active tab will have higher index
if(typeof activeIndex !== 'undefined' && tab.index > activeIndex) {
// tabs is on the right of the active tab ... do whatever needed
}
}
});
Получение вкладок слева от активной вкладки
// get all the tabs in current window
chrome.tabs.query({currentWindow: true}, tabs => {
for (const tab of tabs) {
// stop when reached the active tab
if (tab.active) { break; }
// tabs to the left of the active tab ... do whatever needed
}
});