为什么 tab.url 来自 "chrome.tabs.onUpdated.addListener()" return 的回调未定义?

Why does tab.url from the callback of "chrome.tabs.onUpdated.addListener()" return undefined?

对于我的 Chrome 扩展,我只需要在某个网站上显示 pageAction。我曾经使用 declarativeContent 执行此操作,但 Firefox 不支持它,因此我必须手动执行此操作。 this question 的答案建议您可以简单地使用这样的东西:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (changeInfo.status === "complete" && tab.url) {
    if (tab.url.match(/google.com/)) {
      chrome.pageAction.show(tabId);
    }
  }
});

这对我不起作用,所以我将后台脚本中的代码修改为如下所示:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  console.log("Tab updated! Tab URL: " + tab.url);
});

每次更新选项卡时,控制台只会打印 Tab updated! Tab URL: undefined 几次。我还尝试像 this question 所说的答案那样查询选项卡,这会产生相同的输出。

扩展的其他文件是:

manifest.json:

{
  "name": "Test",
  "version": "1.0",
  "description": "Test for Whosebug",

  "permissions": ["activeTab"],

  "background": {
    "scripts": ["background.js"]
  },
  "page_action": {
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

popup.html:

<!DOCTYPE html>
<html>
  <body>
    This is a popup!
  </body>
</html>

您缺少 manifest.json 文件的 tabs 权限。有了这个权限,选项卡的 URL 也应该被记录到控制台。

"permissions": ["activeTab", "tabs"] // <-- This

manifest.json:

{
  "name": "Test",
  "version": "1.0",
  "description": "Test for Whosebug",

  "permissions": ["activeTab", "tabs"],

  "background": {
    "scripts": ["background.js"]
  },
  "page_action": {
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}