Commit bc4a2428 authored by JC Brand's avatar JC Brand

Automatically convert URLs in chat messages to hyperlinks

parent a723a394
...@@ -212,45 +212,28 @@ ...@@ -212,45 +212,28 @@
'<span class="chat-message-content">{{message}}</span>' + '<span class="chat-message-content">{{message}}</span>' +
'</div>'), '</div>'),
autoLink: function (text) {
// Convert URLs into hyperlinks
var re = /((http|https|ftp):\/\/[\w?=&.\/\-;#~%\-]+(?![\w\s?&.\/;#~%"=\-]*>))/g;
return text.replace(re, '<a target="_blank" href="$1">$1</a>');
},
appendMessage: function (message) { appendMessage: function (message) {
var time, var now = new Date(),
now = new Date(), time = now.toLocaleTimeString().substring(0,5),
minutes = now.getMinutes().toString(), minutes = now.getMinutes().toString(),
list, $chat_content = $(this.el).find('.chat-content');
$chat_content,
match;
message = message.replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/\"/g,"&quot;").replace(/^\s*/, "");
list = message.match(/\b(http:\/\/www\.\S+\.\w+|www\.\S+\.\w+|http:\/\/(?=[^w]){3}\S+[\.:]\S+)[^ ]+\b/g);
if (list) {
for (i = 0; i < list.length; i++) {
message = message.replace(list[i], "<a target='_blank' href='" + escape( list[i] ) + "'>"+ list[i] + "</a>" );
}
}
message = this.autoLink(message);
if (minutes.length==1) {minutes = '0'+minutes;} if (minutes.length==1) {minutes = '0'+minutes;}
time = now.toLocaleTimeString().substring(0,5);
$chat_content = $(this.el).find('.chat-content');
$chat_content.find('div.chat-event').remove(); $chat_content.find('div.chat-event').remove();
$chat_content.append(this.message_template({
match = message.match(/^\/(.*?)(?: (.*))?$/); 'sender': 'me',
if ((match) && (match[1] === 'me')) { 'time': time,
message = message.replace(/^\/me/, '*'+xmppchat.username); 'message': message,
$chat_content.append(this.action_template({ 'username': 'me',
'sender': 'me', 'extra_classes': ''
'time': time, }));
'message': message,
'username': xmppchat.username,
'extra_classes': ''
}));
} else {
$chat_content.append(this.message_template({
'sender': 'me',
'time': time,
'message': message,
'username': 'me',
'extra_classes': ''
}));
}
$chat_content.scrollTop($chat_content[0].scrollHeight); $chat_content.scrollTop($chat_content[0].scrollHeight);
}, },
...@@ -265,7 +248,7 @@ ...@@ -265,7 +248,7 @@
/* XXX: event.mtype should be 'xhtml' for XHTML-IM messages, /* XXX: event.mtype should be 'xhtml' for XHTML-IM messages,
but I only seem to get 'text'. but I only seem to get 'text'.
*/ */
var body = $message.children('body').text(), var body = this.autoLink($message.children('body').text()),
from = Strophe.getBareJidFromJid($message.attr('from')), from = Strophe.getBareJidFromJid($message.attr('from')),
to = $message.attr('to'), to = $message.attr('to'),
composing = $message.find('composing'), composing = $message.find('composing'),
...@@ -318,14 +301,11 @@ ...@@ -318,14 +301,11 @@
insertClientStoredMessages: function () { insertClientStoredMessages: function () {
var msgs = xmppchat.storage.getMessages(this.model.get('jid')), var msgs = xmppchat.storage.getMessages(this.model.get('jid')),
$content = this.$el.find('.chat-content'), i; $content = this.$el.find('.chat-content'), i;
for (i=0; i<_.size(msgs); i++) { for (i=0; i<_.size(msgs); i++) {
var msg = msgs[i], var msg = msgs[i],
msg_array = msg.split(' ', 2), msg_array = msg.split(' ', 2),
date = msg_array[0], date = msg_array[0];
match; msg = this.autoLink(String(msg).replace(/(.*?\s.*?\s)/, ''));
msg = String(msg).replace(/(.*?\s.*?\s)/, '');
match = msg.match(/^\/(.*?)(?: (.*))?$/);
if (msg_array[1] == 'to') { if (msg_array[1] == 'to') {
$content.append( $content.append(
this.message_template({ this.message_template({
...@@ -343,7 +323,7 @@ ...@@ -343,7 +323,7 @@
'message': msg, 'message': msg,
'username': this.model.get('fullname').split(' ')[0], 'username': this.model.get('fullname').split(' ')[0],
'extra_classes': 'delayed' 'extra_classes': 'delayed'
})); }));
} }
} }
}, },
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment