Changeset 19669 for websites

Show
Ignore:
Timestamp:
09/21/08 15:51:39 (2 months ago)
Author:
NeoCat
Message:

enqueue Post to add or remove favouring / follow

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • websites/twicli/twicli.html

    r19645 r19669  
    3838#user_info td { font-size: small; } 
    3939--></style> 
    40 <script> 
     40<script type="text/javascript"> 
    4141function $(id) { return document.getElementById(id); } 
    4242// クロスドメインJavaScript呼び出し 
     
    7373}; 
    7474var xds = new XDomainScript; 
     75// 動的にフレームを生成してPOSTを投げる 
     76var postQueue = []; 
     77function enqueuePost(url, done, err) { 
     78        postQueue.push([url, done, err]); 
     79        if (postQueue.length > 1) // 複数リクエストを同時に投げないようキューイング 
     80                return; 
     81        postNext(); 
     82} 
     83function postNext() { 
     84        if (postQueue.length) { 
     85                postInIFrame(postQueue[0][0], postQueue[0][1], postQueue[0][2]); 
     86        } 
     87} 
     88var postSeq = 0; 
     89function postInIFrame(url, done, err) { 
     90        var frm = document.createElement("form");    // POST用のフォームを生成 
     91        frm.action = url; 
     92        frm.method = "POST"; 
     93        frm.target = "pfr" + seq; 
     94        document.body.appendChild(frm); 
     95        var pfr = document.createElement("iframe"); // formのtargetとなるiframeを生成 
     96        pfr.name = "pfr" + seq; 
     97        pfr.src = "about:blank"; 
     98        pfr.style.display = "block"; 
     99        pfr.style.position = "absolute"; 
     100        pfr.style.width = 300; 
     101        pfr.style.height = 200; 
     102        pfr.style.top = 100; 
     103        pfr.style.left = 200; 
     104        var errTimer = false; 
     105        if (err) {  // 10秒で正常終了しなければエラーとみなす 
     106                errTimer = setTimeout(function(){ 
     107                        err(); 
     108                        frm.parentNode.removeChild(frm); 
     109                        pfr.parentNode.removeChild(pfr); 
     110                        postQueue.shift(); 
     111                        postNext(); 
     112                }, 100000); 
     113        } 
     114        var cnt = 0; 
     115        var onload = pfr.onload = function(){ 
     116                if (cnt++ == 0) { 
     117                        setTimeout(function(){frm.submit();}, 0); 
     118                } else { 
     119                        clearTimeout(errTimer); 
     120                        done(); 
     121                        setTimeout(function(){ 
     122                                frm.parentNode.removeChild(frm); 
     123                                pfr.parentNode.removeChild(pfr); 
     124                                postQueue.shift(); 
     125                                postNext(); 
     126                        }, 0); 
     127                } 
     128        }; 
     129        document.body.appendChild(pfr); 
     130} 
    75131// 要素の位置を取得 
    76132function cumulativeOffset(ele) { 
     
    118174<body onLoad="init()"> 
    119175<div id="control"> 
    120 <iframe name="tx"></iframe> 
     176<iframe name="tx" onload="update()"></iframe> 
    121177<!--発言フォーム--> 
    122178<form name="frm" action="http://twitter.com/statuses/update.xml" method="POST" target="tx"> 
    123 <textarea id="fst" type="text" name="status" onkeyup="if (this.value.indexOf('\n') >= 0) return press(1)" onkeypress="return press(event)"></textarea> 
     179<textarea id="fst" name="status" onkeyup="if (this.value.indexOf('\n') >= 0) return press(1)" onkeypress="return press(event)"></textarea> 
    124180<input type="hidden" name="source" value="twicli"> 
    125181<a id="go" href="javascript:void press(1)"><img src="go.png"></a> 
    126182<a id="rst" href="javascript:void resetFrm()"><img src="clr.png"></a> 
    127183<div id="loading"><img src="loading.gif"></div></form> 
    128 <!--fav/followフォーム--> 
    129 <form name="favf" action="" method="POST" target="tx"></form> 
    130184<!--メニュー--> 
    131185<div id="menu"> 
     
    174228var get_next_func; 
    175229 
    176 // ローディング表示ON、twit更新 
    177 function reload(to) { 
    178         $("loading").style.display = "block"; 
    179         setTimeout(update, to); 
    180 } 
    181230// enterキーで発言 
    182231function press(e) { 
     
    184233        document.frm.status.value = document.frm.status.value.replace(/\n/g, ""); 
    185234        if (document.frm.status.value == '') { // 空欄であればTimeline更新のみ 
    186                 reload(0); 
     235                $("loading").style.display = "block"; 
     236                update(); 
    187237                return false; 
    188238        } 
     
    192242        document.frm.status.select(); 
    193243        setTimeout(resetFrm, 10); // twit送信後にフォームをクリア 
    194         reload(3000); // 送信3秒後にTimelineを更新 
    195244        document.frm.submit(); 
    196245} 
     
    216265function replyTo(user, id) { 
    217266        in_reply_to_user = user; 
    218         document.frm.status.value += "@" + user + " "; 
     267        document.frm.status.value = "@" + user + " " + document.frm.status.value; 
    219268        setReplyId(id); 
    220269        document.frm.status.select(); 
     
    306355// favoriteの追加(f=true)/削除(f=false) 
    307356function fav(img, id) { 
     357        if (img.src.indexOf('throbber') >= 0) return; 
    308358        var f = img.src.indexOf('empty') >= 0; 
    309         document.favf.action = 'http://twitter.com/favourings/' + (f ? 'create' : 'destroy') + '/' + id + '.xml'; 
    310         document.favf.submit(); 
    311         img.src = 'http://assets3.twitter.com/images/icon_star_' + (f ? 'full' : 'empty') + '.gif'; 
     359        img.src = 'http://twitter.com/images/icon_throbber.gif'; 
     360        enqueuePost('http://twitter.com/favourings/' + (f ? 'create' : 'destroy') + '/' + id + '.xml', 
     361                function(){ img.src = 'http://assets3.twitter.com/images/icon_star_' + (f ? 'full' : 'empty') + '.gif'; }, 
     362                function(){ img.src = 'http://assets3.twitter.com/images/icon_star_' + (!f ? 'full' : 'empty') + '.gif'; }); 
    312363} 
    313364// followとremove 
    314365function follow(f) { 
    315         document.favf.action = 'http://twitter.com/friendships/' + (f ? 'create' : 'destroy') + '/' + last_user + '.xml'; 
    316         document.favf.submit(); 
     366        enqueuePost('http://twitter.com/friendships/' + (f ? 'create' : 'destroy') + '/' + last_user + '.xml', switchUser); 
    317367        $("loading").style.display = "block"; 
    318         setTimeout(switchUser, 3000); 
    319368} 
    320369// 自分の最新の発言を受信