chrome.tabs.onUpdated返回一个不存在的标签ID?

时间:2015-02-17 00:56:11

标签: javascript google-chrome google-chrome-extension

所以我在跟踪标签时会更新,并将每个标签存储在一个数组中。然而,有时候,有时只会传递一个不存在的id,而我无法理解我的生活。这是一个已知的铬虫还是我错过了什么?非常感谢!

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,currentTab){
    //Find the containing window in the array of windows
    var containingWindow = windows.filter(function(currentWindow){
        return currentWindow.id===currentTab.windowId;
    });
    if (containingWindow.length===1){
        containingWindow = containingWindow[0];
        //Find the containing tab (by id) in our list of tabs.
        var containingTab = containingWindow.tabs.filter(function(currentTab){
            return currentTab.id===tabId;
        });
        if (containingTab.length===1){
            console.log("It worked!");
        }
        else{
            //At this point, there's no reason for it NOT to work, but sometimes an id gets thrown that doesn't exist.
            debugger;
            throw "Argh the bug is still there matey! "+containingTab.length+" "+containingTab+" "+tabId;;
        }
    }

});

1 个答案:

答案 0 :(得分:0)

嗯,您的代码无法正常工作,因为您在创建时存储在阵列中的Window对象不是这些窗口的实时版本,而是快照。他们的标签列表没有更新。

如果您需要有关包含标签的窗口的更新信息(出于原因),您应该致电chrome.windows.get

chrome.windows.get(
  currentTab.windowId,
  {populate: true},
  function(window) {
    /* ... */
  }
);