From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/chrome/command_line/index.html | 204 ++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 files/ja/chrome/command_line/index.html (limited to 'files/ja/chrome') diff --git a/files/ja/chrome/command_line/index.html b/files/ja/chrome/command_line/index.html new file mode 100644 index 0000000000..4e23c49d55 --- /dev/null +++ b/files/ja/chrome/command_line/index.html @@ -0,0 +1,204 @@ +--- +title: Command Line +slug: Chrome/Command_Line +tags: + - Add-ons + - Extensions +translation_of: Archive/Mozilla/XUL/Providing_Command-Line_Options +--- +
{{outdated("英語版もあわせてご覧下さい。")}}
+

拡張機能と XUL アプリケーションは nsICommandLineHandler を実装したコンポーネントを書いてそれをカテゴリへレジストする事でコマンドラインパラメーターを拾うことができます。

+

Overview

+

この添付したコードは XPCOM フレームワークを用いて書かれたコンポーネントで、XPConnect を通して JavaScript からアクセスできます。

+

Firefoxのコマンドラインをハンドル及び初期化についての公式な(簡明な)ページは nsICommandLine のドキュメントで見つける事ができます。これらは、XPIDL ファイルで定義されているインターフェイスの実装されているコードのリンクを見つけられるでしょう。またそれらは .idl ファイルと関係があります。

+

次に例は二つのコマンドラインパラメータをインプリメントしたコンポーネントの例です。

+ +
+
firefox.exe -myapp
+
My Applicationの chrome window を開きます。
+
firefox.exe -viewapp url
+
My Applicationの chrome window を開き、nsIURL オブジェクトを通します。
+
+

JavaScriptでXPCOMを書くもしくはレジストするこれ以上の方法を見つけるには次の チュートリアル を試してください。

+

コンポーネントを順番にレジストする方法は{{Anch("Sample code")}}セクションに詳細があり、大きなボックスにある JavaScript ファイルを.js という拡張子で保存して、Firefox が拡張機能をスキャンするディレクトリ(Bundlesを参照の事)へ入れてください。Windows における典型的な場所は"C:\Program Files\Mozilla Firefox\components" で、もしくはあなたの拡張機能のcomponents ディレクトリになります。

+

そのとき(Firefoxをシャットダウンしたとき)に、compreg.dat ファイルを削除して Firefox に強制的にコンポーネントのリストを再ビルドさせて、そしてFirefoxを再起動します。compreg.dat ファイルをあなたのプロファイルのディレクトリに見つけても、同じ名前のファイルがFirefox のディレクトリにある事に対して混乱しないでください。

+

あなたのプロファイルのディレクトリはホームディレクトリ以下 (Linuxであれば、 ~/.mozilla/firefox )もしくはたぶん 'Windows上では '"C:\Documents and Settings\(user-name)\Application Data\Mozilla\Firefox\Profiles\" にあるでしょう。

+

Firefoxを再起動した後には、 コンポーネントが新しくなっていれば新たにcompreg.dat ファイルが作られるでしょう。

+

その contact ID はこのようになります: @mozilla.org/commandlinehandler/general-startup;1?type=myapp

+

もし、Extension Developer's Extension がインストールされていれば、Javascript shellを通してコンポーネントにアクセスでき、ためしにComponents.classes や Components.classesByID 配列にアクセスできます。これらの例は このチュートリアルで見ることができます。

+

Sample Code

+ +
const nsIAppShellService    = Components.interfaces.nsIAppShellService;
+const nsISupports           = Components.interfaces.nsISupports;
+const nsICategoryManager    = Components.interfaces.nsICategoryManager;
+const nsIComponentRegistrar = Components.interfaces.nsIComponentRegistrar;
+const nsICommandLine        = Components.interfaces.nsICommandLine;
+const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
+const nsIFactory            = Components.interfaces.nsIFactory;
+const nsIModule             = Components.interfaces.nsIModule;
+const nsIWindowWatcher      = Components.interfaces.nsIWindowWatcher;
+
+// CHANGEME: to the chrome URI of your extension or application
+const CHROME_URI = "chrome://myapp/content/";
+
+// CHANGEME: change the contract id, CID, and category to be unique
+// to your application.
+const clh_contractID = "@mozilla.org/commandlinehandler/general-startup;1?type=myapp";
+
+// use uuidgen to generate a unique ID
+const clh_CID = Components.ID("{2991c315-b871-42cd-b33f-bfee4fcbf682}");
+
+// category names are sorted alphabetically. Typical command-line handlers use a
+// category that begins with the letter "m".
+const clh_category = "m-myapp";
+
+/**
+ * Utility functions
+ */
+
+/**
+ * Opens a chrome window.
+ * @param aChromeURISpec a string specifying the URI of the window to open.
+ * @param aArgument an argument to pass to the window (may be null)
+ */
+function openWindow(aChromeURISpec, aArgument)
+{
+  var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].
+    getService(Components.interfaces.nsIWindowWatcher);
+  ww.openWindow(null, aChromeURISpec, "_blank",
+                "chrome,menubar,toolbar,status,resizable,dialog=no",
+                aArgument);
+}
+
+/**
+ * The XPCOM component that implements nsICommandLineHandler.
+ * It also implements nsIFactory to serve as its own singleton factory.
+ */
+const myAppHandler = {
+  /* nsISupports */
+  QueryInterface : function clh_QI(iid)
+  {
+    if (iid.equals(nsICommandLineHandler) ||
+        iid.equals(nsIFactory) ||
+        iid.equals(nsISupports))
+      return this;
+
+    throw Components.results.NS_ERROR_NO_INTERFACE;
+  },
+
+  /* nsICommandLineHandler */
+
+  handle : function clh_handle(cmdLine)
+  {
+    try {
+      // CHANGEME: change "viewapp" to your command line flag that takes an argument
+      var uristr = cmdLine.handleFlagWithParam("viewapp", false);
+      if (uristr) {
+        // convert uristr to an nsIURI
+        var uri = cmdLine.resolveURI(uristr);
+        openWindow(CHROME_URI, uri);
+        cmdLine.preventDefault = true;
+      }
+    }
+    catch (e) {
+      Components.utils.reportError("incorrect parameter passed to -viewapp on the command line.");
+    }
+
+    // CHANGEME: change "myapp" to your command line flag (no argument)
+    if (cmdLine.handleFlag("myapp", false)) {
+      openWindow(CHROME_URI, null);
+      cmdLine.preventDefault = true;
+    }
+  },
+
+  // CHANGEME: change the help info as appropriate, but
+  // follow the guidelines in nsICommandLineHandler.idl
+  // specifically, flag descriptions should start at
+  // character 24, and lines should be wrapped at
+  // 72 characters with embedded newlines,
+  // and finally, the string should end with a newline
+  helpInfo : "  -myapp               Open My Application\n" +
+             "  -viewapp <uri>       View and edit the URI in My Application,\n" +
+             "                       wrapping this description\n",
+
+  /* nsIFactory */
+
+  createInstance : function clh_CI(outer, iid)
+  {
+    if (outer != null)
+      throw Components.results.NS_ERROR_NO_AGGREGATION;
+
+    return this.QueryInterface(iid);
+  },
+
+  lockFactory : function clh_lock(lock)
+  {
+    /* no-op */
+  }
+};
+
+/**
+ * The XPCOM glue that implements nsIModule
+ */
+const myAppHandlerModule = {
+  /* nsISupports */
+  QueryInterface : function mod_QI(iid)
+  {
+    if (iid.equals(nsIModule) ||
+        iid.equals(nsISupports))
+      return this;
+
+    throw Components.results.NS_ERROR_NO_INTERFACE;
+  },
+
+  /* nsIModule */
+  getClassObject : function mod_gch(compMgr, cid, iid)
+  {
+    if (cid.equals(clh_CID))
+      return myAppHandler.QueryInterface(iid);
+
+    throw Components.results.NS_ERROR_NOT_REGISTERED;
+  },
+
+  registerSelf : function mod_regself(compMgr, fileSpec, location, type)
+  {
+    compMgr.QueryInterface(nsIComponentRegistrar);
+
+    compMgr.registerFactoryLocation(clh_CID,
+                                    "myAppHandler",
+                                    clh_contractID,
+                                    fileSpec,
+                                    location,
+                                    type);
+
+    var catMan = Components.classes["@mozilla.org/categorymanager;1"].
+      getService(nsICategoryManager);
+    catMan.addCategoryEntry("command-line-handler",
+                            clh_category,
+                            clh_contractID, true, true);
+  },
+
+  unregisterSelf : function mod_unreg(compMgr, location, type)
+  {
+    compMgr.QueryInterface(nsIComponentRegistrar);
+    compMgr.unregisterFactoryLocation(clh_CID, location);
+
+    var catMan = Components.classes["@mozilla.org/categorymanager;1"].
+      getService(nsICategoryManager);
+    catMan.deleteCategoryEntry("command-line-handler", clh_category);
+  },
+
+  canUnload : function (compMgr)
+  {
+    return true;
+  }
+};
+
+/* The NSGetModule function is the magic entry point that XPCOM uses to find what XPCOM objects
+ * this component provides
+ */
+function NSGetModule(comMgr, fileSpec)
+{
+  return myAppHandlerModule;
+}
+
-- cgit v1.2.3-54-g00ecf