Using jQuery.ajax on WSH with async:true

(2009-09-30追記: ↓こんな複雑なことしなくても、もっと簡単にできました。)
昨日の続き。WSH 上の JScript から jQuery.ajax を使えるようになったのはいいけれど、async : false でなければいけないのはやっぱりちょっと悔しいので、適当にごまかしてWSH上で setInterval / setTimeout / clearInterval / clearTimeout を動くようにして jQuery.ajax の async:true でも処理できるようにしてみました。これで、レスポンスが返ってくるまでの時間も別の処理に充てられますので、複数のリクエストを並列で処理するときなんかも環境にやさしくスクリプトで遊べますね!

(function(){
    if( typeof( document ) == "undefined" ){
        document = new ActiveXObject( "htmlfile" );
        document.write("<html></html>");
    }
    if( typeof( window     ) == "undefined" ) window = document.parentWindow;
    if( typeof( alert      ) == "undefined" ) //alert = window.alert; 
        alert = function(s){ return window.alert( s )};
    if( typeof( clearTimeout  ) == "undefined" ) clearTimeout = function(id){ return window.clearTimeout(id) };
    if( typeof( confirm       ) == "undefined" ) confirm = function(s){ return window.confirm(s) };
    if( typeof( location      ) == "undefined" ) location = window.location;
    if( typeof( navigator     ) == "undefined" ) navigator = window.navigator;
    if( typeof( clearInterval ) == "undefined" ) clearInterval = function( id ){
        if( !("ontimeout" in document ) ) return;
        if( !(id in document.ontimeout ) ) return;
        window[ arguments[ 1 ] == "clearTimeout" ? "clearTimeut" : "clearInterval" ]( document.ontimeout[ id ].id );
        delete document.ontimeout[ id ];
    };
    if( typeof( clearTimeout ) == "undefined" ) clearTimeout = function(){
        clearInterval( id, "clearTimeout" );
    };

    if( typeof( setInterval   ) == "undefined" ) setInterval = function(code, delay){ 
        var func = typeof( code ) == "function" ? code : new Function( code );

        if( !("ontimeout" in document )) document.ontimeout = {};
        if( !("ontimeout_" in document )) document.ontimeout_ = 0;
        var r = ++document.ontimeout_;
        document.ontimeout[ r ] = { "func": func };
        var js = "document.ontimeout[" + r + "].id=setInterval" + 
            "( (function(){ document.ontimeout[" + r + 
            "].func(); }), " + delay + ")";
        var js;
        if( arguments[ 2 ] == "setTimeout" ){
            js = "document.ontimeout[" + r + "].id=setTimeout" + 
                "( (function(){ var f = document.ontimeout[" + r + 
                "].func; delete document.ontimeout[" + r + "];f();}), " + delay + ")";
        }else{
            js = "document.ontimeout[" + r + "].id=setInterval" + 
                "( (function(){ document.ontimeout[" + r + 
                "].func(); }), " + delay + ")";
        }
        document.write( "<script>" + js + "</script>" );
        return r;
    };
    if( typeof( setTimeout ) == "undefined" ) setTimeout = function(code, delay){ 
        return setInterval( code, delay, "setTimeout" );
    };

    _util = {};
    _util.cat = function(f){
        return (new ActiveXObject( "Scripting.FileSystemObject" )).OpenTextFile( f, 1, false ).ReadAll();
    };
})();

eval( _util.cat( "jquery-1.3.2.js" ) );

var complete = false;
$.ajax( {
    url : "http://example.com/rss.xml",
    dataType : "xml",
    type : "get",
    async : true, 
    success : function( xml ){
        var s = "";
        $( xml ).find( "item" ).each( function(){
            s += 
                $(this).find( "title" ).text() + " <" +
                $(this).find( "link" ).text() + "> (" +
                $(this).find( "dc\\:date" ).text() + ")\n";
        } );
        alert( s );
        complete = true;
    }
} );

for( var i = 0; i < 100; i++ ){
    WScript.Sleep( 100 );
    if( complete ) break;
}
alert( "done" );

やってることは、window.setInterval / window.clearInterval / window.setTimeout / window.clearTimeout を利用して、それらをグローバル空間で動かすためのラッパ関数を用意しただけです。