最終更新日: 2008年 2月 11日
ここは、以下にあるプログラムを参考にしています。
mozilla developer centerのFile I/O関連サンプル
拡張機能用ディレクトリのフルパスを取得
function getExtensionDir(id) {
var ext = Components.classes["@mozilla.org/extensions/manager;1"]
.getService(Components.interfaces.nsIExtensionManager)
.getInstallLocation(id)
.getItemLocation(id);
// ext は nsIFile のインスタンス、ext.path はディレクトリ文字列を保持します
return ext.path;
}
この例では、まず拡張機能が置かれているディレクトリのフルパスの文字列をgetExtensionDIr関数で取得します。 そして、そのディレクトリにある"example.txt"というテキストファイルを開き、1行づつ読み込みます。
var extPath = getExtensionDir('extest@aaa.net');
var file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(extPath + '\\exampe.txt');
// file から入力ストリームを開く
var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
istream.init(file, 0x01, 0444, 0);
istream.QueryInterface(Components.interfaces.nsILineInputStream);
// 行を配列に読み込む
var line = {}, lines = [], hasmore;
do {
hasmore = istream.readLine(line); // 1行読み込む
lines.push(line.value);
} while(hasmore);
istream.close();
// データで何かする
//alert(lines);
要素を別の要素の子要素として追加する場合、以下の関数を使用します。
appendChild()
insertBefore()
replaceChild()
削除ではremoveChild()を使用します
注意. ポップアップを持つ新しいメニューを追加する場合は、appehdChildを実行するタイミングに 注意すること。
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
// 新しい XUL メニューアイテムを作成する
var menu = document.createElementNS(XUL_NS, "menu");
menu.setAttribute("label", "testMenu");
menu.setAttribute("id", "testMenu");
var popup = document.getElementById("cpdev-popup-menu");
popup.appendChild(menu); // この位置が問題!!!
var newPopup = document.createElementNS(XUL_NS, 'menupopup');
menu.appendChild(newPopup);
// 新しい XUL メニューアイテムを作成する
var item = document.createElementNS(XUL_NS, "menuitem");
item.setAttribute("label", 'aaaa');
newPopup.appendChild(item);
正しいのは、新規メニュにポップアップを登録(appendChild)後、親ポップアップに登録します。
メニューアイテムの登録は、その後でも可能です。
var newPopup = document.createElementNS(XUL_NS, 'menupopup');
menu.appendChild(newPopup);
var popup = document.getElementById("cpdev-popup-menu");
popup.appendChild(menu); // これが menu.appendChild(newPopup)の後に来ること
var req = new XMLHttpRequest();
req.open('GET', 'file:///d:/Develop/test.xml', false);
req.send(null);
if (0 != req.status) {
this.debug.dump('XMLファイルを開けませんでした');
return;
}
var dom = req.responseXML;
if (dom.documentElement.nodeName == "parsererror") {
this.debug.dump("error while parsing");
return;
}
this.debug.dump('XML:' + dom.documentElement.nodeName);
・・・//処理・・・
注意. req.open('GET', XMLファイルのパス)のように第3引数のfalseを指定忘れた場合、
req.repsonseXMLはnullを返します。
そして、dom.documentElement.nodeNameで処理がストップされますが、
エラーコンソールには何も表示されず、エラー原因に気がつき難いので、第3引数のfalseは
忘れないように。
参考
以下のJavaScriptでdocumentオブジェクトの要素をalertで確認することができます。
var pr = ""; var i = 0; for (var s in document ) { pr = pr + " " + s ; }; alert(pr);