久次です。
Firefoxの機能拡張が便利なのですが、ついいろいろと入れているとメモリが多少肥大する模様です。すこしでも軽減しようと、Javascriptで代用できるものか、どうか試してみました。
ただ、JSオンリーだけだとどうしても「拡張」としての限界があるので、一応1つだけ、keyconfigという機能拡張をいれることにします(本末転倒ですが(笑))。これは、コマンドをショートカットキーアサインできるとういものですが、無い機能は、Javascriptとして書くことが可能です。
電網探題: keyconfig 20060828.1 日本語版たとえばよく編集で使う機能としてページのタイトルとURLをクリップボードにコピーしたい、ということがあり、それにはこれまでCopy URL+という拡張をつかってました。
電網探題: Copy URL + 1.3.2 日本語版 第二版
ですが、これはkeyconfigと以下のスクリプトで実現できます。
// ウィンドウ取得
var focusedWindow = document.commandDispatcher.focusedWindow;
var url = focusedWindow.location.href;
title = focusedWindow.document.title;
copyToClipboard(title + "\r\n" + url + "\r\n");
// クリップボードへコピー
function copyToClipboard(copyThis) {
var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
if (!str) return false; // couldn't get thingamajig
str.data = copyThis; // unicode string?
var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
if (!trans) return false; //no transferable widget found
trans.addDataFlavor("text/unicode");
trans.setTransferData("text/unicode", str, copyThis.length*2); // *2 cuz it's unicode
var clipid=Components.interfaces.nsIClipboard;
var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
if (!clip) return false; // couldn't get the clipboard
clip.setData(trans, null, clipid.kGlobalClipboard);
return true;
}
これで、機能拡張を1個減らせました。
最終的には拡張keyconfigとfirebugとGreaseMonkeyだけでなんとかならないのかなぁ、と思ったりしています。
なお、クリップボードを実現するコードはこの拡張を参考にさせていただきました。作者の方に感謝です。
functions for keyconfig :: Firefox Add-ons
Firefox Extension: functions for keyconfig



