Chrome 使用后台页面 + 内容脚本的扩展

Chrome extension using a background page + content scripts

我无法理解 Chrome 扩展的结构。

我的扩展有两个不同的部分:

  1. 它使用后台页面通过oAuth登录,然后从oAuth中整理了很多数据并保存到chrome.storage.local

  2. 浏览网页时,它会调用 chrome.storage.local 以检查当前域是否与从 oAuth 存储的信息匹配,如果匹配,则使用 [Rich Notifications API][1] 显示通知

我的 manifest.json 的结构坏了。

{
  "name": "API Test",
  "version": "3.1.2",
  "manifest_version": 2,
  "minimum_chrome_version": "29",
  "app": {
    "background": {
      "scripts": ["main.js"]
    }
  },
  "permissions": ["identity", "storage", "*://*/*"],
  "oauth2": {
    "client_id": "<<client_id>>",
    "scopes": [
      "https://www.googleapis.com/auth/plus.login",
      "https://www.google.com/m8/feeds",
      "https://www.googleapis.com/auth/contacts.readonly"
    ]
  },
  "content_scripts": [
    {
      "matches": ["*://*/*"],
      "js": ["domchecker.js"]
    }
  ]
}

当我这样做时,Chrome 出现以下错误:

There were warnings when trying to install this extension:
'content_scripts' is only allowed for extensions and 
legacy packaged apps, but this is a packaged app.

是否可以同时进行这两个过程?如果没有,我如何使用后台页面来检查页面刷新和 运行 脚本?

您的清单是一个 app 清单,正如错误所提示的那样。

要修复,删除 app "wrapping",它应该只是 background.scripts 键。那么它将是一个有效的扩展清单。


注意:chrome.notifications 不适用于内容脚本;您需要使用 Messaging 来请求您的背景页显示它。