diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/ja/code_snippets/js_xpcom | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/ja/code_snippets/js_xpcom')
-rw-r--r-- | files/ja/code_snippets/js_xpcom/index.html | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/files/ja/code_snippets/js_xpcom/index.html b/files/ja/code_snippets/js_xpcom/index.html new file mode 100644 index 0000000000..6e1b257c99 --- /dev/null +++ b/files/ja/code_snippets/js_xpcom/index.html @@ -0,0 +1,115 @@ +--- +title: JS XPCOM +slug: Code_snippets/JS_XPCOM +translation_of: Archive/Add-ons/Code_snippets/JS_XPCOM +--- +<p> </p> +<p>JavaScript で XPCOM コンポーネントを利用するためのいくつかの有用なコードを示します。</p> +<p>{{ 英語版章題("Contract IDs") }}</p> +<h3 id=".E3.82.B3.E3.83.B3.E3.82.BF.E3.82.AF.E3.83.88_ID">コンタクト ID</h3> +<p>コンタクト ID は XPCOM オブジェクトの固有の名前になります。XPCOM で既知のオブジェクトを作成もしくはアクセスするときに利用します。</p> +<p>{{ 英語版章題("Interfaces") }}</p> +<h3 id=".E3.82.A4.E3.83.B3.E3.82.BF.E3.83.BC.E3.83.95.E3.82.A7.E3.83.BC.E3.82.B9">インターフェース</h3> +<p>全ての XPCOM オブジェクトは、一つ以上のインターフェースをもちます。インターフェースは簡単には、<a href="/ja/NsIFile" title="ja/NsIFile">nsIFile</a> のようなオブジェクト上で呼び出せる定数やメソッドのリストです。全ての XPCOM オブジェクトは、<a href="/ja/NsISupports" title="ja/NsISupports">nsISupports</a> インターフェースを実装していなければなりません。</p> +<p>{{ 英語版章題("Accessing XPCOM components from JavaScript") }}</p> +<h3 id="JavaScript_.E3.81.8B.E3.82.89_XPCOM_.E3.82.B3.E3.83.B3.E3.83.9D.E3.83.BC.E3.83.8D.E3.83.B3.E3.83.88.E3.81.AB.E3.82.A2.E3.82.AF.E3.82.BB.E3.82.B9.E3.81.99.E3.82.8B">JavaScript から XPCOM コンポーネントにアクセスする</h3> +<p>XPCOM オブジェクトは新規インスタンスとして作成 (完全に新しいオブジェクトが戻ります) するか、もしくはサービスとして作成 (同じオブジェクトが戻ります。シングルトンと呼ばれてもいます。) することが可能です。新しいインスタンスとして作成するかサービスとしてアクセスするかはオブジェクトに依存します。XPCOM オブジェクトを取得するには、オブジェクトのコンタクト ID と呼び出したいインターフェースを知っておく必要があります。</p> +<p>{{ 英語版章題("Creating an instance of a component") }}</p> +<h4 id=".E3.82.B3.E3.83.B3.E3.83.9D.E3.83.BC.E3.83.8D.E3.83.B3.E3.83.88.E3.81.AE.E3.82.A4.E3.83.B3.E3.82.B9.E3.82.BF.E3.83.B3.E3.82.B9.E3.82.92.E4.BD.9C.E6.88.90.E3.81.99.E3.82.8B">コンポーネントのインスタンスを作成する</h4> +<pre>var component = Components.classes["@mozilla.org/file/local;1"] + .createInstance(Components.interfaces.nsIFile); +</pre> +<p>このコードにより、コンタクト ID が <code>@mozilla.org/file/local;1</code> のオブジェクトへの新しいインスタンスが生成され、<a href="/ja/NsIFile" title="ja/NsIFile">nsIFile</a> インターフェースの全てのメソッドを呼ぶことができます。</p> +<p>{{ 英語版章題("Getting an XPCOM service") }}</p> +<h4 id="XPCOM_.E3.82.B5.E3.83.BC.E3.83.93.E3.82.B9.E3.82.92.E5.8F.96.E5.BE.97.E3.81.99.E3.82.8B">XPCOM サービスを取得する</h4> +<pre>var preferences = Components.classes["@mozilla.org/preferences-service;1"] + .getService(Components.interfaces.nsIPrefService); +</pre> +<p>このコードにより、設定オブジェクトの <code><a href="/ja/NsIPrefService" title="ja/NsIPrefService">nsIPrefService</a></code> インターフェースの全てのメソッドを呼ぶことができます。</p> +<p>{{ 英語版章題("Getting a different interface for a component") }}</p> +<h4 id=".E3.82.B3.E3.83.B3.E3.83.9D.E3.83.BC.E3.83.8D.E3.83.B3.E3.83.88.E3.81.AE.E5.88.A5.E3.81.AE.E3.82.A4.E3.83.B3.E3.82.BF.E3.83.BC.E3.83.95.E3.82.A7.E3.83.BC.E3.82.B9.E3.82.92.E5.8F.96.E5.BE.97.E3.81.99.E3.82.8B">コンポーネントの別のインターフェースを取得する</h4> +<p>コンポーネントによっては一つ以上のインターフェースを自走しています。時によっては JavaScript はコンポーネントに存在する全てのインターフェースを認識するほど賢いですが、ほとんどの場合はあなたがインターフェースをチェックする必要があります。先ほどの設定サービスのサンプルでは次のように調べます。</p> +<pre>preferences = preferences.QueryInterface(Components.interfaces.nsIPrefBranch2); +</pre> +<p>このコードにより、<code><a href="/ja/NsIPrefBranch2" title="ja/NsIPrefBranch2">nsIPrefBranch2</a></code> インターフェースのメソッドを利用できるようになります。</p> +<p>{{ 英語版章題("Implementing XPCOM components in JavaScript") }}</p> +<h3 id="XPCOM_.E3.82.B3.E3.83.B3.E3.83.9D.E3.83.BC.E3.83.8D.E3.83.B3.E3.83.88.E3.82.92_JavaScript_.E3.81.A7.E5.AE.9F.E8.A3.85.E3.81.99.E3.82.8B">XPCOM コンポーネントを JavaScript で実装する</h3> +<p>JavaScript による XPCOM コンポーネントの簡単なサンプルです。利用するためには、次のことをする必要があります。</p> +<ul> <li>initModule の最初の 3 行をあなた自身のものに入れ替える</li> <li>QueryInterface メソッドのテスト (equals 内部) をあなたが実装しようとしているインターフェースに置き換える</li> <li>プロトタイプに実装しようとしているインターフェースのメソッドを追加する</li> <li>ExsampleComponent コンストラクタに初期化コードを必要なら追加する</li> +</ul> +<pre>function ExampleComponent() +{ + // コンポーネントの初期化コードはここに追加 +} + +ExampleComponent.prototype = { +QueryInterface: function(iid) +{ + if (iid.equals(Components.interfaces.myInterface) + || iid.equals(Ci.nsISupports)) + { + return this; + } + else + { + throw Components.results.NS_ERROR_NO_INTERFACE; + } +} +}; + +var initModule = +{ + ServiceCID: Components.ID("{examplee-xamp-leex-ampl-eexampleexam}"), // quote の中に guid を追加 + ServiceContractID: "@example.com/example;1", // quote の中にコンタクト ID を挿入 + ServiceName: "", // quote の中に名前を挿入 + + registerSelf: function (compMgr, fileSpec, location, type) + { + compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar); + compMgr.registerFactoryLocation(this.ServiceCID,this.ServiceName,this.ServiceContractID, + fileSpec,location,type); + }, + + unregisterSelf: function (compMgr, fileSpec, location) + { + compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar); + compMgr.unregisterFactoryLocation(this.ServiceCID,fileSpec); + }, + + getClassObject: function (compMgr, cid, iid) + { + if (!cid.equals(this.ServiceCID)) + throw Components.results.NS_ERROR_NO_INTERFACE + if (!iid.equals(Components.interfaces.nsIFactory)) + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + return this.instanceFactory; + }, + + canUnload: function(compMgr) + { + return true; + }, + + instanceFactory: + { + createInstance: function (outer, iid) + { + if (outer != null) + throw Components.results.NS_ERROR_NO_AGGREGATION; + return new ExampleComponent().QueryInterface(iid); + } + } +}; //Module + +function NSGetModule(compMgr, fileSpec) +{ + return initModule; +} +</pre> +<p> </p> +<p> </p> + +<p>{{ languages( { "en": "en/Code_snippets/JS_XPCOM", "fr": "fr/Extraits_de_code/JS_XPCOM", "pl": "pl/Fragmenty_kodu/JS_XPCOM" } ) }}</p> + +<p> </p> +<p><hints id="hah_hints"/></p> |