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 : X_String_parseTrustedJsonString( jsonString )
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(){
53 X_NET_JSONPWrapper = X_Class_override( X[ 'Util' ][ 'NinjaIframe' ](), X_TEMP.X_NET_JSONP_params );
55 delete X_TEMP.X_NET_JSONP_init;
56 delete X_TEMP.X_NET_JSONP_params;
58 return X_NET_JSONPWrapper;
61 X_TEMP.X_NET_JSONP_params = {
66 load : function( option ){
68 var url = option[ 'url' ],
69 json2Path = 'js/libs/json2.js',
70 json2FileSize = 18103,
73 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
74 // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
75 // http://d.hatena.ne.jp/cnrd/20080518/1211099169
76 // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい
78 if( X_UA[ 'Opera' ] ){
80 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
82 'onunload=function(){im.onload=im.onerror=""};',
84 '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}}',
86 '<script id="jp"></script>',
87 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
89 X_Net_JSONP_maxOnloadCount = 2;
93 // JavaScriptでunicode文字列をunescapeする
94 // http://perutago.seesaa.net/article/202801583.html
96 // http://blog.livedoor.jp/dankogai/archives/51503830.html
97 // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
98 // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
99 '<script id="jp"></script>',
101 'onunload=function(){clearTimeout(id)};',
103 '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)}',
104 //'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}}',
105 'function tm(){jp.src="', url ,'";nw=+new Date}',
106 'id=setTimeout(tm,16);',
109 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
111 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
113 '<script src="', url, '"></script>' */
115 X_Net_JSONP_maxOnloadCount = 2;
119 '<script id="jp"></script>',
121 'onunload=function(){clearTimeout(id)};',
122 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
123 'function tm(){jp.src="', url ,'";nw=+new Date}',
124 'id=setTimeout(tm,16);',
127 X_Net_JSONP_maxOnloadCount = 2;
129 if( window[ 'JSON' ] ){
133 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
134 //'function cb(o){if(nw){nw-=+new Date;parent.postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
136 '<script src="', url, '"></script>'
138 X_Net_JSONP_maxOnloadCount = 1;
140 if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
142 '<script id="jn"></script>',
143 '<script id="jp"></script>',
145 'onunload=function(){clearTimeout(id)};',
146 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
147 'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
148 'id=setTimeout("t1()",16);',
149 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
152 X_Net_JSONP_maxOnloadCount = 3;
154 if( X_UA[ 'IE' ] < 8 ){ // ie5-7
156 '<script id="jn"></script>',
157 '<script id="jp"></script>',
159 'onunload=function(){clearTimeout(id)};',
160 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
161 'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
162 'id=setTimeout(t1,16);',
163 'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
166 X_Net_JSONP_maxOnloadCount = 3;
170 '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}}',
173 '<script src="', json2Path, '"></script>',
174 '<script src="', url, '"></script>'
176 X_Net_JSONP_maxOnloadCount = 2;
180 [ 'refresh' ]( html.join( '' ) )
181 [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
183 X_NET_JSONPWrapper._busy = true;
187 X_NET_JSONPWrapper.reset();
188 X_NET_JSONPWrapper._canceled = true;
192 X_NET_JSONPWrapper._busy = X_NET_JSONPWrapper._canceled = false;
193 X_NET_JSONPWrapper[ 'unlisten' ]( [ 'ninjaload', 'ninjaerror' ], X_NET_JSONP_iframeListener );
194 X_NET_JSONPWrapper[ 'refresh' ]( '' );
195 X_Net_JSONP_errorTimerID && X_Timer_remove( X_Net_JSONP_errorTimerID );
196 X_Net_JSONP_errorTimerID = X_Net_JSONP_onloadCount = 0;
200 function X_NET_JSONP_iframeListener( e ){
203 console.log( 'iframe onload, but ' + X_Net_JSONP_onloadCount + ' < ' + X_Net_JSONP_maxOnloadCount );
204 if( ++X_Net_JSONP_onloadCount < X_Net_JSONP_maxOnloadCount ) return;
206 // TODO callback が無ければ error -> timeout を観る?
207 X_Net_JSONP_errorTimerID = X_NET_JSONPWrapper[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
210 console.log( 'iframe onerror' );
211 X_NET_JSONPWrapper[ 'asyncDispatch' ]( X_EVENT_ERROR );
214 return X_Callback_UN_LISTEN;