OSDN Git Service

Version 0.6.178, fix X.KB for IE5-, X.HTMLAudio for ChromeWV & AOSP.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 02_XNetJSONP.js
1 //{+jsonp"jsonpによるajax"(jsonpによるクロスドメイン通信。)[+net,+ninjaiframe]
2
3 /*
4  * Operaでも非同期リクエストが並列処理できる img-JSONP
5  * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
6  * 
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
11  * 
12  * Safari が JavaScript ファイルを動的ロードできない件
13  * http://www.bricklife.com/weblog/000618.html
14  * 
15  * IE9でiframe内で遷移した場合window.parentのメソッドを呼べない 
16  * http://kozo002.blogspot.jp/2012/07/ie9iframewindowparent.html
17  * 
18  * IE6(IETester,localhost) で動かない,
19  * 
20  * TODO iframe 内を他ドメインにして jsonp のセキュリティを強化。他ドメインに jsonp を読み込んで 正しい JSON か?検証後にフレーム間通信で戻す。
21  */
22
23         // TODO chashe
24         // TODO iframe useful or not. TODO check dynamicIframe
25         // TODO file: では http: は使えない
26
27 X_TEMP.X_JSONP_cb = function( accessKey, jsonString, time, opt_json2FileSize ){
28                         if( accessKey !== X_JSONP_ACCESS_KEY || !X_JSONP._busy ) return;
29                         
30                         X_JSONP._busy = false;
31                         
32                         X_JSONP
33                                 [ 'asyncDispatch' ]( {
34                                         type     : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
35                                         response : X_JSON_parseTrustableString( jsonString )
36                                 } );
37                         
38                         X_JSONP_errorTimerID && X_Timer_remove( X_JSONP_errorTimerID );
39                         
40                         console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
41         };
42
43 var X_JSONP_ACCESS_KEY = Math.random(),
44         
45         X_JSONP_maxOnloadCount,
46         
47         X_JSONP_onloadCount = 0,
48         
49         X_JSONP_errorTimerID;
50
51 X_TEMP.X_JSONP_init = function(){
52         X[ 'Net' ][ '__json_cb__' ] = X_TEMP.X_JSONP_cb;
53         
54         X_JSONP = X_Class_override( X_NinjaIframe(), X_TEMP.X_JSONP_params );
55         
56         delete X_TEMP.X_JSONP_cb;
57         delete X_TEMP.X_JSONP_init;
58         delete X_TEMP.X_JSONP_params;
59         
60         return X_JSONP;
61 };
62
63 X_TEMP.X_JSONP_params = {
64         
65                         _busy         : false,
66                         _canceled     : false,
67                         
68                         load : function( option ){
69                                 //createURL
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,
76                                         html;
77                                 
78                                 // file プロトコルで外部アクセスの禁止
79                                 if( !X_URL_isSameProtocol( url ) ){
80                                         return X_JSONP[ 'asyncDispatch' ]( X_EVENT_ERROR );
81                                 };
82                                 
83                                 url = X_URL_create( url, params );
84                                 
85                                 if( !callback && !( callback = X_URL_paramToObj( url.split( '?' )[ 1 ] )[ 'callback' ] ) ){
86                                         url += '&callback=cb';
87                                         callback = 'cb';
88                                 };
89                                 
90                                 charset = charset ? ' charset="' + charset + '"' : '';
91                                 
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とか)にはまだ対応してないみたい 
96                         
97                                 if( X_UA[ 'Opera' ] ){
98                                         html = [
99                                                 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
100                                                 '<script>',
101                                                         'onunload=function(){im.onload=im.onerror=""};',
102                                                         'nw=+new Date;',
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}}',
104                                                 '</script>',    
105                                                 '<script', charset, ' id="jp"></script>',
106                                                 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
107                                         ];
108                                         X_JSONP_maxOnloadCount = 2;
109                                 } else
110                                 if( X_UA[ 'IE8' ] ){
111                                         html = [
112                                                 '<script', charset, ' id="jp"></script>',
113                                                 '<script>',
114                                                         'onunload=function(){clearTimeout(id)};',
115                                                         'nw=0;', // なぜか必要,,,
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);',
120                                                 '</script>'
121                                                 
122                                                 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
123                                                 '<script>',
124                                                         'function ', callback, '(o){window.parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
125                                                 '</script>',
126                                                 '<script src="', url, '"></script>' */
127                                         ];
128                                         X_JSONP_maxOnloadCount = 2;
129                                 } else
130                                 if( X_UA[ 'IE9' ] ){
131                                         html = [
132                                                 '<script', charset, ' id="jp"></script>',
133                                                 '<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);',
138                                                 '</script>'
139                                         ];
140                                         X_JSONP_maxOnloadCount = 2;
141                                 } else
142                                 if( window[ 'JSON' ] ){
143                                         html = [        
144                                                 '<script>',
145                                                         'nw=+new Date;',
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}}',
148                                                 '</script>',
149                                                 '<script', charset, ' src="', url, '"></script>'
150                                         ];
151                                         X_JSONP_maxOnloadCount = 1;
152                                 } else
153                                 if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
154                                         html = [
155                                                 '<script id="jn"></script>',
156                                                 '<script', charset, ' id="jp"></script>',
157                                                 '<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)}}',
163                                                 '</script>'
164                                         ];
165                                         X_JSONP_maxOnloadCount = 3;
166                                 } else
167                                 if( X_UA[ 'IE' ] < 8 ){ // ie5-7
168                                         html = [
169                                                 '<script id="jn"></script>',
170                                                 '<script', charset, ' id="jp"></script>',
171                                                 '<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)}}',
177                                                 '</script>'
178                                         ];
179                                         X_JSONP_maxOnloadCount = 3;
180                                 } else {
181                                         html = [
182                                                 '<script>',
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}}',
184                                                         'nw=+new Date;',
185                                                 '</script>',
186                                                 '<script src="', json2Path, '"></script>',
187                                                 '<script', charset, ' src="', url, '"></script>'
188                                         ];
189                                         X_JSONP_maxOnloadCount = 2;
190                                 };
191                                 
192                                 X_JSONP
193                                         [ 'refresh' ]( html.join( '' ) )
194                                         [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_JSONP_iframeListener );
195                                                         
196                                 X_JSONP._busy = true;
197                         },
198                         
199                         cancel : function(){
200                                 X_JSONP.reset();
201                                 X_JSONP._canceled = true;
202                         },
203                         
204                         reset : function(){
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;
210                         }
211                 };
212
213 function X_JSONP_iframeListener( e ){
214         switch( e.type ){
215                 case 'ninjaload' :
216                         console.log( 'iframe onload, but ' + X_JSONP_onloadCount + ' < ' + X_JSONP_maxOnloadCount );
217                         if( ++X_JSONP_onloadCount < X_JSONP_maxOnloadCount ) return;
218                         
219                         // TODO callback が無ければ error -> timeout を観る?
220                         X_JSONP_errorTimerID = X_JSONP[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
221                         break;
222                 case 'ninjaerror' :
223                         console.log( 'iframe onerror' );
224                         X_JSONP[ 'asyncDispatch' ]( X_EVENT_ERROR );
225                         break;
226         };
227         return X_CALLBACK_UN_LISTEN;
228 };