Extension開発でRengeの拡張に失敗
Firefoxの拡張機能開発でRengeを拡張して便利なメソッドを作ろうかと思いチャレンジしたところ行き詰った。
普通にRange.prototypeを拡張てFirebug上で試したところ期待通りの動きをしたので拡張機能として組み込んでみたら動かない…、何故だ!
とりあえず下記のコードを作ってみた。
Range.prototype.testMethod= function() { return "test method return"; }; log.trace("cp01:"); log.trace(Range.prototype.testMethod); var focusedWindow = document.commandDispatcher.focusedWindow; var s=focusedWindow.getSelection(); for(var i=0; i<s.rangeCount; i++){ var range=s.getRangeAt(i); log.debug("cp02:"); log.debug(range instanceof Range); log.debug("cp03:"); log.debug(range.testMethod()); }
でFirefoxをデバックモードで立ち上げて動かしてみたら下記のログが。
[TRACE : helloworld]cp01: [TRACE : helloworld]function () { return "test method return"; } [DEBUG : helloworld]cp02: [DEBUG : helloworld]true [DEBUG : helloworld]cp03: エラー: range.testMethod is not a function ソースファイル: chrome://helloworld/content/overlay.js 行: 105
やっぱ、拡張されてないっぽいな。instanceofでもtrueが帰ってきてるし、コードも参照できる。
なのに「range.testMethod is not a function」かよ。
ひょっとすると、prototypeがそもそもダメなのかと思い。普通にObjectで試してみた。
Object.prototype.testMethod= function() { return "test method return"; }; log.trace("cp01:"); var o = new Object(); log.debug("cp02:"); log.debug(o.testMethod());
で同じように実行。
[TRACE : helloworld]cp01: [DEBUG : helloworld]cp02: [DEBUG : helloworld]test method return
アレ…イケた。なんかセキュリティ絡みの設定が必要なのか??
サッパリだ。誰かHelp me!