]>
git.scottworley.com Git - reliable-chat/blob - webclient/rc.js
3231f4c34b607ff42aaa55a2a8e1734b68ce825c
1 var servers
= ['chkno.net', 'rc2.chkno.net', 'echto.net', 'the-wes.com', 'vibrantlogic.com'];
3 var session
= Math
.random();
7 var nick
= localStorage
.getItem("nick");
14 function rcsetnick(new_nick
) {
15 localStorage
.setItem("nick", new_nick
);
18 function rcserverbase(server
) {
19 // Add the default port if server doesn't contain a port number already
20 if (server
.indexOf(":") == -1) {
21 return "http://" + server
+ ":21059";
23 return "http://" + server
;
27 function rcchangeserverstatus(server
, new_status
) {
28 var statusbar
= document
.getElementById("status");
29 var spans
= statusbar
.getElementsByTagName("span");
31 if (spans
[i
].firstChild
&& 'data' in spans
[i
].firstChild
&& spans
[i
].firstChild
.data
== server
) {
32 spans
[i
].setAttribute("class", new_status
);
37 function rcaddmessagetohistory(message
) {
38 var d
= document
.createElement("div");
39 d
.appendChild(document
.createTextNode(message
));
40 var h
= document
.getElementById("history");
42 window
.scrollTo(0, document
.body
.scrollHeight
);
46 function make_seen_key(id
, text
) {
47 return id
.replace(/@/g
, "@@") + "_@_" + text
.replace(/@/g
, "@@");
50 function receiveMessage(server
, time
, id
, text
) {
51 var seen_key
= make_seen_key(id
, text
);
52 if (!(seen_key
in seen
)) {
53 seen
[seen_key
] = true;
54 rcaddmessagetohistory(text
);
56 rcchangeserverstatus(servers
[i
], "sad");
59 rcchangeserverstatus(server
, "happy");
62 function receiveMessageEvent(event
)
65 if (event
.origin
=== rcserverbase(servers
[i
])) {
66 messages
= JSON
.parse(event
.data
);
68 if ('Time' in messages
[j
] &&
69 'ID' in messages
[j
] &&
70 'Text' in messages
[j
]) {
71 receiveMessage(servers
[i
], messages
[j
]['Time'], messages
[j
]['ID'], messages
[j
]['Text']);
78 function rcconnect() {
79 window
.addEventListener("message", receiveMessageEvent
, false);
81 // Create a hidden iframe for same-origin workaround
82 var iframe
= document
.createElement("iframe");
83 iframe
.setAttribute("src", rcserverbase(servers
[i
]) + "/frame");
84 document
.body
.insertBefore(iframe
, document
.body
.firstChild
);
86 var status_indicator
= document
.createElement("span");
87 status_indicator
.appendChild(document
.createTextNode(servers
[i
]));
88 status_indicator
.setAttribute("class", "sad");
89 document
.getElementById("status").appendChild(status_indicator
);
91 if (rcnick() == 'anonymous') {
92 rcaddmessagetohistory("-!- Set your nick with /nick");
96 function rcsend(d
, message
) {
97 var id
= new Date().getTime() + "-" + session
+ "-" + Math
.random();
98 seen
[make_seen_key(id
, message
)] = true;
100 "?id=" + encodeURIComponent(id
) +
101 "&text=" + encodeURIComponent(message
);
103 var uri
= rcserverbase(servers
[i
]) + path
;
104 var img
= document
.createElement("img");
105 img
.setAttribute("src", uri
);
110 function rckeydown(event
) {
111 if (event
.keyCode
== 13) {
112 var input
= document
.input
.say
.value
;
113 document
.input
.say
.value
= "";
117 var re
= /^\/nick (.*)/;
118 var match
= re
.exec(input
);
120 message
= "*** " + rcnick() + " is now known as " + match
[1];
123 message
= "<" + rcnick() + "> " + input
;
126 // Remind people to set their nick
127 if (rcnick() == 'anonymous') {
128 rcaddmessagetohistory("-!- Set your nick with /nick");
132 var d
= rcaddmessagetohistory(message
);