chrome extension background.js事件不运行

jk9hmnmh  于 2022-12-06  发布在  Go
关注(0)|答案(1)|浏览(214)

我正在尝试创建一个基本的Chrome扩展,当用户点击扩展时,它会打开一个新的标签页。它似乎不起作用。当我打开扩展(点击它)时,什么都没有发生(新的标签页或窗口没有打开)。
1.)我通过命令yarn create vite chrome-sf-poc --template react创建了一个新项目
2.)这是用于生成包yarn build && cp src/manifest.json dist/ && public/background.js dist/的脚本

清单.json

{
  "manifest_version": 3,
  "name": "DEV Articles",
  "description": "A quick way to browse top posts from DEV Community.",
  "version": "0.0.1",
  "action": {
    "default_popup": "index.html"
  },
  "background": {
    "service_worker": "background.js"
  },
  "permissions": ["storage", "activeTab", "scripting", "tabs", "runtime"]
}

背景.js

chrome.browserAction.onClicked.addListener(function (tab) {
  chrome.tabs.create({
    url: chrome.extension.getURL("https://www.google.com/"),
    selected: true,
  });

  chrome.tabs.create({
    url: chrome.extension.getURL("https://www.google.com/"),
    selected: true,
  });
});

chrome.runtime.onStartup.addListener(function () {
  chrome.tabs.create({
    url: chrome.extension.getURL("https://www.google.com/"),
    selected: true,
  });

  chrome.tabs.create({
    url: chrome.extension.getURL("https://www.google.com/"),
    selected: true,
  });
});

当我将扩展加载到chrome中时,它显示service worker处于非活动状态:

hm2xizp9

hm2xizp91#

我认为脚本输出V2。您尝试将其迁移到V3,但失败了。我修复了它。
manifest.json

{
  "manifest_version": 3,
  "name": "DEV Articles",
  "description": "A quick way to browse top posts from DEV Community.",
  "version": "0.0.1",
  "action": {
    "default_title": "hoge"
  },
  "background": {
    "service_worker": "background.js"
  }
}

background.js

chrome.action.onClicked.addListener(function (tab) {
  chrome.tabs.create({ url: "https://www.google.com/" });
});

chrome.runtime.onStartup.addListener(function () {
  chrome.tabs.create({ url: "https://www.google.com/" });
});

相关问题