Chrome 扩展 action.onClicked

Chrome extension action.onClicked

我正在构建一个 chrome 扩展,我使用此代码在新的 window 中打开弹出窗口。 (使用manifest v3)今天突然不行了

Uncaught TypeError: 无法读取未定义的属性(读取 'onClicked') 昨天它工作得很好并且与 chrome 扩展文档相对应。有人知道问题出在哪里吗?

这是我的代码:

chrome.action.onClicked.addListener(tab => {
    chrome.windows.create({
        url: chrome.runtime.getURL("popup.html"),
        type: "popup",
        height: 800,
        width: 516
      }, function(win) {
      });
});

谢谢@woxxom。我已从清单中删除 action。没有意识到 chrome.action 需要它才能工作。固定。

这个问题已经回答了,但我想 post 一个完整的例子,因为我找不到 post 同时具有扩展代码和相应的 manifest.json。

这是使用 Manifest V3 的完整有效示例。

// File: manifest.json //

{
    "manifest_version": 3,
    "name": "My extension",
    "description": "Extension that extends things",
    "author": "Me",
    "version": "0.1",
    "content_scripts": [
      {
        "matches": [
          "https://*"
        ],
        "js": ["content.js"]
      }
    ],
    "icons": { "48":  "icons/icon128.jpg",
              "128":  "icons/icon512.jpg"},
    "background": {
      "service_worker": "background.js"
    },
    "action": {},
    "permissions": [
      "storage",
      "downloads",
      "tabs"
  ]
}
// File: background.js //

// Called when the user clicks on the browser action.
chrome.action.onClicked.addListener(tab => {
    // Send a message to the active tab
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      var activeTab = tabs[0];
      chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
    });
  });

(显然在复制之前从 .json 文件中删除注释,并适当调整您的扩展程序的权限。)

请注意,如果您使用的 Chrome 版本低于 Chrome 93,则您的 manifest.json 和 background.js 必须位于同一目录中。