OSDN Git Service

Version 0.6.150, fix X.Net.
[pettanr/clientJs.git] / 0.6.x / js / 06_net / 02_XNetJSONP.js
1 /*
2  * Operaでも非同期リクエストが並列処理できる img-JSONP
3  * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
4  * 
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
10  * 
11  * Safari が JavaScript ファイルを動的ロードできない件
12  * http://www.bricklife.com/weblog/000618.html
13  * 
14  * IE9でiframe内で遷移した場合window.parentのメソッドを呼べない 
15  * http://kozo002.blogspot.jp/2012/07/ie9iframewindowparent.html
16  * 
17  * IE6(IETester,localhost) で動かない,
18  * 
19  * TODO iframe 内を他ドメインにして jsonp のセキュリティを強化。他ドメインに jsonp を読み込んで 正しい JSON か?検証後にフレーム間通信で戻す。
20  */
21
22         // TODO chashe
23         // TODO iframe useful or not. TODO check dynamicIframe
24         // TODO file: では http: は使えない
25
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;
29                         
30                         X_NET_JSONPWrapper._busy = false;
31                         
32                         X_NET_JSONPWrapper
33                                 [ 'asyncDispatch' ]( {
34                                         type : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
35                                         data : X_String_parseTrustedJsonString( jsonString )
36                                 } );
37                         
38                         X_Net_JSONP_errorTimerID && X_Timer_remove( X_Net_JSONP_errorTimerID );
39                         
40                         console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
41                 }
42 };
43
44 var X_NET_JSONP_ACCESS_KEY = Math.random(),
45         
46         X_Net_JSONP_maxOnloadCount,
47         
48         X_Net_JSONP_onloadCount = 0,
49         
50         X_Net_JSONP_errorTimerID;
51
52 X_TEMP.X_NET_JSONP_init = function(){
53         
54         delete X_TEMP.X_NET_JSONP_init;
55         
56         return X_NET_JSONPWrapper = X_Class_override(
57                 X[ 'Util' ][ 'NinjaIframe' ](),
58                 {
59         
60                         _busy         : false,
61                         _canceled     : false,
62                         
63                         load : function( option ){
64                                 //createURL
65                                 var url           = option[ 'url' ],
66                                         json2Path     = 'js/libs/json2.js',
67                                         json2FileSize = 18103,
68                                         html;
69                                 
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とか)にはまだ対応してないみたい 
74                         
75                                 if( X_UA[ 'Opera' ] ){
76                                         html = [
77                                                 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
78                                                 '<script>',
79                                                         'onunload=function(){im.onload=im.onerror=""};',
80                                                         'nw=+new Date;',
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}}',
82                                                 '</script>',    
83                                                 '<script id="jp"></script>',
84                                                 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
85                                         ];
86                                         X_Net_JSONP_maxOnloadCount = 2;
87                                 } else
88                                 if( X_UA[ 'IE8' ] ){
89                                         html = [
90                                                 // JavaScriptでunicode文字列をunescapeする
91                                                 // http://perutago.seesaa.net/article/202801583.html
92                                                 
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>',
97                                                 '<script>',
98                                                         'onunload=function(){clearTimeout(id)};',
99                                                         'nw=0;', // なぜか必要,,,
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);',
104                                                 '</script>'
105                                                 
106                                                 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
107                                                 '<script>',
108                                                         'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
109                                                 '</script>',
110                                                 '<script src="', url, '"></script>' */
111                                         ];
112                                         X_Net_JSONP_maxOnloadCount = 2;
113                                 } else
114                                 if( X_UA[ 'IE9' ] ){
115                                         html = [
116                                                 '<script id="jp"></script>',
117                                                 '<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);',
122                                                 '</script>'
123                                         ];
124                                         X_Net_JSONP_maxOnloadCount = 2;
125                                 } else
126                                 if( window[ 'JSON' ] ){
127                                         html = [        
128                                                 '<script>',
129                                                         'nw=+new Date;',
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}}',
132                                                 '</script>',
133                                                 '<script src="', url, '"></script>'
134                                         ];
135                                         X_Net_JSONP_maxOnloadCount = 1;
136                                 } else
137                                 if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
138                                         html = [
139                                                 '<script id="jn"></script>',
140                                                 '<script id="jp"></script>',
141                                                 '<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)}}',
147                                                 '</script>'
148                                         ];
149                                         X_Net_JSONP_maxOnloadCount = 3;
150                                 } else
151                                 if( X_UA[ 'IE' ] < 8 ){ // ie5-7
152                                         html = [
153                                                 '<script id="jn"></script>',
154                                                 '<script id="jp"></script>',
155                                                 '<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)}}',
161                                                 '</script>'
162                                         ];
163                                         X_Net_JSONP_maxOnloadCount = 3;
164                                 } else {
165                                         html = [
166                                                 '<script>',
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}}',
168                                                         'nw=+new Date;',
169                                                 '</script>',
170                                                 '<script src="', json2Path, '"></script>',
171                                                 '<script src="', url, '"></script>'
172                                         ];
173                                         X_Net_JSONP_maxOnloadCount = 2;
174                                 };
175                                 
176                                 X_NET_JSONPWrapper
177                                         [ 'refresh' ]( html.join( '' ) )
178                                         [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
179                                                         
180                                 X_NET_JSONPWrapper._busy = true;
181                         },
182                         
183                         cancel : function(){
184                                 X_NET_JSONPWrapper.reset();
185                                 X_NET_JSONPWrapper._canceled = true;
186                         },
187                         
188                         reset : function(){
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 );
194                         }
195                 }
196         );
197 };
198
199 function X_NET_JSONP_iframeListener( e ){
200         switch( e.type ){
201                 case 'ninjaload' :
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;
204                         
205                         // TODO callback が無ければ error -> timeout を観る?
206                         X_Net_JSONP_errorTimerID = X_NET_JSONPWrapper[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
207                         break;
208                 case 'ninjaerror' :
209                         console.log( 'iframe onerror' );
210                         X_NET_JSONPWrapper[ 'asyncDispatch' ]( X_EVENT_ERROR );
211                         break;
212         };
213         return X_Callback_UN_LISTEN;
214 };