12/17

Ctrl+Enterでフォーム送信するGreasemonkeyスクリプト

高速インターフェースさんのデモが適用だとか実行にいちいちCtrl+Enterを仕込んでるのが便利なんで、普通のフォームでもtextareaにCtrl+Enterで送信できるようにするだけのGreasemonkeyスクリプト。今まで無意識にCtrl+Enter打って送信しようとしてたことが何度もあったので。

Greasemonkey Script: Submit on Ctrl-Enter

素朴な作りです。いろいろ細工のあるフォームではなんかあるかも。

HTMLCollection.prototypeforEachを定義してやって、

(function() {
    unsafeWindow.HTMLCollection.prototype.forEach = function(callback, thisObject)
    {
        var thisObject = thisObject || this;
        for (var i = 0, len = this.length; i < len; ++i)
            callback.call(this, this[i], i, this);
    };

    document.getElementsByTagName('form').forEach(function(form)
    {
        form.getElementsByTagName('textarea').forEach(function(textarea)
        {
            textarea.addEventListener(
                'keydown',
                function(event) {
                    if (event.ctrlKey && event.keyCode == 13) form.submit();
                },
                true
            );
        });
    })
})();

とやるつもりだったんだけど(かっこいい!)、これじゃ動かなかった。調べてみたらセキュリティ絡みでgetElementsByTagName()の返り値はXPCNativeWrapperとか言うのにラップされたHTMLCollectionで、いろいろと弄れないようになってる(もちろんFirefoxでの話)ようで断念。Avoid Common Pitfalls in Greasemonkeyにもその辺の話が載ってるみたい。そういえば脆弱性が発見されてからあんまりGreasemonkey触ってなかった気がするなぁ…。

(function(form) {
    return function() { ... }
})(form)

は中の関数中の変数formに現在のformの値を束縛、ってそんなこと書かなくてもいいか。

12/13

Toolbar Grippies 0.4

Firefox 1.5ベータから使ってたくせに拡張を対応させてなかったのでアップデートしました。手直しとかは殆どしてませんのであしからず。

Toolbar Grippies 0.4

海外からリンクされてるとプレッシャーが…。

19:58 | 0 writeback(s) | (8), (19) | ?B

recent entries

tags

syndication

powered by