简体   繁体   English

使用背景页面和内容脚本的Chrome扩展程序

[英]Chrome extension using a background page + content scripts

I'm having trouble wrapping my head around the structure of Chrome extensions. 我无法绕过Chrome扩展程序的结构。

My extension has two different parts: 我的扩展程序有两个不同的部分:

  1. It uses a background page to log in via oAuth, then collates a lot of data from oAuth and saves it to chrome.storage.local . 它使用后台页面通过oAuth登录,然后从oAuth整理大量数据并将其保存到chrome.storage.local

  2. When browsing webpages, it does a call to chrome.storage.local to check whether the current domain matches info stored from oAuth, and if so, displays a notification using the [Rich Notifications API][1] 浏览网页时,它将调用chrome.storage.local以检查当前域是否与从oAuth存储的信息匹配,如果匹配,则使用[Rich Notifications API][1]显示通知。

The structure of my manifest.json is breaking things. 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"]
    }
  ]
}

When I do this, I get the following error from Chrome: 这样做时,我从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.

Is it possible to do the two processes in tandem? 是否可以同时进行这两个过程? If not, how can I use the background page to check for a page refresh and run a script? 如果没有,如何使用后台页面检查页面刷新并运行脚本?

Your manifest is an app manifest, as helpfully suggested by the error. 您的清单是应用清单,如错误提示所提示。

To fix, remove the app "wrapping", it should be just background.scripts key. 要修复,请删除app “包装”,它应该只是background.scripts键。 Then it will be a valid extension manifest. 然后它将是有效的扩展清单。


Of note: chrome.notifications is not available to content scripts; 注意: chrome.notifications不可用于内容脚本; you'll need to use Messaging to request your background page to show it. 您将需要使用“ 消息传递”来请求背景页面进行显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM