2005 | 10 | 11 | 12 |
2006 | 01 | 02 | 03 | 04 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 02 | 03 | 04 | 05 | 06 | 11 | 12 |
2008 | 01 | 09 | 10 |
2009 | 01 | 02 | 03 | 06 |
2006 | 01 | 02 | 03 | 04 | 07 | 08 | 09 | 10 | 11 | 12 |
2007 | 02 | 03 | 04 | 05 | 06 | 11 | 12 |
2008 | 01 | 09 | 10 |
2009 | 01 | 02 | 03 | 06 |
2008/10/16 Thu
■ [UserJS]onload時のfocusを無効にする

// ==UserScript==
// @name kill onload focus
// @namespace http://orera.g.hatena.ne.jp/takef/
// @include *
// ==/UserScript==
(function () {
var inputFocusFunc = HTMLInputElement.prototype.focus;
var textareaFocusFunc = HTMLInputElement.prototype.focus;
opera.addEventListener('BeforeScript', function (e) {
HTMLInputElement.prototype.focus = function(){};
HTMLTextAreaElement.prototype.focus = function(){};
}, false);
document.addEventListener('load', function (e) {
setTimeout(function() {
HTMLInputElement.prototype.focus = inputFocusFunc;
HTMLTextAreaElement.prototype.focus = textareaFocusFunc;
}, 500);
}, false);
})();
Twitterのスクリプトが変更されるたびにその場しのぎで対応するのが面倒になったので、どんなサイトでも対応できるようなのを作った。
やってることは難しくなくて、スクリプトの実行前にfocusをダミーの関数に置き換えて、onloadの0.5秒後に元に戻しているだけ。
追記(2008/10/19)
いただいたコメントを元に修正しました。
// ==UserScript==
// @name kill onload focus
// @namespace http://orera.g.hatena.ne.jp/takef/
// @include *
// ==/UserScript==
(function () {
var inputFocusFunc = HTMLInputElement.prototype.focus;
var textareaFocusFunc = HTMLTextAreaElement.prototype.focus;
HTMLInputElement.prototype.focus = function(){};
HTMLTextAreaElement.prototype.focus = function(){};
document.addEventListener('load', function (e) {
setTimeout(function() {
HTMLInputElement.prototype.focus = inputFocusFunc;
HTMLTextAreaElement.prototype.focus = textareaFocusFunc;
}, 500);
}, false);
})();
トラックバック - http://orera.g.hatena.ne.jp/takef/20081016
は
var textareaFocusFunc = HTMLTextAreaElement.prototype.focus;
の間違いですね。
これでやると、はてなダイアリー (例えばこのページなど) で、onload 後に focus 関数が元に戻ってないと思うのですが、再現しますか? 他のページでは大丈夫っぽいんですけど。
この場合、BeforeScriptを使う必要がないかなと思います。
delete で自分が定義したプロパティを消すことができるので、こんな感じにすることもできます。
HTMLInputElement.prototype.focus =
HTMLTextAreaElement.prototype.focus = Function();
window.addEventListener('load', function () {
setTimeout(function() {
delete HTMLInputElement.prototype.focus;
delete HTMLTextAreaElement.prototype.focus;
}, 500);
}, false);
http://d.hatena.ne.jp/Griever/20081017/1224261825
わ、はずかしいミスですね。ご指摘感謝。
>os0xさん
おお、確かにBeforeScriptは使う必要がないですね。
そしてJSONPのことは想定外というか忘れてました。
ありがとうございます。
ちょっと気になったんですが、delete使ってしまうと元々あったfocusが消えてしまわないですか?
それにしてもコメント入力欄が小さすぎる・・・
あとでなんとかしよう。