chrome.tabs.highlight 给出错误 "No tab at index"

chrome.tabs.highlight giving error "No tab at index"

调用 chrome.tabs.highlight({'tabs': tabId}, function(){}); 时出现此错误:

Unchecked runtime.lastError while running tabs.highlight: No tab at index: 7355.

此函数不采用选项卡 ID,而是采用选项卡索引(window 中的位置)

chrome.tabs.highlight 需要选项卡索引,而不是 tabId。您可以使用 chrome.tabs.get:

将 tabId 转换为索引
chrome.tabs.get(tabId, function(tab) {
  chrome.tabs.highlight({'tabs': tab.index}, function() {});
});

另一个重要的事情(除了使用标签索引)是提供windowId。这在官方 chrome 文档中没有记录,但如果不同的 window 或检查器处于活动状态,则会有所帮助。

chrome.tabs.highlight({
  windowId: tab.windowId,
  tabs: tab.index
}, function () {});