content_scripts

类型 数组
Mandatory No
范例
"content_scripts": [
  {
    "matches": ["*://*.mozilla.org/*"],
    "js": ["borderify.js"]
  }
]
									

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

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

  • must contain a key named 匹配 , which specifies the URL patterns to be matched in order for the scripts to be loaded;
  • may contain keys named js and css , which list scripts and/or stylesheets to be loaded into matching pages; and
  • may contain a number of other properties that control finer aspects of how and when content scripts are loaded.

Details of all the keys you can include are given in the table below.

名称 类型 描述
all_frames 布尔
true

Inject the scripts specified in js and css into all frames matching the specified URL requirements, even if the frame is not the topmost frame in a tab. This does not inject into child frames where only their parent matches the URL requirements and the child frame does not match the URL requirements. The URL requirements are checked for each frame independently.

注意: This also applies to any tracker or ad that uses iframes, which means that enabling this could make your content script get called dozens of times on some pages.

false
Inject only into frames matching the URL requirements which are the topmost frame in a tab.

默认为 false .

css 数组 An array of paths, relative to manifest.json , referencing CSS files that will be injected into matching pages.

Files are injected in the order given, and at the time specified by run_at .

注意: Firefox resolves URLs in injected CSS files relative to the CSS file itself, rather than to the page it's injected into.

exclude_globs 数组 An array of strings containing wildcards. See Matching URL patterns 下文。
exclude_matches 数组 An array of match patterns 。见 Matching URL patterns 下文。
include_globs 数组 An array of strings containing wildcards. See Matching URL patterns 下文。
js 数组 An array of paths, relative to manifest.json , referencing JavaScript files that will be injected into matching pages.

Files are injected in the order given. This means that, for example, if you include jQuery here followed by another content script, like this...

"js": ["jquery.js", "my-content-script.js"]
										

...then  "my-content-script.js" can use jQuery.

The files are injected after any files in css , and at the time specified by run_at .

match_about_blank 布尔 Insert the content scripts into pages whose URL is "about:blank" or "about:srcdoc" , if the URL of the page that opened or created this page matches the patterns specified in the rest of the content_scripts key.

This is especially useful to run scripts in empty iframes , whose URL is "about:blank" . To do this you should also set the all_frames key.

For example, suppose you have a content_scripts key like this:

  "content_scripts": [
    {
      "js": ["my-script.js"],
      "matches": ["https://example.org/"],
      "match_about_blank": true,
      "all_frames": true
    }
  ]
										

If the user loads https://example.org/ , and this page embeds an empty iframe, then "my-script.js" 将是 loaded into the iframe.

注意: match_about_blank is supported in Firefox from version 52.

Note that in Firefox, content scripts won't be injected into empty iframes at "document_start" , even if you specify that value in run_at .

匹配 数组 An array of match patterns 。见 Matching URL patterns 下文。

This is the only mandatory key.

run_at 字符串 This option determines when the files specified in css and js are injected. You can supply one of three strings here, each of which identifies a state in the process of loading a document. The states directly correspond to Document.readyState :

"document_start"
Corresponds to loading . The DOM is still loading.
"document_end"
Corresponds to interactive . The DOM has finished loading, but resources such as scripts and images may still be loading.
"document_idle"
Corresponds to complete . The document and all its resources have finished loading.

默认值为 "document_idle" .

In all cases, files in js are injected after files in css .

Matching URL patterns

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

匹配

an array of match patterns

exclude_matches

an array of match patterns

include_globs

an array of globs

exclude_globs

an array of globs

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.

由于 匹配 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 匹配 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.

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

范例

"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 .

浏览器兼容性

BCD tables only load in the browser

Found a problem with this page?

最后修改: , 由 MDN 贡献者

  1. 浏览器扩展名
  2. 快速入门
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
    6. What next?
  3. 概念
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Working with files
    5. 国际化
    6. Content Security Policy
    7. Native messaging
    8. Differences between API implementations
    9. Chrome incompatibilities
  4. 用户界面
    1. 用户界面
    2. Toolbar button
    3. Address bar button
    4. Sidebars
    5. Context menu items
    6. Options page
    7. Extension pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. 如何
    1. Intercept HTTP requests
    2. Modify a web page
    3. Insert external content
    4. Share objects with page scripts
    5. Add a button to the toolbar
    6. Implement a settings page
    7. Work with the Tabs API
    8. Work with the Bookmarks API
    9. Work with the Cookies API
    10. Work with contextual identities
    11. Interact with the clipboard
    12. Build a cross-browser extension
  6. Firefox differentiators
  7. JavaScript API
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. captivePortal
    8. clipboard
    9. 命令
    10. contentScripts
    11. contextualIdentities
    12. Cookie
    13. devtools
    14. dns
    15. downloads
    16. events
    17. extension
    18. extensionTypes
    19. find
    20. history
    21. i18n
    22. identity
    23. idle
    24. management
    25. menus
    26. notifications
    27. omnibox
    28. pageAction
    29. permissions
    30. pkcs11
    31. privacy
    32. proxy
    33. runtime
    34. search
    35. sessions
    36. sidebarAction
    37. storage
    38. tabs
    39. theme
    40. topSites
    41. 类型
    42. userScripts
    43. webNavigation
    44. webRequest
    45. windows
  8. Manifest keys
    1. 介绍
    1. 作者
    2. background
    3. browser_action
    4. browser_specific_settings
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. 命令
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. dictionaries
    15. externally_connectable
    16. homepage_url
    17. icons
    18. incognito
    19. manifest_version
    20. name
    21. offline_enabled
    22. omnibox
    23. optional_permissions
    24. options_page
    25. options_ui
    26. page_action
    27. permissions
    28. protocol_handlers
    29. short_name
    30. sidebar_action
    31. storage
    32. theme
    33. theme_experiment
    34. user_scripts
    35. version
    36. version_name
    37. web_accessible_resources
  9. Extension Workshop
    1. Develop
    2. Publish
    3. Manage
    4. Enterprise
  10. Contact us
  11. Channels
    1. Add-ons blog
    2. Add-ons forum
    3. Add-ons chat