【问题标题】:Migrated manifestV2 to V3 after that chrome.action.onClicked.addListener not working在 chrome.action.onClicked.addListener 不起作用后将 manifestV2 迁移到 V3
【发布时间】: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


【解决方案1】:

(一旦OP向我们提供更多信息,我将编辑此答案)

我创建了一个测试扩展

  • 你的 manifest.json
  • 你的 background.js,用 setDomain({ tab: tabs[0] }); 替换为 console.log("setDomain({ tab: tabs[0] });");
  • 只包含代码console.log("content.js");的content.js

当我单击测试扩展中的操作时,它会执行代码console.log("setDomain({ tab: tabs[0] });");

换句话说:您向我们展示的代码不足以诊断您的问题。

如果您执行以下操作,我们只能为您提供帮助:

请将您的扩展程序的整个源代码上传到 Github 或类似网站。

如果您不想上传扩展程序的整个源代码:

请为您的扩展创建一个简化版本,以重现您的问题。 然后将简化扩展的整个源代码上传到 Github 或类似网站。

【讨论】:

  • 请找到样板源代码链接。当我们单击扩展时,此代码第一次运行良好。在下一次尝试单击时,它不起作用。如果您有解决方案,请告诉我们@Thomas github.com/shaikshahina/chromeextension
猜你喜欢
  • 1970-01-01
  • 2013-11-26
  • 2012-10-04
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
相关资源
最近更新 更多