Now browsing the archives for 10月, 2006.
[userChrome.js] 選択範囲内のすべてのURLをタブで開く、またはWeb検索する
選択範囲内にあるURLと思しき文字列をすべてタブで開くための、 userChrome.js 用スクリプトです。さらに、URLが含まれない場合はWeb検索します。URLかどうかの判定はかなり曖昧ですが、「ttp://」形式や「http://」が無い形式にも対応していますので、以下のような場合に有効です。
—
ttp://www.example.com/photo1.jpg
ttp://www.example.com/photo2.jpg
ttp://www.example.com/photo3.jpg
—
下記ではとりあえず関数として定義しています。
使い方は色々ありますが、userChrome.js マウスジェスチャからも利用可能です。
/**
* A user script for userChrome.js extension.
* @name Open all URLs in selection or Web-search
* @compatibility Firefox 2.0
* @author Gomita
* @version 2007.1.12
* @permalink http://www.xuldev.org/blog/?p=48
*/
function ucjsOpenOrSearchSelection()
{
// getBrowserSelection function couldn't be apply since it removes all line breaks.
var win = document.commandDispatcher.focusedWindow;
var sel = win.getSelection().toString();
if (!sel) return;
// process each lines
var flag = false;
sel = sel.split("
");
sel.forEach(function(str)
{
// extract strings which may be URL (not strict)
str = str.match(/([a-zA-Z0-9+$;?.%,!#~*/:@&=_-]+)/);
if (!str || str[1].indexOf(".") < 0 || str[1].length < 8 ) return;
str = str[1];
if ( str.indexOf("ttp://") == 0 ) str = "h" + str;
// Open in a background tab
gBrowser.loadOneTab(str, null, null, null, true, false);
flag = true;
});
// If the selection doesn't contain URL, we does Web-Search it.
if ( !flag )
BrowserSearch.loadSearch(sel, true);
}
ついでに All-in-one Gestures の「閉じたタブを元に戻す」ジェスチャで Firefox 本体の undoCloseTab 関数を呼び出すように変更。
aioUndoCloseTab = undoCloseTab;
[userChrome.js] 「最近閉じたタブ」のエントリを中クリックしてタブを開き直す
Firefox 2.0 ではメニューバーの [履歴] → [最近閉じたタブ] から、閉じたタブを開き直すことができる。しかし、[履歴] メニュー下に最高10個表示される履歴のエントリが中クリックによって新しいタブで開くことができるのに対し、[最近閉じたタブ] のエントリは中クリックでは開くことができず、左クリックのみとなる。
そこで、 userChrome.js で以下のような簡単なスクリプトを読み込ませることで、中クリックも可能なようにする。
(function()
{
document.getElementById("historyUndoPopup").addEventListener("click", function(event)
{
if (event.button == 1) event.originalTarget.doCommand();
}, false);
})();
なお、Firefox 3.0 では本体側で修正されるもよう。
nsIStringBundleService
JavaScript内でローカライズされた文字列(平たく言えば日本語)を使用するには、properties ファイルを xul:stringbundle 要素から参照して getString や getFormattedString によって文字列を取り出す方法がある。これは XUL Tutorial にも記載されているように基本的な方法である。
しかし、ユーザーインターフェースとは切り離されたプログラム的な制御がメインの JavaScript にてローカライズされた文字列を使用する場合(特に自前のXPCOMコンポーネントを実装する場合)、いちいち xul へ stringbundle 要素を配置するのは面倒であり、規模が大きくなるとややこしくなりやすい。こういう場合には nsIStringBundleService が便利である。このXPCOMサービスは createBundle メソッドによって指定したURIから properties ファイルを読み込んで nsIStringBundle 型のXPCOMオブジェクトを生成し、 GetStringFromName や formatStringFromName メソッドによってローカライズされた文字列を取り出したりすることができる。以下のサンプルにある getLocaleString 関数は、 BookmarksUtils を参考にして文字列の置換の有無に両対応したものである。
リファレンス:
Interface Reference – nsIStringBundleService
Interface Reference – nsIStringBundle
sample.properties
HELLO=こんにちは MY_NAME_IS=私の名前は%Sです。
sample.js
var FoxkehUtils = {
_stringBundle : null,
getLocaleString : function(aStringKey, aReplacements)
{
// 初めて呼び出された時に properties ファイルを読み込んで nsIStringBundle オブジェクト生成
if ( !this._stringBundle ) {
const BUNDLE_SVC = Components.classes['@mozilla.org/intl/stringbundle;1'].getService(Components.interfaces.nsIStringBundleService);
this._stringBundle = BUNDLE_SVC.createBundle("chrome://sample/locale/sample.properties");
}
try {
if ( !aReplacements )
// 置換なし
return this._stringBundle.GetStringFromName(aStringKey);
else
// 置換あり
return this._stringBundle.formatStringFromName(aStringKey, aReplacements, aReplacements.length);
} catch(ex) {
// 未定義の場合 fallout
return aStringKey;
}
},
say : function()
{
alert(this.getLocaleString("HELLO")); // こんにちは
alert(this.getLocaleString("MY_NAME_IS", ["フォクすけ"])); // 私の名前はフォクすけです。
},
}
FoxkehUtils.say();