spongy-client-chrome/app/wirc.js

238 lines
5.5 KiB
JavaScript
Raw Normal View History

2014-07-23 21:15:04 -06:00
var msgRe = /([^ ]+) (<[^>]+>) (.*)/;
var kibozeRe = /[Nn]eal/;
var urlRe = /[a-z]+:\/\/[^ ]*/;
var nick = "Mme. M";
2014-07-24 10:34:02 -06:00
2014-10-24 15:51:53 -06:00
var scrollbackLength = 500;
2014-10-23 21:21:54 -06:00
if (String.prototype.startsWith == null) {
String.prototype.startsWith = function(needle) {
return this.lastIndexOf(needle, 0) == 0;
}
}
2014-10-24 15:51:53 -06:00
function getTemplate(className) {
return document.templates.getElementsByClassName(className)[0].cloneNode(true);
}
2014-07-24 10:34:02 -06:00
function isinView(oObject) {
return (oObject.offsetParent.clientHeight <= oObject.offsetTop);
}
2014-07-23 21:15:04 -06:00
2014-10-24 15:51:53 -06:00
function selectForum(room) {
var kids = document.rooms_list.childNodes;
for (i = 0; i < kids.length; i += 1) {
e = kids[i];
2014-10-24 15:51:53 -06:00
if (e == room) {
e.className = "room selected";
e.messages.display = "block";
} else {
2014-10-24 15:51:53 -06:00
e.className = "room";
e.messages.display = "none";
}
}
2014-10-24 15:51:53 -06:00
if (room.lastChild) {
room.lastChild.scrollIntoView(false);
}
2014-10-24 15:51:53 -06:00
}
2014-10-24 15:51:53 -06:00
fora = {}
function getForumElement(forum) {
2014-10-24 15:51:53 -06:00
var fe = fora[forum];
if (! fe) {
2014-10-24 15:51:53 -06:00
var room = getTemplate("channel room");
room.textContent = forum;
document.rooms_list.appendChild(room);
fe = getTemplate("messages");
fe.room = room;
room.messages = fe;
// XXX: split out into non-anon function
room.addEventListener("click", function() {selectForum(fe)});
fora[forum] = fe;
document.getElementById("messages-container").appendChild(fe);
}
2014-10-24 15:51:53 -06:00
return fe;
2014-10-24 15:51:53 -06:00
}
2014-07-23 21:15:04 -06:00
function addMessagePart(p, className, text) {
var e = document.createElement("span");
e.className = className;
e.appendChild(document.createTextNode(text));
p.appendChild(e);
p.appendChild(document.createTextNode(" "));
}
function addText(p, text, kiboze) {
// Look for a URL
var txtElement = document.createElement("span");
txtElement.className = "text";
var rhs = text;
var match;
2014-10-24 15:51:53 -06:00
while ((match = urlRe.exec(rhs)) != null) {
var before = rhs.substr(0, match.index);
var a = document.createElement("a");
var href = match[0];
2014-10-24 15:51:53 -06:00
if (href.indexOf("hxx") == 0) {
href = "htt" + href.substr(3);
}
a.href = href
a.target = "_blank";
a.appendChild(document.createTextNode(match[0]));
txtElement.appendChild(document.createTextNode(before));
txtElement.appendChild(a);
rhs = rhs.substr(match.index + match[0].length);
}
txtElement.appendChild(document.createTextNode(rhs));
p.appendChild(txtElement);
2014-10-24 15:51:53 -06:00
if ((kiboze) || (-1 != text.search(kibozeRe))) {
var k = document.getElementById("kiboze");
var p2 = p.cloneNode(true);
2014-10-24 15:51:53 -06:00
if (k) {
k.insertBefore(p2, k.firstChild);
p2.onclick = function() { focus(p); }
// Setting title makes the tab flash sorta
document.title = document.title;
}
}
}
2014-10-24 15:51:53 -06:00
2014-07-24 10:56:37 -06:00
function focus(e) {
var pct = 1;
var timeout;
2014-10-24 15:51:53 -06:00
selectForum(e.parentNode);
2014-07-24 10:56:37 -06:00
e.scrollIntoView(false);
e.style.backgroundColor = "yellow";
2014-10-24 15:51:53 -06:00
2014-07-24 10:56:37 -06:00
timeout = setInterval(function() {
pct = pct - 0.1;
e.style.backgroundColor = "rgba(255, 255, 0, " + pct + ")";
if (pct <= 0) {
2014-10-24 15:51:53 -06:00
e.style.backgroundColor = "inherit";
2014-07-24 10:56:37 -06:00
clearInterval(timeout);
}
}, 50)
}
2014-07-23 21:15:04 -06:00
function addMessage(txt) {
var lhs = txt.split(" :", 1)[0]
var parts = lhs.split(' ')
var ts = new Date(parts[0] * 1000);
var fullSender = parts[1];
var command = parts[2];
var sender = parts[3];
var forum = parts[4];
var args = parts.slice(5);
var msg = txt.substr(lhs.length + 2)
2014-10-24 15:51:53 -06:00
var forumElement = getForumElement(forum);
2014-10-24 15:51:53 -06:00
var p = getTemplate("message");
2014-07-23 21:15:04 -06:00
addMessagePart(p, "timestamp", ts.toLocaleTimeString());
2014-10-24 15:51:53 -06:00
2014-07-23 21:15:04 -06:00
switch (command) {
case "PING":
case "PONG":
return;
break;
case "PRIVMSG":
addMessagePart(p, "forum", forum);
2014-07-24 10:34:02 -06:00
addMessagePart(p, "sender", sender);
addText(p, msg, (sender == forum));
break;
case "NOTICE":
addMessagePart(p, "forum", forum);
addMessagePart(p, "sender notice", sender);
addText(p, msg, (sender == forum));
2014-07-23 21:15:04 -06:00
break;
default:
addMessagePart(p, "forum", forum);
2014-07-24 10:34:02 -06:00
addMessagePart(p, "sender", sender);
2014-07-23 21:15:04 -06:00
addMessagePart(p, "raw", command + " " + args + " " + msg);
break;
}
2014-10-24 15:51:53 -06:00
while (forumElement.childNodes.length > scrollbackLength) {
2014-10-24 12:29:38 -06:00
forumElement.removeChild(forumElement.firstChild)
}
forumElement.appendChild(p);
2014-07-23 21:15:04 -06:00
p.scrollIntoView(false);
}
2014-10-24 15:51:53 -06:00
function newmsg(oEvent) {
msgs = oEvent.data.split("\n");
var first = Math.max(0, msgs.length - scrollbackLength);
for (var i = first; i < msgs.length; i += 1) {
2014-07-23 21:15:04 -06:00
addMessage(msgs[i]);
}
}
2014-10-24 15:51:53 -06:00
function handleInput(oEvent) {
console.log(oEvent);
2014-07-23 21:15:04 -06:00
var oReq = new XMLHttpRequest();
function reqListener() {
}
2014-10-24 15:51:53 -06:00
var txt = oEvent.target.value;
2014-10-23 21:21:54 -06:00
if (txt.startsWith("/connect ")) {
// XXX: should allow tokens with spaces
var parts = txt.split(" ");
connect(parts[1], parts[2], parts[3]);
2014-10-24 12:29:38 -06:00
} else {
oReq.onload = reqListener;
oReq.open("POST", window.postURL, true);
oReq.send(new FormData(event.target));
2014-10-23 21:21:54 -06:00
}
2014-10-24 15:51:53 -06:00
oEvent.target.value = "";
2014-07-23 21:15:04 -06:00
return false;
}
2014-10-23 21:21:54 -06:00
function connect(url, server, authtok) {
2014-10-24 15:51:53 -06:00
document.postURL = url;
2014-10-23 21:21:54 -06:00
var pullURL = url + "?server=" + server + "&auth=" + authtok
2014-10-24 12:29:38 -06:00
2014-10-24 15:51:53 -06:00
if (document.source != null) {
document.source.close();
2014-10-24 12:29:38 -06:00
}
2014-10-24 15:51:53 -06:00
document.source = new EventSource(pullURL);
document.source.onmessage = newmsg;
2014-10-24 12:29:38 -06:00
2014-10-24 15:51:53 -06:00
chrome.storage.sync.set({"connections": [[url, server, authtok]]});
2014-10-24 12:29:38 -06:00
}
function restore(items) {
2014-10-24 15:51:53 -06:00
var connections = items["connections"];
for (var k = 0; k < connections.length; k += 1) {
var conn = connections[k];
connect(conn[0], conn[1], conn[2]);
}
2014-10-23 21:21:54 -06:00
}
function init() {
2014-10-24 15:51:53 -06:00
chrome.storage.sync.get("connections", restore);
document.getElementById("input").addEventListener("change", handleInput);
document.templates = document.getElementById("templates");
document.rooms_list = document.getElementById("rooms-container").getElementsByClassName("rooms")[0];
2014-07-23 21:15:04 -06:00
}
2014-10-24 15:51:53 -06:00
window.addEventListener("load", init);