aboutsummaryrefslogtreecommitdiff
path: root/files/ja/mozilla/add-ons/webextensions/api/contentscripts/index.html
blob: 5726499a9bc7d06d4273d7ad396b5aa2a5001121 (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
39
40
41
42
43
44
45
46
47
---
title: contentScripts
slug: Mozilla/Add-ons/WebExtensions/API/contentScripts
tags:
  - API
  - Extensions
  - Interface
  - WebExtensions
  - contentScripts
  - インタフェース
  - コンテントスクリプト
  - 拡張機能
translation_of: Mozilla/Add-ons/WebExtensions/API/contentScripts
---
<div>{{AddonSidebar}}</div>

<p>このAPIはコンテントスクリプトを登録するためにお使いいただけます。コンテントスクリプトを登録することで、指定したURLにマッチするページにそのスクリプトを挿入するようブラウザに指定することができます。</p>

<p>このAPIは<code>manifest.json</code>にある"content_scripts"キーと似ていますが、"content_scripts"ではコンテントスクリプトとURLのパターンは<strong>インストールタイム</strong>に固定されます。<span class="seoSummary">一方<code>content_scripts</code> APIは、<strong>ランタイム(実行時)</strong>でスクリプトを登録・登録解除することが可能です。</span></p>

<p>このAPIは、{{WebExtAPIRef("contentScripts.register()")}} メソッドを呼び出して使用していただけます。その際は、登録するコンテントスクリプト、URLのマッチングパターン、またその他のオプションを実引数(arguments)として渡してください。このメソッドは{{WebExtAPIRef("contentScripts.RegisteredContentScript")}} オブジェクトがresolveされた <code><a href="/ja/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a></code> を返します。</p>

<p><code>RegisteredContentScript</code> オブジェクトは <code>register()</code> で登録されたスクリプトを保持し、<code>unregister()</code>でそのスクリプトを登録解除(unregister)できます。また、コンテントスクリプトはそれらを作ったページが消された際にも自動的に登録解除されます。例えば、backgroundページによって登録されたコンテントスクリプトは、backgroundページが消去された際に自動的に登録解除されます。</p>

<p><code>contentScripts</code> API にパーミッションは存在しませんが、拡張機能はregister()でマッチさせるURLにおいては適切な<a href="/ja/Add-ons/WebExtensions/manifest.json/permissions#Host_permissions">host permissions</a>を持っている必要があります。</p>

<h2 id="Types">Types</h2>

<dl>
 <dt>{{WebExtAPIRef("contentScripts.RegisteredContentScript")}}</dt>
 <dd>
 <p>このタイプのオブジェクトは{{WebExtAPIRef("contentScripts.register()")}}関数の返り値です。{{WebExtAPIRef("contentScripts.register()")}}関数で登録されたコンテントスクリプトを持ち、このオブジェクトを使ってそれらを登録解除することができます。</p>
 </dd>
</dl>

<h2 id="Functions">Functions</h2>

<dl>
 <dt>{{WebExtAPIRef("contentScripts.register()")}}</dt>
 <dd>コンテントスクリプトを登録します。</dd>
</dl>

<h2 id="ブラウザの互換性">ブラウザの互換性</h2>

<p>{{Compat("webextensions.api.contentScripts", 10, 1)}}</p>

<p>{{WebExtExamples("h2")}}</p>