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
index 6c008f0..b233d7b 100644 (file)
@@ -1,10 +1,11 @@
+//{+jsonp"jsonpによるajax"(jsonpによるクロスドメイン通信。)[+net,+ninjaiframe]
+
 /*
  * Operaでも非同期リクエストが並列処理できる img-JSONP
  * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
  * 
  * iframe を使った jsonp の読み込みエラー判定の記事、
- * JSONPのエラーを判断する は web archive でも観れない
- * http://d.hatena.ne.jp/yuushimizu/20090128/1233146321
+ * https://web.archive.org/web/20120917100043/http://d.hatena.ne.jp/yuushimizu/20090128/1233146321
  * TODO JSONPの動的取得+エラー処理
  * http://d.hatena.ne.jp/NeoCat/20110206/1296934235
  * 
  * 
  * IE6(IETester,localhost) で動かない,
  * 
- * TODO postMessage
+ * TODO iframe 内を他ドメインにして jsonp のセキュリティを強化。他ドメインに jsonp を読み込んで 正しい JSON か?検証後にフレーム間通信で戻す。
  */
 
-X[ 'Net' ][ 'JSONP' ] = {
-       'cb' : function( accessKey, jsonString, time, opt_json2FileSize ){
-                       if( accessKey !== X_NET_JSONP_ACCESS_KEY || !X_NET_JSONPWrapper._busy ) return;
+       // TODO chashe
+       // TODO iframe useful or not. TODO check dynamicIframe
+       // TODO file: では http: は使えない
+
+X_TEMP.X_JSONP_cb = function( accessKey, jsonString, time, opt_json2FileSize ){
+                       if( accessKey !== X_JSONP_ACCESS_KEY || !X_JSONP._busy ) return;
                        
-                       X_NET_JSONPWrapper._busy = false;
+                       X_JSONP._busy = false;
                        
-                       X_NET_JSONPWrapper
+                       X_JSONP
                                [ 'asyncDispatch' ]( {
-                                       type : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
-                                       data : window.JSON ? JSON.parse( jsonString ) : eval( 'var a=' + jsonString + ';a' )
+                                       type     : jsonString ? X_EVENT_SUCCESS : X_EVENT_ERROR,
+                                       response : X_JSON_parseTrustableString( jsonString )
                                } );
                        
+                       X_JSONP_errorTimerID && X_Timer_remove( X_JSONP_errorTimerID );
+                       
                        console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
-               }
-};
-
-function X_Net_JSONP_reciveMessage( data ){
-       console.log( data );
-};
+       };
 
-var X_NET_JSONP_ACCESS_KEY = Math.random(),
+var X_JSONP_ACCESS_KEY = Math.random(),
        
-       X_NET_JSONP_SEND_MSG_KEY = X_System.message( 'X.Net.JSONP', X_Net_JSONP_reciveMessage ),
+       X_JSONP_maxOnloadCount,
        
-       X_NET_JSONP_NinjaIframe,
+       X_JSONP_onloadCount = 0,
        
-       X_Net_JSONP_onloadCount;
+       X_JSONP_errorTimerID;
 
+X_TEMP.X_JSONP_init = function(){
+       X[ 'Net' ][ '__json_cb__' ] = X_TEMP.X_JSONP_cb;
        
-
-function X_NET_JSONP_loadScriptInNinjaIframe( url ){
-       var json2Path     = 'js/libs/json2.js',
-               json2FileSize = 18103,
-               html;
+       X_JSONP = X_Class_override( X_NinjaIframe(), X_TEMP.X_JSONP_params );
        
-       X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = X[ 'Util' ][ 'NinjaIframe' ]() );
+       delete X_TEMP.X_JSONP_cb;
+       delete X_TEMP.X_JSONP_init;
+       delete X_TEMP.X_JSONP_params;
        
-       // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
-       // TODO postMessage の利用
-       // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
-       // http://d.hatena.ne.jp/cnrd/20080518/1211099169
-       // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい 
+       return X_JSONP;
+};
 
-       if( X_UA[ 'Opera' ] ){
-               html = [
-                       ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
-                       '<script>',
-                               'onunload=function(){im.onload=im.onerror=""};',
-                               'nw=+new Date;',
-                               '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}}',
-                       '</script>',    
-                       '<script id="jp"></script>',
-                       '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
-               ];
-               X_Net_JSONP_onloadCount = 2;
-       } else
-       if( X_UA[ 'IE8' ] ){
-               html = [
-                       // JavaScriptでunicode文字列をunescapeする
-                       // http://perutago.seesaa.net/article/202801583.html
+X_TEMP.X_JSONP_params = {
+       
+                       _busy         : false,
+                       _canceled     : false,
                        
-                       // http://blog.livedoor.jp/dankogai/archives/51503830.html              
-                       // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
-                       // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
-                       '<script id="jp"></script>',
-                       '<script>',
-                               'onunload=function(){clearTimeout(id)};',
-                               'nw=0;', // なぜか必要,,,
-                               '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)}',
-                               //'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}}',                   
-                               'function tm(){jp.src="', url ,'";nw=+new Date}',
-                               'id=setTimeout(tm,16);',
-                       '</script>'
+                       load : function( option ){
+                               //createURL
+                               var url           = option[ 'url' ],
+                                       params        = option[ 'params' ],
+                                       callback      = option[ 'callbackName' ],
+                                       charset       = option[ 'charset' ],
+                                       json2Path     = window.RegExp ? 'js/libs/json2.js' : 'js/libs/json2_regfree.js',
+                                       json2FileSize = 18103,
+                                       html;
+                               
+                               // file プロトコルで外部アクセスの禁止
+                               if( !X_URL_isSameProtocol( url ) ){
+                                       return X_JSONP[ 'asyncDispatch' ]( X_EVENT_ERROR );
+                               };
+                               
+                               url = X_URL_create( url, params );
+                               
+                               if( !callback && !( callback = X_URL_paramToObj( url.split( '?' )[ 1 ] )[ 'callback' ] ) ){
+                                       url += '&callback=cb';
+                                       callback = 'cb';
+                               };
+                               
+                               charset = charset ? ' charset="' + charset + '"' : '';
+                               
+                               // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
+                               // document.postMessage()→window.postMessage() (Opera 9.50 build 9841 -)
+                               // http://d.hatena.ne.jp/cnrd/20080518/1211099169
+                               // 最近の仕様変更(引数のtargetOriginとかMessageEventのoriginとか)にはまだ対応してないみたい 
                        
-                       /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
-                       '<script>',
-                               'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
-                       '</script>',
-                       '<script src="', url, '"></script>' */
-               ];
-               X_Net_JSONP_onloadCount = 2;
-       } else
-       if( X_UA[ 'IE9' ] ){
-               html = [
-                       '<script id="jp"></script>',
-                       '<script>',
-                               'onunload=function(){clearTimeout(id)};',
-                               'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
-                               'function tm(){jp.src="', url ,'";nw=+new Date}',
-                               'id=setTimeout(tm,16);',
-                       '</script>'
-               ];
-               X_Net_JSONP_onloadCount = 2;
-       } else
-       if( window[ 'JSON' ] ){
-               html = [        
-                       '<script>',
-                               'nw=+new Date;',
-                               'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
-                               //'function cb(o){if(nw){nw-=+new Date;parent.postMessage("', X_NET_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
-                       '</script>',
-                       '<script src="', url, '"></script>'
-               ];
-               X_Net_JSONP_onloadCount = 1;
-       } else
-       if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
-               html = [
-                       '<script id="jn"></script>',
-                       '<script id="jp"></script>',
-                       '<script>',
-                               'onunload=function(){clearTimeout(id)};',
-                               'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
-                               'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
-                               'id=setTimeout("t1()",16);',
-                               'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
-                       '</script>'
-               ];
-               X_Net_JSONP_onloadCount = 3;
-       } else
-       if( X_UA[ 'IE' ] < 8 ){ // ie5-7
-               html = [
-                       '<script id="jn"></script>',
-                       '<script id="jp"></script>',
-                       '<script>',
-                               'onunload=function(){clearTimeout(id)};',
-                               'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
-                               'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
-                               'id=setTimeout(t1,16);',
-                               'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
-                       '</script>'
-               ];
-               X_Net_JSONP_onloadCount = 3;
-       } else {
-               html = [
-                       '<script>',
-                               '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}}',
-                               'nw=+new Date;',
-                       '</script>',
-                       '<script src="', json2Path, '"></script>',
-                       '<script src="', url, '"></script>'
-               ];
-               X_Net_JSONP_onloadCount = 1;
-       };
-       
-       X_NET_JSONP_NinjaIframe
-               [ 'refresh' ]( html.join( '' ) )
-               [ 'listen' ]( [ X_EVENT_SUCCESS, X_EVENT_ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
-};
-
+                               if( X_UA[ 'Opera' ] ){
+                                       html = [
+                                               ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
+                                               '<script>',
+                                                       'onunload=function(){im.onload=im.onerror=""};',
+                                                       'nw=+new Date;',
+                                                       '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}}',
+                                               '</script>',    
+                                               '<script', charset, ' id="jp"></script>',
+                                               '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
+                                       ];
+                                       X_JSONP_maxOnloadCount = 2;
+                               } else
+                               if( X_UA[ 'IE8' ] ){
+                                       html = [
+                                               '<script', charset, ' id="jp"></script>',
+                                               '<script>',
+                                                       'onunload=function(){clearTimeout(id)};',
+                                                       'nw=0;', // なぜか必要,,,
+                                                       'function ', callback, '(o){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',parent.X.JSON.stringify(o),-nw)}',
+                                                       //'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}}',                   
+                                                       'function tm(){jp.src="', url ,'";nw=+new Date}',
+                                                       'id=setTimeout(tm,16);',
+                                               '</script>'
+                                               
+                                               /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
+                                               '<script>',
+                                                       'function ', callback, '(o){window.parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
+                                               '</script>',
+                                               '<script src="', url, '"></script>' */
+                                       ];
+                                       X_JSONP_maxOnloadCount = 2;
+                               } else
+                               if( X_UA[ 'IE9' ] ){
+                                       html = [
+                                               '<script', charset, ' id="jp"></script>',
+                                               '<script>',
+                                                       'onunload=function(){clearTimeout(id)};',
+                                                       'function ', callback, '(o){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
+                                                       'function tm(){jp.src="', url ,'";nw=+new Date}',
+                                                       'id=setTimeout(tm,16);',
+                                               '</script>'
+                                       ];
+                                       X_JSONP_maxOnloadCount = 2;
+                               } else
+                               if( window[ 'JSON' ] ){
+                                       html = [        
+                                               '<script>',
+                                                       'nw=+new Date;',
+                                                       'function ', callback, '(o){if(nw){nw-=+new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
+                                                       //'function ', callback, '(o){if(nw){nw-=+new Date;parent.postMessage("', X_JSONP_SEND_MSG_KEY,' "+nw+"|"+JSON.stringify(o),"', location.origin, '");nw=0}}',
+                                               '</script>',
+                                               '<script', charset, ' src="', url, '"></script>'
+                                       ];
+                                       X_JSONP_maxOnloadCount = 1;
+                               } else
+                               if( X_UA[ 'IE4' ] || X_UA[ 'MacIE' ] ){
+                                       html = [
+                                               '<script id="jn"></script>',
+                                               '<script', charset, ' id="jp"></script>',
+                                               '<script>',
+                                                       'onunload=function(){clearTimeout(id)};',
+                                                       'function ', callback, '(o){nw-=new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
+                                                       'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
+                                                       'id=setTimeout("t1()",16);',
+                                                       'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
+                                               '</script>'
+                                       ];
+                                       X_JSONP_maxOnloadCount = 3;
+                               } else
+                               if( X_UA[ 'IE' ] < 8 ){ // ie5-7
+                                       html = [
+                                               '<script id="jn"></script>',
+                                               '<script', charset, ' id="jp"></script>',
+                                               '<script>',
+                                                       'onunload=function(){clearTimeout(id)};',
+                                                       'function ', callback, '(o){nw-=new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
+                                                       'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
+                                                       'id=setTimeout(t1,16);',
+                                                       'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
+                                               '</script>'
+                                       ];
+                                       X_JSONP_maxOnloadCount = 3;
+                               } else {
+                                       html = [
+                                               '<script>',
+                                                       'function ', callback, '(o){if(nw){nw-=new Date;parent.X.Net.__json_cb__(' + X_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw,', json2FileSize, ');nw=0}}',
+                                                       'nw=+new Date;',
+                                               '</script>',
+                                               '<script src="', json2Path, '"></script>',
+                                               '<script', charset, ' src="', url, '"></script>'
+                                       ];
+                                       X_JSONP_maxOnloadCount = 2;
+                               };
+                               
+                               X_JSONP
+                                       [ 'refresh' ]( html.join( '' ) )
+                                       [ 'listen' ]( [ 'ninjaload', 'ninjaerror' ], X_JSONP_iframeListener );
+                                                       
+                               X_JSONP._busy = true;
+                       },
+                       
+                       cancel : function(){
+                               X_JSONP.reset();
+                               X_JSONP._canceled = true;
+                       },
+                       
+                       reset : function(){
+                               X_JSONP._busy = X_JSONP._canceled = false;
+                               X_JSONP[ 'unlisten' ]( [ 'ninjaload', 'ninjaerror' ], X_JSONP_iframeListener );
+                               X_JSONP[ 'refresh' ]( '' );
+                               X_JSONP_errorTimerID && X_Timer_remove( X_JSONP_errorTimerID );
+                               X_JSONP_errorTimerID = X_JSONP_onloadCount = 0;
+                       }
+               };
 
-function X_NET_JSONP_iframeListener( e ){
+function X_JSONP_iframeListener( e ){
        switch( e.type ){
-               case X_EVENT_SUCCESS :
-                       console.log( 'iframe onload, but ' + X_NET_JSONPWrapper._onloadCount + ' < ' + X_Net_JSONP_onloadCount );
-                       if( ++X_NET_JSONPWrapper._onloadCount < X_Net_JSONP_onloadCount ) return;
-                       // TODO callback が無ければ error
-                       X_NET_JSONPWrapper[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
+               case 'ninjaload' :
+                       console.log( 'iframe onload, but ' + X_JSONP_onloadCount + ' < ' + X_JSONP_maxOnloadCount );
+                       if( ++X_JSONP_onloadCount < X_JSONP_maxOnloadCount ) return;
+                       
+                       // TODO callback が無ければ error -> timeout を観る?
+                       X_JSONP_errorTimerID = X_JSONP[ 'asyncDispatch' ]( 1000, X_EVENT_ERROR );
                        break;
-               case X_EVENT_ERROR :
+               case 'ninjaerror' :
                        console.log( 'iframe onerror' );
-                       X_NET_JSONPWrapper[ 'asyncDispatch' ]( X_EVENT_ERROR );
+                       X_JSONP[ 'asyncDispatch' ]( X_EVENT_ERROR );
                        break;
        };
-       X_NET_JSONP_NinjaIframe[ 'unlisten' ]();
-       return X_Callback_UN_LISTEN;
+       return X_CALLBACK_UN_LISTEN;
 };
-
-// TODO extend NinjaIframe
-X_NET_JSONPWrapper = X_Class_override(
-       X_EventDispatcher(),
-       {
-
-               _busy         : false,
-               _canceled     : false,
-               _onloadCount  : 0,
-               
-               load : function( url, data, timeout ){
-                       //createURL
-                       X_NET_JSONP_loadScriptInNinjaIframe( url );
-                                               
-                       this._busy = true;
-               },
-               
-               cancel : function(){
-                       this._canceled = true;
-               },
-               
-               reset : function(){
-                       this._busy = this._canceled = false;
-                       this._onloadCount = 0;
-               }
-       }
-);