Chrome API:如何使用当前空的“新标签”而不是创建另一个标签

Chrome API: how to use the current empty "New Tab" instead of creating another one?

本文关键字:标签 新标签 API 另一个 创建 何使用 Chrome      更新时间:2023-09-26

>假设您刚刚打开了Chrome。已经打开了一个"新选项卡"。我的扩展有一个按钮,当用户按下它时,我会创建一个新选项卡来显示我的内容(index.html)。

现在一切正常,但问题是"新选项卡"毫无用处。我使用以下调用来创建一个新选项卡:

chrome.tabs.create({
  "url":"index.html"
});

update() 不起作用(什么也没发生)。

chrome.tabs.update({
  "url":"index.html"
});

当我检查当前选项卡时,它说"未定义":

chrome.tabs.getCurrent(function(t){console.log(t.id)})

如何使用当前的空选项卡而不是创建新选项卡?我看到其他一些扩展程序(即谷歌邮件检查器)也有同样的问题。

显然,Tabs API 不允许你省略前导的可选 argunemts,例如 jQuery 这样做。 要更改当前选项卡,您需要显式包含null作为第一个参数 (tabId) 以update()

chrome.tabs.update(null, {
    "url":"index.html"
});