Content scripts can be declared in manifest.json
to be always injected into pages that match a set of URL patterns.
"content_scripts" : [
{
"js": ["content.js"],
"css": ["content.css"]
"matches": ["http://example.com/*"]
}
]
This manifest entry instructs Chrome to inject a content script content.js
, along with the CSS file content.css
, after any navigation to a page matching the match pattern http://example.com/*
Both js
and css
keys are optional: you can have only one of them or both depending on what you need.
content_scripts
key is an array, and you can declare several content script definitions:
"content_scripts" : [
{
"js": ["content.js"],
"matches": ["http://*.example.com/*"]
},
{
"js": ["something_else.js"],
"matches": ["http://*.example.org/*"]
}
]
Note that both js
and matches
are arrays, even if you only have one entry.
More options are available in the official documentation and other Examples.
Content scripts declared in the manifest will only be injected on new navigations after the extension load. They will not be injected in existing tabs. This also applies to extension reloads while developing, and extension updates after release.
If you need to ensure that currently opened tabs are covered, consider also doing programmatic injection on startup.