1 //{+jsonp"jsonpによるajax"(jsonpによるクロスドメイン通信。)[+net,+ninjaiframe]
4 * Operaでも非同期リクエストが並列処理できる img-JSONP
5 * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
7 * iframe を使った jsonp の読み込みエラー判定の記事、
8 * https://web.archive.org/web/20120917100043/http://d.hatena.ne.jp/yuushimizu/20090128/1233146321
9 * TODO JSONPの動的取得+エラー処理
10 * http://d.hatena.ne.jp/NeoCat/20110206/1296934235
12 * Safari が JavaScript ファイルを動的ロードできない件
13 * http://www.bricklife.com/weblog/000618.html
15 * IE9でiframe内で遷移した場合window.parentのメソッドを呼べない
16 * http://kozo002.blogspot.jp/2012/07/ie9iframewindowparent.html
18 * IE6(IETester,localhost) で動かない,
20 * TODO iframe 内を他ドメインにして jsonp のセキュリティを強化。他ドメインに jsonp を読み込んで 正しい JSON か?検証後にフレーム間通信で戻す。
24 // TODO iframe useful or not. TODO check dynamicIframe
25 // TODO file: では http: は使えない
27 X_TEMP.X_JSONP_cb = function( accessKey, jsonString, time, opt_json2FileSize ){
28 if( accessKey !== X_JSONP_ACCESS_KEY || !X_JSONP._busy ) return;
30 X_JSONP._busy = false;
33 [ 'asyncDispatch' ]( {
34 type : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
35 response : X_JSON_parseTrustableString( jsonString )
38 X_JSONP_errorTimerID && X_Timer_remove( X_JSONP_errorTimerID );
40 console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
43 var X_JSONP_ACCESS_KEY = Math.random(),
45 X_JSONP_maxOnloadCount,
47 X_JSONP_onloadCount = 0,
51 X_TEMP.X_JSONP_init = function(){
52 X[ 'Net' ][ '__json_cb__' ] = X_TEMP.X_JSONP_cb;
54 X_JSONP = X_Class_override( X_NinjaIframe(), X_TEMP.X_JSONP_params );
56 delete X_TEMP.X_JSONP_cb;
57 delete X_TEMP.X_JSONP_init;
58 delete X_TEMP.X_JSONP_params;
63 X_TEMP.X_JSONP_params = {
68 load : function( option ){
70 var url = option[ 'url' ],
71 params = option[ 'params' ],
72 callback = option[ 'callbackName' ],
73 charset = option[ 'charset' ],
74 json2Path = window.RegExp ? 'js/libs/json2.js' : 'js/libs/json2_regfree.js',
75 json2FileSize = 18103,
78 // file プロトコルで外部アクセスの禁止
79 if( !X_URL_isSameProtocol( url ) ){
80 return X_JSONP[ 'asyncDispatch' ]( X_EVENT_ERROR );
83 url = X_URL_create( url, params );
85 if( !callback && !( callback = X_URL_paramToObj( url.split( '?' )[ 1 ] )[ 'callback' ] ) ){
86 url += '&callback=cb';
90 charset = charset ? ' charset="' + charset + '"' : '';
92 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
93 // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
94 // http://d.hatena.ne.jp/cnrd/20080518/1211099169
95 // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい
97 if( X_UA[ 'Opera' ] ){
99 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
101 'onunload=function(){im.onload=im.onerror=""};',
103 'function ', callback, '(o){if(nw){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw', window[ 'JSON' ] ? json2FileSize : 0 ,');nw=0}}',
105 '<script', charset, ' id="jp"></script>',
106 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
108 X_JSONP_maxOnloadCount = 2;
112 '<script', charset, ' id="jp"></script>',
114 'onunload=function(){clearTimeout(id)};',
116 'function ', callback, '(o){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',parent.X.JSON.stringify(o),-nw)}',
117 //'function ', callback, '(o){if(nw){nw-=+new Date;postMessage("', X_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}}',
118 'function tm(){jp.src="', url ,'";nw=+new Date}',
119 'id=setTimeout(tm,16);',
122 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
124 'function ', callback, '(o){window.parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
126 '<script src="', url, '"></script>' */
128 X_JSONP_maxOnloadCount = 2;
132 '<script', charset, ' id="jp"></script>',
134 'onunload=function(){clearTimeout(id)};',
135 'function ', callback, '(o){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
136 'function tm(){jp.src="', url ,'";nw=+new Date}',
137 'id=setTimeout(tm,16);',
140 X_JSONP_maxOnloadCount = 2;
142 if( window[ 'JSON' ] ){
146 'function ', callback, '(o){if(nw){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
147 //'function ', callback, '(o){if(nw){nw-=+new Date;parent.postMessage("', X_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
149 '<script', charset, ' src="', url, '"></script>'
151 X_JSONP_maxOnloadCount = 1;
153 if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
155 '<script id="jn"></script>',
156 '<script', charset, ' id="jp"></script>',
158 'onunload=function(){clearTimeout(id)};',
159 'function ', callback, '(o){nw-=new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
160 'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
161 'id=setTimeout("t1()",16);',
162 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
165 X_JSONP_maxOnloadCount = 3;
167 if( X_UA[ 'IE' ] < 8 ){ // ie5-7
169 '<script id="jn"></script>',
170 '<script', charset, ' id="jp"></script>',
172 'onunload=function(){clearTimeout(id)};',
173 'function ', callback, '(o){nw-=new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
174 'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
175 'id=setTimeout(t1,16);',
176 'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
179 X_JSONP_maxOnloadCount = 3;
183 'function ', callback, '(o){if(nw){nw-=new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw,', json2FileSize, ');nw=0}}',
186 '<script src="', json2Path, '"></script>',
187 '<script', charset, ' src="', url, '"></script>'
189 X_JSONP_maxOnloadCount = 2;
193 [ 'refresh' ]( html.join( '' ) )
194 [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_JSONP_iframeListener );
196 X_JSONP._busy = true;
201 X_JSONP._canceled = true;
205 X_JSONP._busy = X_JSONP._canceled = false;
206 X_JSONP[ 'unlisten' ]( [ 'ninjaload', 'ninjaerror' ], X_JSONP_iframeListener );
207 X_JSONP[ 'refresh' ]( '' );
208 X_JSONP_errorTimerID && X_Timer_remove( X_JSONP_errorTimerID );
209 X_JSONP_errorTimerID = X_JSONP_onloadCount = 0;
213 function X_JSONP_iframeListener( e ){
216 console.log( 'iframe onload, but ' + X_JSONP_onloadCount + ' < ' + X_JSONP_maxOnloadCount );
217 if( ++X_JSONP_onloadCount < X_JSONP_maxOnloadCount ) return;
219 // TODO callback が無ければ error -> timeout を観る?
220 X_JSONP_errorTimerID = X_JSONP[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
223 console.log( 'iframe onerror' );
224 X_JSONP[ 'asyncDispatch' ]( X_EVENT_ERROR );
227 return X_CALLBACK_UN_LISTEN;