69

I'm trying my hands at a simple Chrome Extension, but am running into a problem with providing a value for the matches array in my content_scripts.

{
  "name": "My Extension",
  "version": "1.0",
  "description": "My Extension Experiment",
  "browser_action": {
    "default_icon": "icon.png",
    "default_title": "Ext",
    "default_popup": "popup.html"
  },
  "content_scripts": {
    "matches": ["http://*"],
    "js": ["scripts.js"]
  }
}

When I try to load this extension into Chrome, I get the following message:

Could not load extension from 'C:\Users\foo\Desktop\Extensions\bar'.
Invalid value for 'content_scripts'.

I cannot see what is "invalid" about my value though. What I'm trying to do is match every URL, so my extension can manipulate the DOM (via javascript within scripts.js) of any page it is ran on. Am I missing something, going about this all wrong, or what?

update

After posting this question, I did notice that the Google example was slightly different than mine, so I modified my code a bit to reflect their syntax:

"content_scripts": [{
  "matches": ["http://*"],
  "js": ["scripts.js"]
}]

That being said, I still get the following error when trying to load my extension:

Could not load extension from 'C:\Users\foo\Desktop\Extensions\bar'.
Invalid value for 'content_scripts[0].matches[0]'.

Aristotle
  • 1,052
  • 1
  • 10
  • 15
  • I was receiving same error but I havne't write `"matches"` key . where do I see the chrome developer documentation which are required field while creating `content_scripts` – xkeshav Oct 09 '14 at 13:11

7 Answers7

88

You need to surround the value of the content_scripts field in square brackets:

"content_scripts": [ {
  "matches": ["http://*"],
  "js": ["scripts.js"]
} ]

(see the Chrome Docs for more info)

Incidentally, using http://*/* would be a better match for all urls (see the docs), adding https://*/* if you also need to match those as well.

Edit:

Following your edit, the error you are getting is because of the match pattern being incorrect.

ack
  • 7,356
  • 2
  • 25
  • 20
adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • I saw the `http://*/*` pattern, but was worried that this would require urls to have a `/` after the `http://` in order for my extension to run. Is this incorrect? – Aristotle May 04 '10 at 23:08
  • 1
    The docs page on pattern matching (http://code.google.com/chrome/extensions/match_patterns.html) explain it in detail. All extensions I have seen use the `http://*/*` pattern without issue. – adrianbanks May 04 '10 at 23:10
  • Yes. I saw this page just moments before asking my question. But their example shows `http://*/*` as matching `http://www.google.com/` which caused me to believe that a url must have a forward-slash after those associated with `http://` in order to invoke my extension logic. – Aristotle May 04 '10 at 23:12
  • Try it and see - I believe it will work for both `http://site.com` and `http://site.com/` – adrianbanks May 04 '10 at 23:16
  • 1
    Sure enough, it ended up being the pattern. Thank you, Adrian. – Aristotle May 04 '10 at 23:34
  • what about for edge extensions manifest files? – Hello Mar 04 '21 at 20:28
80

If you want to match every URL, then Google has a special pattern just for this purpose: <all_urls>

Sample usage:

"matches": ["<all_urls>"],

See this page for more info: https://developer.chrome.com/extensions/match_patterns

thdoan
  • 18,421
  • 1
  • 62
  • 57
  • 2
    Very cool, I hadn't known about that. For those looking for the relevant documentation though, the link is actually: http://code.google.com/chrome/extensions/match_patterns.html – npdoty Nov 08 '10 at 07:21
  • 2
    All links to code.google.com in the chosen answer and all comments before this one are broken following a site reorg. I've fixed the chosen answer itself with working links. – ack Mar 09 '14 at 10:22
11

Any match pattern should be of the following structure [scheme]://[host][path]

  1. scheme is '*' | 'http' | 'https' | 'file' | 'ftp'
  2. host is '' | '.' (any char except '/' and '*')+
  3. path is '/' (any chars)

For matching any HTTP/S and FILE URL use:

 "matches": [
    "*://*/*",
    "file://*/*"
  ],

Ref: https://developer.chrome.com/apps/match_patterns

By the way, in order to allow access to local files - add the permission:

"permissions": [
   "file://*/*"
]

Or approve file access on the extension settings page.

David D.
  • 557
  • 7
  • 20
4

For many that are getting errors involving:

'content_scripts[0].matches' is missing or invalid.

or

'content_scripts[0].matches[0]': Empty path.

Trying filling in, or creating, the matches field with your specific URL needed. If you want to use all URLs, then use the <all_urls> tag like below.

"content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": [ "jquery.js" ]
        }
    ]

Files listed in the "js" array have their path relative to you app. In other words, the location of "manifest.json" is your root directory.

Note: jquery.js is a file in my project's directory and you should replace it with whatever script file you want.

Loufs
  • 1,596
  • 1
  • 14
  • 22
3

After many tries following working , it matches all URL's and also supports http & https.enter code here

 "manifest_version": 2,
 "content_scripts": [
    {
     "matches": [
        "*://*/*",
      ]
   }
0

If you are using Jquery on a CDN, Just download it and include it to your working folder, And then try to import it

-3

Bro you forgot to add

"manifest_version":2

Which is mandatory one.