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) で動かない,
19 * TODO iframe 内を他ドメインにして jsonp のセキュリティを強化。他ドメインに jsonp を読み込んで 正しい JSON か?検証後にフレーム間通信で戻す。
23 // TODO iframe useful or not. TODO check dynamicIframe
24 // TODO file: では http: は使えない
26 X[ 'Net' ][ 'JSONP' ] = {
27 'cb' : function( accessKey, jsonString, time, opt_json2FileSize ){
28 if( accessKey !== X_NET_JSONP_ACCESS_KEY || !X_NET_JSONPWrapper._busy ) return;
30 X_NET_JSONPWrapper._busy = false;
33 [ 'asyncDispatch' ]( {
34 type : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
35 data : window.JSON ? JSON.parse( jsonString ) : eval( 'var a=' + jsonString + ';a' )
38 X_Net_JSONP_errorTimerID && X_Timer_remove( X_Net_JSONP_errorTimerID );
40 console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
44 var X_NET_JSONP_ACCESS_KEY = Math.random(),
46 X_Net_JSONP_maxOnloadCount,
48 X_Net_JSONP_onloadCount = 0,
50 X_Net_JSONP_errorTimerID;
52 X_TEMP.X_NET_JSONP_init = function(){
54 delete X_TEMP.X_NET_JSONP_init;
56 return X_NET_JSONPWrapper = X_Class_override(
57 X[ 'Util' ][ 'NinjaIframe' ](),
63 load : function( option ){
65 var url = option[ 'url' ],
66 json2Path = 'js/libs/json2.js',
67 json2FileSize = 18103,
70 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
71 // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
72 // http://d.hatena.ne.jp/cnrd/20080518/1211099169
73 // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい
75 if( X_UA[ 'Opera' ] ){
77 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
79 'onunload=function(){im.onload=im.onerror=""};',
81 '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}}',
83 '<script id="jp"></script>',
84 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
86 X_Net_JSONP_maxOnloadCount = 2;
90 // JavaScriptでunicode文字列をunescapeする
91 // http://perutago.seesaa.net/article/202801583.html
93 // http://blog.livedoor.jp/dankogai/archives/51503830.html
94 // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
95 // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
96 '<script id="jp"></script>',
98 'onunload=function(){clearTimeout(id)};',
100 '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)}',
101 //'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}}',
102 'function tm(){jp.src="', url ,'";nw=+new Date}',
103 'id=setTimeout(tm,16);',
106 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
108 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
110 '<script src="', url, '"></script>' */
112 X_Net_JSONP_maxOnloadCount = 2;
116 '<script id="jp"></script>',
118 'onunload=function(){clearTimeout(id)};',
119 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
120 'function tm(){jp.src="', url ,'";nw=+new Date}',
121 'id=setTimeout(tm,16);',
124 X_Net_JSONP_maxOnloadCount = 2;
126 if( window[ 'JSON' ] ){
130 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
131 //'function cb(o){if(nw){nw-=+new Date;parent.postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
133 '<script src="', url, '"></script>'
135 X_Net_JSONP_maxOnloadCount = 1;
137 if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
139 '<script id="jn"></script>',
140 '<script id="jp"></script>',
142 'onunload=function(){clearTimeout(id)};',
143 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
144 'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
145 'id=setTimeout("t1()",16);',
146 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
149 X_Net_JSONP_maxOnloadCount = 3;
151 if( X_UA[ 'IE' ] < 8 ){ // ie5-7
153 '<script id="jn"></script>',
154 '<script id="jp"></script>',
156 'onunload=function(){clearTimeout(id)};',
157 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
158 'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
159 'id=setTimeout(t1,16);',
160 'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
163 X_Net_JSONP_maxOnloadCount = 3;
167 '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}}',
170 '<script src="', json2Path, '"></script>',
171 '<script src="', url, '"></script>'
173 X_Net_JSONP_maxOnloadCount = 2;
177 [ 'refresh' ]( html.join( '' ) )
178 [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
180 X_NET_JSONPWrapper._busy = true;
184 X_NET_JSONPWrapper.reset();
185 X_NET_JSONPWrapper._canceled = true;
189 X_NET_JSONPWrapper._busy = X_NET_JSONPWrapper._canceled = false;
190 X_Net_JSONP_onloadCount = 0;
191 X_NET_JSONPWrapper[ 'unlisten' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
192 X_NET_JSONPWrapper[ 'refresh' ]( '' );
193 X_Net_JSONP_errorTimerID && X_Timer_remove( X_Net_JSONP_errorTimerID );
199 function X_NET_JSONP_iframeListener( e ){
202 console.log( 'iframe onload, but ' + X_Net_JSONP_onloadCount + ' < ' + X_Net_JSONP_maxOnloadCount );
203 if( ++X_Net_JSONP_onloadCount < X_Net_JSONP_maxOnloadCount ) return;
205 // TODO callback が無ければ error -> timeout を観る?
206 X_Net_JSONP_errorTimerID = X_NET_JSONPWrapper[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
209 console.log( 'iframe onerror' );
210 X_NET_JSONPWrapper[ 'asyncDispatch' ]( X_EVENT_ERROR );
213 return X_Callback_UN_LISTEN;