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

标签 javascript google-chrome google-chrome-extension oauth

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

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

  1. 它使用后台页面通过oAuth登录,然后整理来自oAuth的大量数据并将其保存到chrome.storage.local

  2. 浏览网页时,它会调用 chrome.storage.local 来检查当前域是否与 oAuth 存储的信息匹配,如果匹配,则使用 显示通知>[丰富通知 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.

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

最佳答案

您的 list 是一个应用程序 list ,正如错误所提示的那样。

要修复此问题,请删除 app “包装”,它应该只是 background.scripts 键。那么它将是一个有效的扩展 list 。

<小时/>

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

关于javascript - 使用后台页面 + 内容脚本的 Chrome 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28657752/

相关文章:

javascript - 在 chrome 扩展程序中显示通知

javascript - Chrome 扩展程序向页面上的每个图像添加按钮

Javascript( typescript )Chrome 扩展,函数回调如 promise ?

javascript - 表单提交 404 未找到错误

第一页加载时或重新加载一定次数后的 JavaScript,无法用 "clear caches"重现

javascript - 使用javascript错误在另一个div中显示菜单

javascript - Chrome 浏览器 : not set cookie from response header

javascript - Window.open 不适用于 Google Chrome 中的多个链接

javascript - 获取多个 touchend 事件 javascript

javascript - 如何创建简单的 javascript/jquery 客户端验证码?