--- title: JS XPCOM slug: Code_snippets/JS_XPCOM translation_of: Archive/Add-ons/Code_snippets/JS_XPCOM ---
JavaScript で XPCOM コンポーネントを利用するためのいくつかの有用なコードを示します。
{{ 英語版章題("Contract IDs") }}
コンタクト ID は XPCOM オブジェクトの固有の名前になります。XPCOM で既知のオブジェクトを作成もしくはアクセスするときに利用します。
{{ 英語版章題("Interfaces") }}
全ての XPCOM オブジェクトは、一つ以上のインターフェースをもちます。インターフェースは簡単には、nsIFile のようなオブジェクト上で呼び出せる定数やメソッドのリストです。全ての XPCOM オブジェクトは、nsISupports インターフェースを実装していなければなりません。
{{ 英語版章題("Accessing XPCOM components from JavaScript") }}
XPCOM オブジェクトは新規インスタンスとして作成 (完全に新しいオブジェクトが戻ります) するか、もしくはサービスとして作成 (同じオブジェクトが戻ります。シングルトンと呼ばれてもいます。) することが可能です。新しいインスタンスとして作成するかサービスとしてアクセスするかはオブジェクトに依存します。XPCOM オブジェクトを取得するには、オブジェクトのコンタクト ID と呼び出したいインターフェースを知っておく必要があります。
{{ 英語版章題("Creating an instance of a component") }}
var component = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsIFile);
このコードにより、コンタクト ID が @mozilla.org/file/local;1 のオブジェクトへの新しいインスタンスが生成され、nsIFile インターフェースの全てのメソッドを呼ぶことができます。
{{ 英語版章題("Getting an XPCOM service") }}
var preferences = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
このコードにより、設定オブジェクトの nsIPrefService インターフェースの全てのメソッドを呼ぶことができます。
{{ 英語版章題("Getting a different interface for a component") }}
コンポーネントによっては一つ以上のインターフェースを自走しています。時によっては JavaScript はコンポーネントに存在する全てのインターフェースを認識するほど賢いですが、ほとんどの場合はあなたがインターフェースをチェックする必要があります。先ほどの設定サービスのサンプルでは次のように調べます。
preferences = preferences.QueryInterface(Components.interfaces.nsIPrefBranch2);
このコードにより、nsIPrefBranch2 インターフェースのメソッドを利用できるようになります。
{{ 英語版章題("Implementing XPCOM components in JavaScript") }}
JavaScript による XPCOM コンポーネントの簡単なサンプルです。利用するためには、次のことをする必要があります。
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;
}
{{ languages( { "en": "en/Code_snippets/JS_XPCOM", "fr": "fr/Extraits_de_code/JS_XPCOM", "pl": "pl/Fragmenty_kodu/JS_XPCOM" } ) }}
<hints id="hah_hints"/>