前置きが長くなったがやりたいことはタイトルの通りで、KeySnail :: Add-ons for Firefoxを使って実現できた。keysnailでは自前のJavaScriptにキーを割り当てる機能があるので、あとはハイパーリンクとしてコピーするロジックを書けばよい。Firefoxの仕様はほとんど知らないので苦労しそうかと思ったところ、運がよいことにほぼそのもののコードがUsing the Clipboard - MDCにあった。この中にある copyLinkToClipboard を少しだけ書き換えて以下のコードを作った。
function (ev, arg) {
var w = window._content;
var d = w.document;
var copyLabel = d.title;
var copyURL = d.location.href;
var textUnicode = copyURL;
var textHtml = ("<a href=\"" + copyURL + "\">" + copyLabel + "</a>");
// make a copy of the Unicode
var str = Components.classes["@mozilla.org/supports-string;1"].
createInstance(Components.interfaces.nsISupportsString);
if (!str) return false; // couldn't get string obj
str.data = textUnicode; // unicode string?
// make a copy of the HTML
var htmlstring = Components.classes["@mozilla.org/supports-string;1"].
createInstance(Components.interfaces.nsISupportsString);
if (!htmlstring) return false; // couldn't get string obj
htmlstring.data = textHtml;
// add Unicode & HTML flavors to the transferable widget
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, textUnicode.length * 2); // *2 because it's unicode
trans.addDataFlavor("text/html");
trans.setTransferData("text/html", htmlstring, textHtml.length * 2); // *2 because it's unicode
// copy the transferable widget!
var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].
getService(Components.interfaces.nsIClipboard);
if (!clipboard) return false; // couldn't get the clipboard
clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
}
あとは次の手順でKeySnailを設定する
- KeySnailの設定を開き [キーバインド] を選択
- [追加] - [オリジナルのコマンド] を選択
- コマンド名は適当に「Copy as HyperLink」とでもしておく
- モードはGlobal or Viewに設定
- 関数に上記のコードを貼り付ける
- 「キーバインド一覧表示を戻る」を押下して一覧に戻り、好みのキーを割り当てる
- 「変更の反映」を押下
0 コメント:
コメントを投稿