【发布时间】:2022-09-07 18:21:04
【问题描述】:
从清单 v2 迁移到 v3 时面临问题(chrome.action.onClicked.addListener 不工作/调用)。
我有一个这样定义的 manifest.json
{
"name": "dummy",
"manifest_version": 3,
"version": "5.2.0",
"version_name": "5.2.0",
"description": "The dummy v5.2.0 plugin allows our users to gain instant access to
their metadata and data.",
"action": {
"default_title": "execute.js will run (watch the Chrome DevTools' console)"
},
"content_scripts": [
{
"js": ["content.js"],
"matches": [
"https://*/*",
"http://*/*"
]
}
],
"background": {
"service_worker": "background.js"
},
"permissions": [
"contextMenus",
"tabs",
"scripting",
"storage"
],
"host_permissions": [
"https://*/*",
"http://*/*"
],
"web_accessible_resources": [{
"resources": ["*.html"],
"matches": ["https://*/*","http://*/*"]}]}
和background.js 文件有这个代码
chrome.action.onClicked.addListener(function (tab) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
setDomain({ tab: tabs[0] });
});});
我真的迷路了,调试起来非常困难。这段代码在迁移到清单 v3 之前就可以工作。
【问题讨论】:
-
您是否看到错误消息?如果是这样,将其包含在问题中会有所帮助。
-
删除 chrome.tabs.query 行并简单地调用
setDomain({ tab });请注意,Chrome 中存在使 service worker 无法运行的错误:尝试在后台脚本的开头添加self.oninstall=()=>skipWaiting()。
标签: javascript google-chrome google-chrome-extension chromium chrome-extension-manifest-v3