home
« 前 目次

1.設定の読み込み、書き込み

Firefoxの設定システム(ロケーションバーにabout:configと打つと出てくるヤツ)を利用して設定の読み書きができます。以下では例として"extensions.helloworld.message"の設定名の値の読み書きを行います。なお、アドオンの設定名は"extensions.<アドオン名>."で始めるのが慣習のようです。

まず、helloworld.xulの該当部分を以下のように書き換えて、右クリックメニューから取得命令を、ツールメニューから設定命令を実行できるようにします。

	<!-- ツールメニューの追加 -->
	<menupopup id="menu_ToolsPopup">
		<menuitem insertafter="devToolsSeparator" label="設定"
			accesskey="S" oncommand="helloworld.messageSet();" />
	</menupopup>

	<!-- 右クリックメニューの追加 -->
	<popup id="contentAreaContextMenu">
		<menuitem id="hw_contextMenu" label="取得" 
			oncommand="helloworld.messageGet();"/>
	</popup>

helloworld.jsに以下のコードを貼り付けます。

//他のアドオンと変数や関数の名前がかぶるとうまく動作しないので、かぶらないような名前をつける。あるいはオブジェクトにまとめる。
hw_prefs=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
hw_Branch=hw_prefs.getBranch("extensions.helloworld.");
var helloworld={

messageGet: function(){
	var msg = hw_Branch.getCharPref("message");// "extensions.helloworld.message"(文字列型)の取得
    alert(msg);
},
messageSet: function(){
	var msg = prompt("メッセージの入力");
	hw_Branch.setCharPref("message",msg);// "extensions.helloworld.message"(文字列型)にmsgを保存
	}
};

まず、最初の行(hw_prefs〜)は、Firefoxの設定システムを使用することを宣言しています。次の行は "extensions.helloworld." のBranchを取得しています。これにより、例えばgetCharPref()(文字列型の設定を取得する命令)で "message" だけで "extensions.helloworld.message" を指定できます(コードの赤い部分参照)。設定には文字列、整数値、真偽値の三つの型があり、それぞれ取得と設定にgetCharPref()setCharPref()getIntPref()setIntPref()getBoolPref()setBoolPref()を使います。

さて、ここでいったんFirefoxを再起動してアドオンを更新します(前々回xpiファイルでインストールしていた場合、chrome.manifestを修正して再度インストールする)。「取得」メニューを実行すると全くメッセージが表示されないと思いますが、これは最初"extensions.helloworld.message"に何の値も設定されてないためです。「defaults/preferences/helloworld.js」ファイル(無ければ作る)に以下のように入力することでデフォルトの値を設定できます。

pref("extensions.helloworld.message", "Hello, World!");//prefs(設定名, 設定値)の書式で書く

再びFirefoxを再起動します。about:configを開いて"extensions.helloworld."と入力すると、"extensions.helloworld.message"の設定名で設定が保存されていることがわかります。

詳細な説明:
Code snippets:Preferences

2.設定ウインドウ

Firefoxには設定ウインドウ専用のウインドウ部品であるprefwindowが用意されています。設定ウインドウ用のpref.xulファイルを作って以下のコードを貼り付けてください。

<?xml version="1.0"?>

<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?>

<prefwindow id="helloworld.pref" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 
	title="helloworldの設定" bottons="accept,cancel">
	<prefpane id="pref.general" label="一般">
		<preferences>
			<preference id="extensions.helloworld.message" name="extensions.helloworld.message" type="string" />
		</preferences>
		<hbox>
			<label id="label1" value="メッセージ" />
			<textbox id="extensions.helloworld.message" preference="extensions.helloworld.message" />
		</hbox>
	</prefpane>
	<prefpane id="pref.empty" label="その他"></prefpane>
</prefwindow>
bottons="accept,cancel"
prefwindowのbuttons属性に"accept,cancel"を指定することで自動的にOK,キャンセルボタンが作られます。
<prefpane id="pref.general" label="一般">
設定のカテゴリごとにprefpaneに分けます(カテゴリが一つしかない場合でも必ずprefpaneを書く必要があります)
<preference id="extensions.helloworld.message" name="extensions.helloworld.message" type="string" />
preferencesタグ内にそのprefpaneで設定する設定名の数だけpreferenceタグを書きます。nameに設定したい設定名を書き、typeに型(int,bool,stringなど)を書きます。
preference="extensions.helloworld.message"
設定を入力する部品にpreference属性を追加し、設定名を書きます。これにより、ウインドウを開くときにはその部品に自動的に設定の値が読み込まれ、OKを押したときは自動的に値が保存されます。

3.設定ウインドウの登録

install.rdfに以下のように設定ウインドウのchromeアドレスを記入することで、アドオンウインドウの設定ボタンから設定ウインドウを開けるようになります。

<em:optionsURL>chrome://helloworld/content/pref.xul</em:optionsURL>

xpiファイルを使わない方法でインストールしてる場合、install.rdfの変更を反映するためには、いったんアドオンを削除して、再インストールする必要があります。

メニューなどから設定ウインドウを開きたい場合、openDialog()を使います。

window.openDialog("chrome://helloworld/content/pref.xul", "Preferences", "chrome,titlebar,toolbar,centerscreen,modal","pref.general");//四つ目の引数でどのprefpaneを開くかを指定できる。

詳細な説明:
 prefwindowのリファレンス
 preferenceのリファレンス

« 前 目次
inserted by FC2 system