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

标签 javascript google-chrome-extension

假设您刚刚打开 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)})

如何使用当前的空选项卡而不是创建新选项卡?我发现其他一些扩展(即 Google Mail Checker )也有同样的问题。

最佳答案

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

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

关于javascript - Chrome API : how to use the current empty "New Tab" instead of creating another one?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10075667/

相关文章:

javascript - 垂直内联的 Div 框

javascript - 无法使用 Fabric JS 取消分组

google-chrome-extension - 如何为 chrome 扩展正确写入新的 "event page"?

Javascript chrome 扩展点击页面上的所有元素只更新一半

javascript - 找不到语法错误,帮我看看

javascript - 在图表js中创建带有标签的饼图

javascript - 如何从特定位置读取 JSON

javascript - Chrome 扩展 : Run Content Script before any Embedded Scripts run on Page

google-chrome - 如何从 background.html 以编程方式打开 chrome 扩展弹出窗口

google-chrome-extension - 在Chrome中调试用户脚本的方法