Google chrome extension Chrome扩展:选择下一个选项卡?

Google chrome extension Chrome扩展:选择下一个选项卡?,google-chrome-extension,tabs,Google Chrome Extension,Tabs,Chrome extension:如何激活当前标签旁边的标签(即右边的标签)。这是我的代码,但它不能正常工作-而不是下一个标签,一个随机的似乎得到激活 特别是,我是否正确地假设选项卡的index属性指示其在当前页面中从左到右的索引 // switch to next tab function nextTab() { // first, get currently active tab chrome.tabs.query({active: true}, function(tabs) {

Chrome extension:如何激活当前标签旁边的标签(即右边的标签)。这是我的代码,但它不能正常工作-而不是下一个标签,一个随机的似乎得到激活

特别是,我是否正确地假设选项卡的index属性指示其在当前页面中从左到右的索引

// switch to next tab
function nextTab() {
  // first, get currently active tab
  chrome.tabs.query({active: true}, function(tabs) {
    if (tabs.length) {
      var activeTab = tabs[0],
      tabId = activeTab.id,
      currentIndex = activeTab.index;
      // next, get number of tabs in the window, in order to allow cyclic next
      chrome.tabs.query({currentWindow: true}, function (tabs) {
        var numTabs = tabs.length;
        // finally, get the index of the tab to activate and activate it
        chrome.tabs.query({index: (currentIndex+1) % numTabs}, function(tabs){
          if (tabs.length) {
            var tabToActivate = tabs[0],
            tabToActivate_Id = tabToActivate.id;
            chrome.tabs.update(tabToActivate_Id, {active: true});
          }
        });
      });
    }
  });
}
编辑:

问题似乎是查询
chrome.tabs.query({active:true},function(tabs){…})
似乎返回了多个选项卡。 “我的窗口”当前有14个选项卡,其中7个选项卡的
active
属性为true。这是怎么回事?我还尝试基于
{selected:true}
进行查询,但给出的错误是:
参数1的值无效。属性“已选定”:意外属性。


任何帮助都将不胜感激

您似乎打开了多个Chrome实例。
要将选项卡查询的上下文保留到当前实例,您应该将
currentWindow:true
添加到所做的每个查询中。否则,它将与所有其他实例中的所有选项卡ID发生冲突

例如,您的第一个查询如下所示:

chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { // ...

此代码适用于甲烷罐,用于尝试。已编辑要更新的帖子。是否打开了多个实例?如果是这样,请尝试将
currentWindow:true
添加到您的第一个查询对象:
chrome.tabs.query({active:true,currentWindow:true}
是的,我想应该是这样!将检查并确认。是的,它现在已修复,谢谢!