ふたりのチャット

■ソース
[diary:174247]
[beforeExec]
getChat = function(){
chatCh = 0;
Fx.e(ch0_uri, "cn0");
};
Lo.cn0 = function(msg){
if(!isLoop) return;
msg = Fx.dec(msg.replace(/^.+name="text">(.+)\x3c\/textarea>.+$/, "$1"));
if(u0msg != msg){
echo("logo_0", msg == "done." ? '<input type="button" value="入室" onclick="login(0)" />' : "入室中");
}
if(msg == "done.") chatCh++;
u0msg = msg;
Fx.e(ch1_uri, "cn1");
};
Lo.cn1 = function(msg){
if(!isLoop) return;
msg = Fx.dec(msg.replace(/^.+name="text">(.+)\x3c\/textarea>.+$/, "$1"));
if(u1msg != msg){
echo("logo_1", msg == "done." ? '<input type="button" value="入室" onclick="login(1)" />' : "入室中");
}
if(msg == "done.") chatCh += 2;
u1msg = msg;
if(chatCh != isChatCh){
echo("submitForm", chatCh == 3 ? "ホストで入室になります" :
chatCh == 0 ? "満室です" : "相方が入室してお待ちしています"
);
}
isChatCh = chatCh;
chatHost = chatCh == 3 ? true : false;
if(isLoop) setTimeout("getChat()", 1000);
};

ac1_a = function(){ Fx.e(ch_uri + chatBoxNo2, "ac1_b"); };
Lo.ac1_b = function(s){
var msg = Fx.dec(s.replace(/^.+name="text">(.+)\x3c\/textarea>.+$/, "$1"));
Fid("ac2").style.color = msg == "done." ? "#ccf" : "#339";
if(msg != "done." && msg != user2Msg){
showChatDisp(msg);
Fid("ac3").style.color="orange";
}
user2Msg = msg;
Fid("ac1").style.color = "#fcc";
};

isLogin = function(){
Fid("logoAct").style.color = timeCnt % 2 ? "#0f0" : "#cfc";
if(timeCnt % 5 == 0){
Fid("ac1").style.color="tomato";
ac1_a();
}
echo("u_time", userTimes());
if(chatHost && timeCnt % 31 == 0 && msgStack != 0) updateBody();
timeCnt++;
};
uc = function(){ updateBody(); };
updateBody = function(){
msgStack = 0;
Fid("uc1").style.color = "magenta";
var str = '\x3cdiv class="chat_info">' + chatDefaultInfo + '\x3c/div>\n' +
'\x3cdiv class="chat_body">' + chatDefaultBody + '\x3c/div>';
str = str.replace(/\t/g, "\n");
Fx.e("diary_form",diaryForm(chatArticle, chatTitle, str,0, 949), "uE2");
};
Lo.uE2 = function(){
Fid("uc1").style.color = "#339";
};
getArticle = function(){
var str = Fx.e(["/cgi-bin/diary_form.cgi?mode=edit;oid=",Vis.oid,";article_id=",chatArticle], false);
str = str.replace(/\r?\n/g, "\t");
return Fx.dec(str);
};
userTimes = function(){
var a = chatTime(0).split(":");
return a[0] + '\x3cspan style="color:' + (timeCnt % 2 ? "#000" : "#ccc") + '">:\x3c/span>' + a[1];
};

login = function(n){
isLoop = false;
chatBox = n;
chatBox2 = n ? 0 : 1;
chatName = ["なんちゃん","葉月"];
chatUser = chatName[n];
chatUser2 = chatName[chatBox2];
var str = chatUser + ' <input type="text" id="txt1" size="' +
(n ? 86 : 80) + '" />';
chatBoxNo = ch[n];
chatBoxNo2 = ch[chatBox2];
echo("submitForm", str);
chatLogin = true;
echo("logo_" + chatBox, '\x3cspan id="u_time">' + userTimes() + '\x3c/span> \x3cspan id="uc1">■\x3c/span>\x3cspan id="uc2" onclick="uc()">■\x3c/span>').className = "logo_a";
echo("logo_" + chatBox2, '\x3cspan id="logoAct">■\x3c/span>\x3cspan id="ac1">■\x3c/span>\x3cspan id="ac2">■\x3c/span>\x3cspan id="ac3">■\x3c/span>').className = "logo_b";
user2Msg = "";
var s = '\x3cspan class="c_info">■' + chatTime(1) + " " + chatUser + "がログインしました\x3c/span>";
chat(s);
showChatDisp(s);
chatTimer1 = setInterval("isLogin()", 1000);
};

entry = function(){
ch = [46326,46325];
chatArticle = Fx.loc.match(/page=(\d+)/) ? RegExp.$1 : 174289;
var doc = getArticle();
chatTitle = doc.replace(/^.+ value="(ふたりのチャット \(\d+\))".+$/, "$1");
chatDefaultBody = doc.replace(/^.+\x3cdiv class="chat_body">(.+?)\x3c\/div>.+$/, "$1");
chatDefaultInfo = doc.replace(/^.+\x3cdiv class="chat_info">(.+?)\x3c\/div>.+$/, "$1");
echo("chatDisp", chatDefaultBody.replace(/\t/g, "\x3cbr />"));
ch_uri = "/cgi-bin/diary_editor.cgi?oid=" + Vis.oid + "&type=comments;id=";
ch0_uri = ch_uri + ch[0];
ch1_uri = ch_uri + ch[1];
ini();
getChat();
};
ini = function(){
msgStack = 0;
chatLogin = false;
timeCnt = 0;
u0msg = "";
u1msg = "";
isLoop = true;
isChatCh = -1;
};

chatTime = function(n){
var d = new Date();
var mm = ("0" + (d.getMonth() + 1)).slice(-2);
var dd = ("0" + d.getDate()).slice(-2);
var h = ("0" + d.getHours()).slice(-2);
var m = ("0" + d.getMinutes()).slice(-2);
return (n ? mm + "/" + dd + " " : "") + h + ":" + m;
};

chat = function(msg){
Fid("ac3").style.color = "yellow";
var str = "/cgi-bin/diary_editor.cgi?mode=update&oid=" + Vis.oid + "&type=comments&id=" +
chatBoxNo + "&text=" + enc(msg);
Fx.e(str, false);
};

showChatDisp = function(str){
var o = Fid("chatDisp");
echo(o.insertBefore(Fce("div"), o.firstChild), str);
chatDefaultBody = str + "\n" + chatDefaultBody;
msgStack++;
if(chatHost && msgStack > 10) updateBody();
};

chatSubmit = function(){
var str = '\x3cspan class="c_user' + chatBox + '">' + chatUser + '\x3c/span> : \x3cspan class="c_body' + chatBox + '">' + echo("txt1") + '\x3c/span> \x3cspan class="c_time' + chatBox + '">(' + chatTime(0) + ')\x3c/span>';
chat(str);
showChatDisp(str);
echo("txt1", "").focus();

};
window.onunload = function(){
if(chatLogin) chat("done.");
};
[/beforeExec]
[cssText]
#mainDisp { margin-left:2em; }
#chatDisp {
min-height:30em; margin-bottom:3em;
margin-top:.5em;
}
.top_form {
padding-top:95px; width:350px; text-align:center; float:left;
background:url(http://nancyan.main.jp/image/futari/n/nancyan_hazuki_kotobanohako_123.jpg) no-repeat center top;
}
#txt1 { padding:2px .25em; font-size:105%; }
#logo_w0 { display:inline-block; width:49%; text-align:right; }
#logo_w1 { display:inline-block; width:49%; text-align:left; }
#logo_0 { margin-right:60px;}
#logo_1 { margin-left:65px; }
.logo_a { color:#339;}
.logo_b { color:darkgreen; }

#uc2 { cursor:default; }

.logo { margin-left:360px; padding-top:1em; }
.logo span { font-size:80%; }
.logo a { text-decoration:none; font:130%/1.3 serif; }

.clear { clear:left; }
#submitForm { margin:.5em 0em; font-size:90%; }
#u_time { color:#808080; font-family:Verdana; }
#_u_time span { font-weight:bold; }
[/cssText]
[body]
<form onsubmit="return false;" class="top_form">
<$$span id="logo_w0"><$$span id="logo_0">入室中<$$/span><$$/span>
<$$span id="logo_w1"><$$span id="logo_1">入室中<$$/span><$$/span>
</form>
<$$div class="logo">
<$$a href="main.html?diary=174247">ふたりのチャット <$$span>1.0<$$/span><$$/a>
<$$/div>
<$$div class="clear"><$$/div>
<form onsubmit="chatSubmit(); return false;" id="submitForm">
done.
</form>

<$$div id="chatDisp" class="chat_body"><$$/div>
[/body]
[exec]
document.title = "ふたりのチャット";
entry();
[/exec]
[/diary:174247]

■仕様書 2010 09/01 11:40 Wrote