JVG
JVG

Reputation: 21170

Chrome extension using a background page + content scripts

I'm having trouble wrapping my head around the structure of Chrome extensions.

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.

  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]

The structure of my manifest.json is breaking things.

{
  "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:

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?

Upvotes: 0

Views: 1979

Answers (1)

Xan
Xan

Reputation: 77571

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. Then it will be a valid extension manifest.


Of note: chrome.notifications is not available to content scripts; you'll need to use Messaging to request your background page to show it.

Upvotes: 5

Related Questions