contentScripts

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

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

この API は、contentScripts.register() メソッドを呼び出して使用していただけます。その際は、登録するコンテントスクリプト、URL のマッチングパターン、またその他のオプションを実引数(arguments)として渡してください。このメソッドはcontentScripts.RegisteredContentScript オブジェクトが resolve された Promise を返します。

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

contentScripts API にパーミッションは存在しませんが、拡張機能は register()でマッチさせる URL においては適切なhost permissionsを持っている必要があります。

Types

contentScripts.RegisteredContentScript

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

Functions

contentScripts.register()

コンテントスクリプトを登録します。

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
contentScripts
RegisteredContentScript
RegisteredContentScript.unregister
register
register.cookieStoreId
register.matchOriginAsFallback
register.world

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

Example extensions