Firefox 拡張機能 (Extensions) の作成方法(9) -- 再起動と右クリックした場所のデータの取得

firefoxOverlay.xul
右クリックした時、カーソルの下にあるオブジェクトを取得する為、
contentAreaContextMenu をオーバレイしています。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://fr/skin/overlay.css" type="text/css"?>
<!DOCTYPE overlay SYSTEM "chrome://fr/locale/fr.dtd">
<overlay id="fr-overlay"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="overlay.js"/>
  <stringbundleset id="stringbundleset">
    <stringbundle id="fr-strings" src="chrome://fr/locale/fr.properties"/>
  </stringbundleset>

  <popup id="contentAreaContextMenu">
    <menuseparator id="fr-Sep1" />  
    <menuitem id="fr-hello" label="&fr.label1;" 
              oncommand="fr.onMenuItemCommand1(event);"/>
    <menuseparator id="fr-Sep2" />  
    <menuitem id="fr-hello" label="&fr.label2;" 
              oncommand="fr.onMenuItemCommand2(event);"/>
  </popup>  
</overlay>
overlay.js
Firefox の再起動処理 は、Firefox/Thunderbird を再起動する にあるリンク先のソースコード
の一部ですが、これでうまく動いています。

カーソルの下のオブジェクトは、document.popupNode で取得されますが、フォーカスがある場合は、
そちらから取得しています。目的によって使い分けると良いと思います

※ 最終的に必要な文字列をクリップボードへコピーしています。
var fr = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("fr-strings");
  },
  onMenuItemCommand1: function(e) {
	// *************************************************
	// 再起動処理
	// *************************************************
	var appStartup =
		Components.classes["@mozilla.org/toolkit/app-startup;1"].
		getService(Components.interfaces.nsIAppStartup);
		appStartup.quit(appStartup.eAttemptQuit | appStartup.eRestart);
  },
  onMenuItemCommand2: function(e) {

	// *************************************************
	// 右クリックした場所のデータ
	// *************************************************
	var str = "";
	var tag = document.popupNode.tagName;

	if ( document.commandDispatcher.focusedElement != null ) {
		str = document.commandDispatcher.focusedElement.innerHTML;
	}
	else {
		switch( tag ) {
			case "IMG" :
				str = document.popupNode.src;
				break;
			default:
				str = document.popupNode.innerHTML;
				break;
		}
	}
	alert(str);
	// クリップボードへコピー
	var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].
		getService(Components.interfaces.nsIClipboardHelper);   
		gClipboardHelper.copyString(str);

  },

};
window.addEventListener("load", function(e) { fr.onLoad(e); }, false);
fr.dtd
<!ENTITY fr.label1 "再起動">
<!ENTITY fr.label2 "右クリックした場所のデータ">
install.rdf
<?xml version="1.0" encoding="UTF-8"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
 xmlns:em="http://www.mozilla.org/2004/em-rdf#">
  <Description about="urn:mozilla:install-manifest">
    <em:id>popupmenu@sample</em:id>
    <em:name>Firefox ポップアップメニューサンプル</em:name>
    <em:version>1.0</em:version>
    <em:creator>lightbox</em:creator>
    <em:targetApplication>
      <Description>
        <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id> <!-- firefox -->
        <em:minVersion>1.5</em:minVersion>
        <em:maxVersion>3.0.*</em:maxVersion>
      </Description>
    </em:targetApplication>
  </Description>
</RDF>