クロスドメインなiframeのcontentWindowにアクセスできない (Opera) →というのは一部誤りでした

これのソースが読みやすくて応用が効きそうだったので遊んでたら、エラーが出て先に進めなかった。

(ちなみにニコニコ動画のアカウントが無いので元の UserJS は試してません)

エラーの内容はこれ。

name: ReferenceError
message: Security error: attempted to read protected variable

以下の行で引っ掛かってるっぽい。

object.contentDocument.postMessage(encodeURIComponent([

試しにこんなブックマークレットで実験してみたら同じエラーが出た。

javascript:(function(){
var f=document.createElement('iframe');
f.src='http://google.com/';
document.body.appendChild(f);
f.addEventListener('load',function(){
  try{alert(f.contentWindow);}catch(e){alert(e);}
},false);
})()

contentWindow を contentDocument でもだめ。
http://www.google.com/ から http://google.com/ を開いたときには通るっぽい。

元のスクリプトが書かれたときは Opera 9.2x だったはずなので、Opera 9.5 で仕様が変わったのかも。

Tombloo みたいなことが出来そうと思ったのに、めんどくさいなあ。

コメント頂いたように、こういう感じにすれば通るみたい。

javascript:(function(){
var f=document.createElement('iframe');
f.src='http://ss-o.net/xjs/postMessage.html';
document.body.appendChild(f);
f.addEventListener('load',function(){
  try{f.contentWindow.postMessage(p,'http://ss-o.net');}catch(e){alert(e);}
},false);
})()

iframe の contentWindow を返させようとするとセキュリティ上のエラーが出るけれども、contentWindow.postMessage は通るわけか。

postMessage の targetOrigin というのがちょっとわかりにくいなあ。src で指定してもリダイレクトされているかもしれないから、ちゃんとドメインを絞れよという意味なのかな。