From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../webextensions/api/contentscripts/index.html | 47 +++++++++ .../api/contentscripts/register/index.html | 107 +++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 files/ja/mozilla/add-ons/webextensions/api/contentscripts/index.html create mode 100644 files/ja/mozilla/add-ons/webextensions/api/contentscripts/register/index.html (limited to 'files/ja/mozilla/add-ons/webextensions/api/contentscripts') diff --git a/files/ja/mozilla/add-ons/webextensions/api/contentscripts/index.html b/files/ja/mozilla/add-ons/webextensions/api/contentscripts/index.html new file mode 100644 index 0000000000..d72cf1de1d --- /dev/null +++ b/files/ja/mozilla/add-ons/webextensions/api/contentscripts/index.html @@ -0,0 +1,47 @@ +--- +title: contentScripts +slug: Mozilla/Add-ons/WebExtensions/API/contentScripts +tags: + - API + - Extensions + - Interface + - WebExtensions + - contentScripts + - インタフェース + - コンテントスクリプト + - 拡張機能 +translation_of: Mozilla/Add-ons/WebExtensions/API/contentScripts +--- +
{{AddonSidebar}}
+ +

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

+ +

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

+ +

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

+ +

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

+ +

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

+ +

Types

+ +
+
{{WebExtAPIRef("contentScripts.RegisteredContentScript")}}
+
+

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

+
+
+ +

Functions

+ +
+
{{WebExtAPIRef("contentScripts.register()")}}
+
コンテントスクリプトを登録します。
+
+ +

ブラウザの互換性

+ +

{{Compat("webextensions.api.contentScripts", 10, 1)}}

+ +

{{WebExtExamples("h2")}}

diff --git a/files/ja/mozilla/add-ons/webextensions/api/contentscripts/register/index.html b/files/ja/mozilla/add-ons/webextensions/api/contentscripts/register/index.html new file mode 100644 index 0000000000..ec8cfeaa28 --- /dev/null +++ b/files/ja/mozilla/add-ons/webextensions/api/contentscripts/register/index.html @@ -0,0 +1,107 @@ +--- +title: contentScripts.register() +slug: Mozilla/Add-ons/WebExtensions/API/contentScripts/register +tags: + - API + - Extensions + - Method + - Reference + - contentScripts + - register +translation_of: Mozilla/Add-ons/WebExtensions/API/contentScripts/register +--- +
{{AddonSidebar()}}
+ +

このメソッドは一つ以上の content scripts を登録するときに使用します。

+ +

manifest.json内の content_scripts に似た一つのオブジェクトを引数に持ちます。content_scripts では配列ですが、この register() ではオブジェクトを引数に持ちます。

+ +

これは Promise を返す非同期関数です。

+ +

構文

+ +
var registering = browser.contentScripts.register(
+  contentScriptOptions       // object
+)
+
+ +

パラメーター

+ +
+
contentScriptOptions
+
+

object です。RegisteredContentScriptOptions オブジェクトは登録するコンテントスクリプトを表します。content_scripts と似た構文のオブジェクトで、その違いは以下の通りです。

+ +
    +
  • プロパティ名にはスネーク形式ではなくキャメル形式を使用します (例えば、excludeMatchesを使用します。exclude_matches ではありません)
  • +
  • js プロパティと css プロパティには、相対パスのほかに文字列も指定できます。このため、登録したいものがどちらであるのかを明確にできる構文になっています。
  • +
+ +

RegisteredContentScriptOptions は次のプロパティを持ちます:

+ +
+
allFrames{{optional_inline}}
+
content_scripts における all_frames と同様です。
+
css{{optional_inline}}
+
オブジェクトの配列。 それぞれのオブジェクトは file という名前の manifest.json からの相対パスで登録したい CSS ファイルを指定した URL の文字列を持つプロパティか、code という名前の登録したい CSS の文字列を持つプロパティを含みます。
+
excludeGlobs{{optional_inline}}
+
content_scripts における exclude_globs と同様です。
+
excludeMatches{{optional_inline}}
+
content_scripts における exclude_matches と同様です。
+
includeGlobs{{optional_inline}}
+
content_scripts における include_globs と同様です。
+
js{{optional_inline}}
+
オブジェクトの配列。各オブジェクトは file または code プロパティを含み、その要素は css プロパティと同様です。
+
matchAboutBlank{{optional_inline}}
+
content_scripts における match_about_blank と同様です。
+
matches
+
content_scripts における matches と同様です。
+
runAt{{optional_inline}}
+
content_scripts における run_at と同様です。
+
+
+
+ +

返り値

+ +

登録したコンテントスクリプトを削除することができる {{WebExtAPIRef("contentScripts.RegisteredContentScript")}} オブジェクトを引数に持つ Promise を返します。

+ +


+ 現在、登録したコンテントスクリプトは、これを登録した拡張機能ページをアンロードしたときに削除されます。したがって、コンテントスクリプトを登録する際は、少なくとも登録されたままであってほしいだけ存在する拡張機能ページから登録すべきです。

+ +

ブラウザー実装状況

+ + + +

{{Compat("webextensions.api.contentScripts.register", 10)}}

+ +

+ +

defaultCode コンテントスクリプトを、すべての .org URL に対して登録します。

+ +
const defaultHosts = "*://*.org/*";
+const defaultCode = "document.body.innerHTML = '<h1>このページは書き換えられました<h1>'";
+
+async function register(hosts, code) {
+
+  return await browser.contentScripts.register({
+    matches: [hosts],
+    js: [{code}],
+    runAt: "document_idle"
+  });
+
+}
+
+var registered = register(defaultHosts, defaultCode);
+ +

次のコードは content_scripts/example.js にある JavaScript ファイルを登録します。

+ +
const scriptObj = await browser.contentScripts.register({
+  "js": [{file: "/content_scripts/example.js"}],
+  "matches": ["<all_urls>"],
+  "allFrames": true,
+  "runAt": "document_start"
+});
+
+ +

{{WebExtExamples}}

-- cgit v1.2.3-54-g00ecf