chrome extension: Uncaught TypeError: Cannot read properties of undefined (reading 'onClicked')

chrome extension: Uncaught TypeError: Cannot read properties of undefined (reading 'onClicked')

我一直在创建一个 chrome 扩展程序,它应该 运行 在扩展程序单击的特定选项卡上的某个脚本 (index.js)。

service_worker.js

// action on extension click
chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.executeScript({
        tabId: tab.id,
    }, { file: "index.js" });

});

我也试过了

chrome.action......

browser....

但没有任何效果,我正在使用清单 v3。

manifest.json

{
    "name": "Meet scraper",
    "version": "0.1",
    "author": "Naveenkumar M",
    "description": "Scrapes meet data from meetup.com",
    "manifest_version": 3,
    "permissions": [
        "activeTab",
        "tabs"
    ],
    "background": {
        "service_worker": "service_worker.js"
    }
}

我的 index.js 文件是

console.log("Hello world")

我收到错误

如有错误请指正

清单 v2

The following keys must be declared in the manifest to use this API.

browser_action

查看此 link 了解更多详情

https://developer.chrome.com/docs/extensions/reference/browserAction/

更新 1:

清单 v3

您需要在清单文件中添加操作

{
  "action": { … }
}

然后你可以这样称呼它

chrome.action.onClicked.addListener(tab => { … });