↑ arvau

MDN / content_scripts

Instructs the browser to load content scripts into web pages whose URL matches a pattern.

This key is an array. Each item is an object which:

  • must contain a property named matches, which specifies the URL patterns to be matched for the scripts to be loaded;

  • may contain properties named js and css, which list scripts and stylesheets to be loaded into matching pages; and

  • may contain a number of other properties that control aspects of how and when content scripts are loaded.

This table details all the properties you can include.

Load order

Registered objects in content_scripts are injected into matching web pages at the time specified by run_at (first document_start, then document_end, and finally document_idle):

  • In the order specified in the content_scripts array, for each object with a matching run_at value, then:

    • CSS is applied in the order specified in its css array. By default CSS from the "author" origin is given priority unless css_origin is set to "user".

    • JavaScript code is executed in the order specified in its js array.

For example, in this key specification:

"content_scripts": [
    {
    "matches": ["*://*.mozilla.org/*"],
    "js": ["jquery.js", "my-content-script.js"],
    "run_at": "document_idle"
  },
  {
    "matches": ["*://*.mozilla.org/*"],
    "css": ["my-css.css"],
    "js": ["another-content-script.js", "yet-another-content-script.js"],
    "run_at": "document_idle"
  },
  {
    "matches": ["*://*.mozilla.org/*"],
    "js": ["run-first.js"],
    "run_at": "document_start"
  }
]

The files are loaded like this when a mozilla.org domain opens:

  • "run-first.js" - because it’s requested to run at "document_start".

  • "jquery.js" - because it’s in the first array requesting run at "document_idle".

  • "my-content-script.js" - because it’s the second item in the first array requesting run at "document_idle".

  • "my-css.css" - because an object’s CSS is loaded before its JavaScript.

  • "another-content-script.js" - because it’s the first item in the js property.

  • "yet-another-content-script.js"

Matching URL patterns

The "content_scripts" key attaches content scripts to documents based on URL matching: if the document’s URL matches the specification in the key, then the script will be attached. There are four properties inside "content_scripts" that you can use for this specification:

To match one of these properties, a URL must match at least one of the items in its array. For example, given a property like:

"matches": ["*://*.example.org/*", "*://*.example.com/*"]

Both http://example.org/ and http://example.com/ will match.

Since matches is the only mandatory key, the other three keys are used to limit further the URLs that match. To match the key as a whole, a URL must:

  • match the matches property

  • AND match the include_globs property, if present

  • AND NOT match the exclude_matches property, if present

  • AND NOT match the exclude_globs property, if present

globs

A glob is just a string that may contain wildcards.

There are two types of wildcard, and you can combine them in the same glob:

  1. * matches zero or more characters

  2. ? matches exactly one character.

For example: "*na?i" would match "illuminati" and "annunaki", but not "sagnarelli".

Example

"content_scripts": [
  {
    "matches": ["*://*.mozilla.org/*"],
    "js": ["borderify.js"]
  }
]

This injects a single content script borderify.js into all pages under mozilla.org or any of its subdomains, whether served over HTTP or HTTPS.

  "content_scripts": [
    {
      "exclude_matches": ["*://developer.mozilla.org/*"],
      "matches": ["*://*.mozilla.org/*"],
      "js": ["jquery.js", "borderify.js"]
    }
  ]

This injects two content scripts into all pages under mozilla.org or any of its subdomains except developer.mozilla.org, whether served over HTTP or HTTPS.

The content scripts see the same view of the DOM and are injected in the order they appear in the array, so borderify.js can see global variables added by jquery.js.

Specifications

Browser compatibility