Greasemonkey のソースをちょっと読む

Greasemonkey の編集に Carbon Emacs を使うで書きました、エディタを開くあたりのコードがどうも気になりまして、ソースをちょっと見てみようかと思いました。

http://greasemonkey.devjavu.com/ にあるように、ソースをチェックアウト。スクリプトがついているのでビルドもしてみます。


[ihara@Macintosh.local:~/work] $ svn co http://svn.devjavu.com/greasemonkey/
[ihara@Macintosh.local:~/work] $ greasemonkey/trunk/src
[ihara@Macintosh.local:~/work/greasemonkey/trunk/src] $ chmod +x build.sh
[ihara@Macintosh.local:~/work/greasemonkey/trunk/src] $ ./build.sh
[ihara@Macintosh.local:~/work/greasemonkey/trunk/src] $ l *.xpi
-rw-r--r--  1 ihara  ihara  62853  1  3 14:07 greasemonkey-0.8.20080103.0.xpi

さくっとでけました。

edit あたりをキーワードに探していくとどうも trunk/src/chrome/chromeFiles/content/utils.js の openInEditor が該当の関数っぽいような。

JAVASCRIPT:
  1. function openInEditor(aFile, promptTitle) {
  2.   var editor, editorPath;
  3.   try {
  4.     editorPath = GM_prefRoot.getValue("editor");
  5.   } catch(e) {
  6.     GM_log( "Failed to get 'editor' value:" + e );
  7.     if (GM_prefRoot.exists("editor")) {
  8.       GM_log("A value for 'editor' exists, so let's remove it because it's causing problems");
  9.       GM_prefRoot.remove("editor");
  10.     }
  11.     editorPath = false;
  12.   }
  13.   if (editorPath) {
  14.     // check whether the editor path is valid
  15.     GM_log("Try editor with path " + editorPath);
  16.     editor = Components.classes["@mozilla.org/file/local;1"]
  17.                        .createInstance(Components.interfaces.nsILocalFile);
  18.     editor.followLinks = true;
  19.     editor.initWithPath(editorPath);
  20.   } else {
  21.     var nsIFilePicker = Components.interfaces.nsIFilePicker;
  22.     var filePicker = Components.classes["@mozilla.org/filepicker;1"]
  23.                                .createInstance(nsIFilePicker);
  24.  
  25.     filePicker.init(window, promptTitle, nsIFilePicker.modeOpen);
  26.     filePicker.appendFilters(nsIFilePicker.filterApplication);
  27.     filePicker.appendFilters(nsIFilePicker.filterAll);
  28.  
  29.     if (filePicker.show() != nsIFilePicker.returnOK) {
  30.       return false;
  31.     }
  32.     editor = filePicker.file;
  33.     GM_log("User selected: " + editor.path);
  34.     GM_prefRoot.setValue("editor", editor.path);
  35.   }
  36.  
  37.   if (editor.exists() && editor.isExecutable()) {
  38.     try {
  39.       GM_log("launching ...");
  40.  
  41.       var process = Components.classes["@mozilla.org/process/util;1"]
  42.                               .getService(Components.interfaces.nsIProcess);
  43.       process.init(editor);
  44.       process.run(false, // non-blocking
  45.                   [aFile.path],
  46.                   1); // number of arguments in second param
  47.       return true;
  48.     } catch (e) {
  49.       GM_log("Failed to launch editor: " + e, true);
  50.     }
  51.   } else {
  52.     GM_log("Editor '" + editorPath + "' does not exist or isn't executable. " +
  53.            "Put it back, check the permissions, or just give up and reset " +
  54.            "editor using about:config", true);
  55.   }
  56.   return false;
  57. };

GM_log の出力を Firefox のエラーコンソールじゃなくて Firebug のコンソールに出しましょうってことで about:config から showChrome あたりで showChromeErrors と showChromeMessage の値をそれぞれ true に変更、んで GM_log('...', true) あたりを埋め込みながら追っていくと、上のソースの 43 行目、process.init(editor) でこけている模様。

Code snippets:Running applications とか nsIProcess あたりを見てみるものの、いまいちわからず。

って、前は少なくともエディタ起動してたのに、この落としてきたソースだと起動すらしないよねって、このソースのバージョンは 0.8、リリース版は 0.7 っつう話ですか。アガガ。

さらに探してみると、openInEditor not working on OS X ってバグを発見。openInEditor 動かんっつってますね。チケット 59 の実装に戻そうみたいな話なので、そのパッチを適用してみると、無事エディタ起動するようになりました。

ハー良かった、てか、実際プロセスを起動するあたりを見たかったのでもうちょっと深いところにいかないとダメみたいです。ムー。

というわけで期せずして手元の Greasemonkey は 0.8 に上がりました。ハイ。


About this entry