2 * Operaでも非同期リクエストが並列処理できる img-JSONP
3 * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
5 * iframe を使った jsonp の読み込みエラー判定の記事、
6 * JSONPのエラーを判断する は web archive でも観れない
7 * http://d.hatena.ne.jp/yuushimizu/20090128/1233146321
8 * TODO JSONPの動的取得+エラー処理
9 * http://d.hatena.ne.jp/NeoCat/20110206/1296934235
11 * Safari が JavaScript ファイルを動的ロードできない件
12 * http://www.bricklife.com/weblog/000618.html
14 * IE9でiframe内で遷移した場合window.parentのメソッドを呼べない
15 * http://kozo002.blogspot.jp/2012/07/ie9iframewindowparent.html
17 * IE6(IETester,localhost) で動かない,
23 cb : function( accessKey, jsonString, time, opt_json2FileSize ){
24 if( accessKey !== X_NET_JSONP_ACCESS_KEY || !X_NET_JSONPWrapper._busy ) return;
26 X_NET_JSONPWrapper._busy = false;
30 type : jsonString ? X.Event.SUCCESS : X.Event.ERROR,
31 data : jsonString //window.JSON ? JSON.parse( jsonString ) : eval( jsonString )
34 console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
38 function X_Net_JSONP_reciveMessage( data ){
42 var X_NET_JSONP_ACCESS_KEY = Math.random(),
44 X_NET_JSONP_SEND_MSG_KEY = X_System.message( 'X.Net.JSONP', X_Net_JSONP_reciveMessage ),
46 X_NET_JSONP_NinjaIframe,
48 X_Net_JSONP_onloadCount;
52 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
53 var json2Path = 'js/libs/json2.js',
54 json2FileSize = 18103,
57 X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
59 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
60 // TODO postMessage の利用
61 // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
62 // http://d.hatena.ne.jp/cnrd/20080518/1211099169
63 // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい
67 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
69 'onunload=function(){im.onload=im.onerror=""};',
71 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw', window[ 'JSON' ] ? json2FileSize : 0 ,');nw=0}}',
73 '<script id="jp"></script>',
74 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
76 X_Net_JSONP_onloadCount = 2;
80 // JavaScriptでunicode文字列をunescapeする
81 // http://perutago.seesaa.net/article/202801583.html
83 // http://blog.livedoor.jp/dankogai/archives/51503830.html
84 // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
85 // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
86 '<script id="jp"></script>',
88 'onunload=function(){clearTimeout(id)};',
90 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o).replace(/\\\\u([a-fA-F0-9]{4})/g,function(a,b){return String.fromCharCode(parseInt(b,16))}),-nw)}',
91 //'function cb(o){if(nw){nw-=+new Date;postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+parent.JSON.stringify(o).replace(/\\\\u([a-fA-F0-9]{4})/g,function(a,b){return String.fromCharCode(parseInt(b,16))}),"*");nw=0}}',
92 'function tm(){jp.src="', url ,'";nw=+new Date}',
93 'id=setTimeout(tm,16);',
96 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
98 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
100 '<script src="', url, '"></script>' */
102 X_Net_JSONP_onloadCount = 2;
106 '<script id="jp"></script>',
108 'onunload=function(){clearTimeout(id)};',
109 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
110 'function tm(){jp.src="', url ,'";nw=+new Date}',
111 'id=setTimeout(tm,16);',
114 X_Net_JSONP_onloadCount = 2;
116 if( window[ 'JSON' ] ){
120 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
121 //'function cb(o){if(nw){nw-=+new Date;parent.postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
123 '<script src="', url, '"></script>'
125 X_Net_JSONP_onloadCount = 1;
127 if( X_UA.IE4 || X_UA.MacIE ){
129 '<script id="jn"></script>',
130 '<script id="jp"></script>',
132 'onunload=function(){clearTimeout(id)};',
133 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
134 'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
135 'id=setTimeout("t1()",16);',
136 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
139 X_Net_JSONP_onloadCount = 3;
141 if( X_UA.IE < 8 ){ // ie5-7
143 '<script id="jn"></script>',
144 '<script id="jp"></script>',
146 'onunload=function(){clearTimeout(id)};',
147 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
148 'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
149 'id=setTimeout(t1,16);',
150 'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
153 X_Net_JSONP_onloadCount = 3;
157 'function cb(o){if(nw){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw,', json2FileSize, ');nw=0}}',
160 '<script src="', json2Path, '"></script>',
161 '<script src="', url, '"></script>'
163 X_Net_JSONP_onloadCount = 1;
166 X_NET_JSONP_NinjaIframe
167 .refresh( html.join( '' ) )
168 .listen( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
172 function X_NET_JSONP_iframeListener( e ){
174 case X.Event.SUCCESS :
175 console.log( 'iframe onload, but' );
176 if( ++X_NET_JSONPWrapper._onloadCount < X_Net_JSONP_onloadCount ) return;
177 // TODO callback が無ければ error
178 X_NET_JSONPWrapper.asyncDispatch( 1000, X.Event.ERROR );
181 console.log( 'iframe onerror' );
182 X_NET_JSONPWrapper.asyncDispatch( X.Event.ERROR );
185 X_NET_JSONP_NinjaIframe.unlisten();
186 return X.Callback.UN_LISTEN;
189 // TODO extend NinjaIframe
190 X_NET_JSONPWrapper = X_Class_override(
191 new X.EventDispatcher(),
198 load : function( url, data, timeout ){
200 X_NET_JSONP_loadScriptInNinjaIframe( url );
206 this._canceled = true;
210 this._busy = this._canceled = false;
211 this._onloadCount = 0;