blob: 16f7fa2fdfa0030e9ea92c706c6ec9d698f916b6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
---
title: contentScripts
slug: Mozilla/Add-ons/WebExtensions/API/contentScripts
translation_of: Mozilla/Add-ons/WebExtensions/API/contentScripts
---
<div>{{AddonSidebar}}</div>
<p>Use this API to register content scripts. Registering a content script instructs the browser to insert the given content scripts into pages that match the given URL patterns.</p>
<p>This API is very similar to the <code><a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts">"content_scripts"</a></code> manifest.json key, except that with <code>"content_scripts"</code> , the set of content scripts and associated patterns is fixed at install time. With the <code>contentScripts</code> API, an extension can register and unregister scripts at runtime.</p>
<p>To use the API, call {{WebExtAPIRef("contentScripts.register()")}} passing in an object defining the scripts to register, the URL patterns, and other options. This returns a <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code> that is resolved with a {{WebExtAPIRef("contentScripts.RegisteredContentScript")}} object.</p>
<p>The <code>RegisteredContentScript</code> object represents the scripts that were registered in the <code>register()</code> call. It defines an <code>unregister()</code> method that you can use to unregister the content scripts. Content scripts are also unregistered automatically when the page that created them is destroyed. For example, if they are registered from the background page they will be unregistered automatically when the background page is destroyed, and if they are registered from a sidebar or a popup, they will be unregistered automatically when the sidebar or popup is closed.</p>
<p>There is no <code>contentScripts</code> API permission, but an extension must have the appropriate <a href="/en-US/Add-ons/WebExtensions/manifest.json/permissions#Host_permissions">host permissions</a> for any patterns it passes to <code>register()</code>.</p>
<h2 id="Types">Types</h2>
<dl>
<dt>{{WebExtAPIRef("contentScripts.RegisteredContentScript")}}</dt>
<dd>
<p>An object of this type is returned by the {{WebExtAPIRef("contentScripts.register()")}} function. It represents the content scripts that were registered by that call, and can be used to unregister the content script.</p>
</dd>
</dl>
<h2 id="Functions">Functions</h2>
<dl>
<dt>{{WebExtAPIRef("contentScripts.register()")}}</dt>
<dd>Registers the given content scripts.</dd>
</dl>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("webextensions.api.contentScripts", 10, 1)}}</p>
<p> {{WebExtExamples("h2")}}</p>
|