diff --git a/lib/ns_client.js b/lib/ns_client.js index 803186b..0e552fd 100644 --- a/lib/ns_client.js +++ b/lib/ns_client.js @@ -48,9 +48,9 @@ var NsClient = function() { mo.socket = mch[1]; mo.login = mch[2]; mo.host = mch[3]; - mo.location = decodeURI(mch[4]); + mo.location = elem.msgDecode(mch[4]); mo.group = mch[5]; - mo.message = decodeURI(mch[6]); + mo.message = elem.msgDecode(mch[6]); elem.msgHandler(mo); } @@ -60,6 +60,18 @@ var NsClient = function() { })(this); }; +NsClient.prototype.msgDecode = function(msg) { + msg = unescape(msg) + // TODO: unescape extra characters: @*/+ + return msg; +}; + +NsClient.prototype.msgEncode = function(msg) { + msg = escape(msg) + // TODO: escape extra characters: @*/+ + return msg; +}; + NsClient.prototype.connect = function() { if (!this.is_connected) { this.storage.get(null, (function(elem) { @@ -139,20 +151,28 @@ NsClient.prototype.daemonize = function() { } }; +NsClient.prototype.sendMessage = function(to, message) { + var msg = "user_cmd msg_user " + to + " msg " + this.msgEncode(message) + "\n"; + + chrome.socket.write(this.socket, str2ab(msg), function(w_inf) {}); +}; + NsClient.prototype.updateStatus = function() { var status_txt = document.getElementById("status_txt"); if (this.is_connected) { - if (status_txt !== null) + if (status_txt !== null) { status_txt.innerHTML = this.state; + } var status_msg = "user_cmd state "; status_msg += this.state + ":"; status_msg += Math.round(new Date().getTime() / 1000) + "\n"; chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {}); } else { - if (status_txt !== null) + if (status_txt !== null) { status_txt.innerHTML = 'disconnected'; + } } }; diff --git a/lib/nsui.js b/lib/nsui.js index e3a3392..68bf69b 100644 --- a/lib/nsui.js +++ b/lib/nsui.js @@ -133,9 +133,11 @@ Nsui.prototype.init = function() { }; })(this); - NsClient.prototype.msgHandler = (function(elem) { - return function(msg) { - elem.addContentToTab(msg.login, msg); - }; - })(this); + if (typeof $cs.client !== "undefined") { + NsClient.prototype.msgHandler = (function(elem) { + return function(msg) { + elem.addContentToTab(msg.login, msg); + }; + })(this); + } }; diff --git a/lib/tab.nsui.js b/lib/tab.nsui.js index d7bf950..52ff30a 100644 --- a/lib/tab.nsui.js +++ b/lib/tab.nsui.js @@ -70,6 +70,20 @@ Tab.prototype.initBodyElement = function() { chat_input_w.classList.add("chat-input-wrapper"); chat_input.classList.add("chat-input"); chat_input.setAttribute("type", "text"); + chat_input.addEventListener("keyup", (function(elem) { + return function(event) { + if (event.keyCode == 13 && this.value != "") { + var msg = this.value; + this.value = ""; + + if (typeof $cs.client !== "undefined") { + $cs.client.sendMessage(elem.name, msg); + } else { + console.error("chromesoul client not found"); + } + } + }; + })(this), false); chat_input_w.appendChild(chat_input); this.el_body.appendChild(this.chat_log); diff --git a/start.js b/start.js index fc4295c..7590464 100644 --- a/start.js +++ b/start.js @@ -1,17 +1,18 @@ -(function() { - var om, ui, cs; +var chromesoul = (function() { + var chromesoul = { + "opts": new OptionsManager(), + "ui": new Nsui(), + "client": new NsClient() + }; - om = new OptionsManager(); - om.init(); - - ui = new Nsui(); - ui.init(); - - cs = new NsClient(); - cs.init(); - - ui.setReconnect(function() { - cs.disconnect(); - cs.connect(); - }); + return (window.chromesoul = window.$cs = chromesoul); })(); + +$cs.opts.init(); +$cs.ui.init(); +$cs.client.init(); + +$cs.ui.setReconnect(function() { + $cs.client.disconnect(); + $cs.client.connect(); +});