From 82f748855794d15ef84e836e80077be9233d68d4 Mon Sep 17 00:00:00 2001 From: Marius Halden Date: Mon, 11 Jan 2016 18:01:30 +0100 Subject: Initial commit --- irc-sse.js | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 irc-sse.js (limited to 'irc-sse.js') diff --git a/irc-sse.js b/irc-sse.js new file mode 100644 index 0000000..97d4663 --- /dev/null +++ b/irc-sse.js @@ -0,0 +1,183 @@ +var max_length = 50; +var default_channel = "#channel1" + +if (window.location.hash.length == 0 && default_channel.length > 0) { + window.location.replace(default_channel); +} + +var channel = ""; +var i; +if ((i = window.location.hash.indexOf("#")) >= 0) { + channel = window.location.hash.substring(i + 1); +} + +function scrollDown() { + $('html, body').animate({scrollTop: $('html, body').height()}, 1); +} + +function cleanOld() { + var elems = $("#chat li"); + if (elems.length > max_length) { + + var i = elems.length - max_length; + elems.each(function() { + if (i > 0) { + $(this).remove(); + i--; + return true; + } else { + return false; + } + }); + } +} + +function printMessage(msg) { + $("#chat").append("
  • "+msg+"
  • "); + cleanOld(); + scrollDown(); +} + +function setTopic(newTopic) { + $("#topic").children("h3").html(newTopic); + document.title = newTopic + " - IRC-SSE"; +} + +function getUser(user) { + return user.substring(0, user.indexOf('!')); +} + +function privmsg(msg) { + printMessage(msg.time + " <" + getUser(msg.user) + "> " + msg.message); +} + +function action(msg) { + printMessage(msg.time + " * " + getUser(msg.user) + " " + msg.action); +} + +function join(msg) { + printMessage(msg.time + " -!- " + msg.user + " joined"); +} + +function leave(msg) { + printMessage(msg.time + " -!- " + msg.user + " left"); +} + +function kick(msg) { + printMessage(msg.time + " -!- " + getUser(msg.kickee) + " was kicked by " + getUser(msg.kicker) + " reason: " + msg.message); +} + +function rename(msg) { + printMessage(msg.time + " -!- " + getUser(msg.oldname) + " is now known as " + getUser(msg.newname)); +} + +function mode(msg) { + var output = msg.time + " -!- mode/" + msg.channel + + " [" + ((msg.set == true)?"+":"-") + + msg.modes; + $.each(msg.args, function(i, v) { + if(v != null) { + output += " " + v; + } + }); + output += "] by " + getUser(msg.user); + printMessage(output); +} + +function topic(msg) { + var output = msg.time + " -!- " + msg.user + " changed topic to " + msg.topic; + printMessage(output); +} + +function daychange(msg) { + var output = msg.time + " Day changed to " + msg.date; + printMessage(output); +} + +function parseMsg(msg) { + switch(msg.type) { + case 'action': + action(msg); + break; + case 'topic': + topic(msg); + setTopic(msg.topic); + break; + case 'privmsg': + privmsg(msg); + break; + case 'mode': + mode(msg); + break; + case 'leave': + leave(msg); + break; + case 'join': + join(msg); + break; + case 'kick': + kick(msg); + break; + case 'rename': + rename(msg); + break; + case 'daychange': + daychange(msg); + break; + case 'refresh': + location.reload(); + break; + default: + break; + } +} + +$(document).ready(function() { + if (channel.length > 0) { + $.ajax({ + url: "/backlog?" + channel, + // crossDomain: true, + dataType: "json", + success: function(data) { + setTopic(data.topic); + + $.each(data.msg, function(i, msg) { + parseMsg(msg); + }); + } + }); + } + + $(window).on('hashchange', function() { + location.reload(); + }); +}); + +;(function($) { + $.fn.textfill = function(options) { + var fontSize = options.maxFontPixels; + var ourText = $('#chat', this); + var maxHeight = $(this).height(); + var maxWidth = $(this).width(); + var textHeight; + var textWidth; + + do { + ourText.css('font-size', fontSize); + textHeight = ourText.height(); + textWidth = ourText.width(); + fontSize = fontSize - 1; + } while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize > 3); + + return this; + } +})(jQuery); + +//$(document).ready(function() { +// $("#chat-container").textfill({ maxFontPixels: 100 }); +//}); + +var evtSrc = new EventSource("/subscribe?" + channel); +evtSrc.onmessage = function(e) { + parseMsg($.parseJSON(e.data)) +} -- cgit v1.2.3