Using setInterval / setTimeout on WSH

さらに昨日の続き。いろいろ試してみると、昨日みたいに複雑なことをしなくても、window.setInterval などを呼び出すラッパ関数を作ってやれば、普通に setInterval や setTimeout も呼び出せました。というわけで、以下のような偽 window、偽 document を準備するコードを用意してやれば、jQuery.ajax を非同期で動かしたり、setTimeout で遅延実行させたり、自由自在ですね。

// ユーティリティ関数
var _util = { cat : function(f){return (new ActiveXObject("Scripting.FileSystemObject")).OpenTextFile(f,1,false).ReadAll();} };
// 偽 window、偽document、グローバルオブジェクトの準備
(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( 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){
        return window.clearInterval( id );
    };
    if( typeof( clearTimeout  ) == "undefined" ) clearTimeout = function(id){
        return window.clearTimeout(id);
    };
    if( typeof( setInterval   ) == "undefined" ) setInterval = function(code, interval){
        return window.setInterval(code, interval);
    }
    if( typeof( setTimeout    ) == "undefined" ) setTimeout = function(code, delay){
        return window.setTimeout(code, delay);
    }
})();

// setInterval で繰り返し実行も自由自在
var n = 6;
var id = setInterval( function(){ if( --n ) alert( 'interval' ); else clearInterval( id );}, 500 );
while( n ){
    WScript.Sleep( 100 );
}

// jQueryの読み込み
eval( _util.cat( "jquery-1.3.2.js" ) );

// はてなダイアリー、はてなブックマークそれぞれのRSSを並行して読み込みます。
var complete = 0;
$.ajax( {
    url : "http://d.hatena.ne.jp/hasegawayosuke/rss",
    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 += 1;
    }
} );

$.ajax( {
    url : "http://b.hatena.ne.jp/hasegawayosuke/rss",
    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 += 1;
    }
} );

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

※ IE8 on Vista SP1 と IE7 on XP SP3 な環境で確認しました。