userChrome.js のメモ

自分でもまだよく分かってない部分があるのだけどとりあえずメモしておく。
でも参考ページを直接見た方が早いかもしれない…。

userChorome.js って何?

userChrome.js は一言でいうなら「Firefox 自体の見た目を変えるためのスクリプト」。
Greasemonkey がサイトの見た目を変えられることに似ている。
朝顔日記 - Firefox の拡張機能、userChrome.js の私の使い方Firefox Help: 設定ファイルの編集を読むとイメージが掴みやすいと思う。

導入方法

ITmedia エンタープライズ:Firefoxを鍛え直せ! フォクすけブートキャンプ:第4日目:クラフトマンシップを持て――userChrome.jsが分かりやすい。

  • userChrome.js 0.8をインストールする。
    • インストールすると、Firefox のプロファイルフォルダの chrome 以下に userChrome.js が作成される。
  • Sub-Script/XUL 修正版 (以下) を userChrome.js に上書きする。
/* :::::::: Sub-Script/XUL Loader ::::::::::::::: */


// automatically includes all files ending in .uc.js and .uc.xul from the profile's chrome folder


if (location != "chrome://browser/content/browser.xul") throw "stop";

(function() {
  var getURLSpecFromFile = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler).getURLSpecFromFile;
  var chromeDir = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("UChrm", Components.interfaces.nsILocalFile);
  var files = chromeDir.directoryEntries.QueryInterface(Components.interfaces.nsISimpleEnumerator);
  var xul_files = [];
  
  while (files.hasMoreElements())
  {
    var file = files.getNext().QueryInterface(Components.interfaces.nsIFile);
    if (/\.uc\.js$/i.test(file.leafName))
    {
      setTimeout(function(aFile) {
        Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader).loadSubScript(getURLSpecFromFile(aFile));
      }, 0, file);
    }
    else if (/(^userChrome|\.uc)\.xul$/i.test(file.leafName))
    {
      xul_files.push(file);
    }
  }
  
  setTimeout(function() {
    if (xul_files.length > 0)
    {
      document.loadOverlay(getURLSpecFromFile(xul_files.shift()), null);
      setTimeout(arguments.callee, 0);
    }
  }, 0);
})();

使い方

  • 基本的に userChrome.js はいじらない。
  • その代わり、hogehoge.uc.js のような uc.js ファイルを chorome に追加していくことで Firefox を拡張する。

userChrome.js について調べようと思うまで経緯

  • Greasemonkeyスクリプトをインストールしまくってて、だんだんどれが何をしているかを把握しづらくなってきた。
  • 特に最近は LDR 関連のものがたくさん公開されていて、面白そうだからとドンドン追加していってるためエントロピー増大しまくりんぐ!
  • 一応、適用される範囲の広い順に手動で整理してみたんだけど、新しいの入れるたびに整理し直すのはすごい面倒だ。
  • この問題は、Greasemonkey の管理画面にソート機能がついてないからだと気付く。
  • でも管理画面ってどうやったらいじれるんだろう? -> userChrome.js