Skip to content

Instantly share code, notes, and snippets.

@mojodna
Created October 14, 2011 23:14
Show Gist options
  • Save mojodna/1288645 to your computer and use it in GitHub Desktop.
Save mojodna/1288645 to your computer and use it in GitHub Desktop.
caveatPatchor.js with html5 audio support
var HTML5Audio = true;
if (HTML5Audio) {
Campfire.HTML5Audio = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for(var i = 0; i < messages.length; i++) {
this.detectAudio(messages[i], false);
}
},
detectAudio: function(message, autoplay) {
if (!message.pending() && message.kind === 'text') {
var links = message.bodyElement().select('a:not(image)');
if (links.length != 1) return;
var audio_url = links[0].getAttribute('href');
var match = audio_url.match(/\.(wav|mp3|m4a)$/);
if (!match) return;
var audio = '';
if (autoplay) {
audio = '<audio autoplay="autoplay" controls="controls"><source src="' + audio_url + '" /></audio>';
} else {
audio = '<audio controls="controls"><source src="' + audio_url + '" /></audio>';
}
message.resize((function() {
message.bodyCell.insert({ bottom: audio });
}).bind(this));
}
},
onMessagesInsertedBeforeDisplay: function(messages) {
for (var i = 0; i < messages.length; i++) {
this.detectAudio(messages[i], true);
}
},
onMessageAccepted: function(message, messageID) {
this.detectAudio(message, true);
}
});
Campfire.Responders.push("HTML5Audio");
window.chat.installPropaneResponder("HTML5Audio", "html5audio");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment