Skip to content

Instantly share code, notes, and snippets.

@dlackty
Created December 26, 2012 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlackty/4377786 to your computer and use it in GitHub Desktop.
Save dlackty/4377786 to your computer and use it in GitHub Desktop.
Propane caveatPatchor.js snippet to embed SpeakerDeck slide inline.
Campfire.SpeakerDeckExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectSpeakerDeckURL(messages[i]);
}
},
detectSpeakerDeckURL: function(message) {
/* we are going to use the messageID to uniquely identify our requestJSON request
so we don't check pending messages */
if (!message.pending() && message.kind === 'text') {
var links = message.bodyElement().select('a:not(image)');
if (links.length != 1) {
return;
}
var href = links[0].getAttribute('href');
var match = href.match(/^https?:\/\/speakerdeck.com\/[A-Za-z0-9-]+\/[A-Za-z0-9-]+\/?$/);
if (!match) return;
window.propane.requestJSON(message.id(), 'https://speakerdeck.com/oembed.json?url=' + href, 'window.chat.speakerdeckexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
},
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
if (data['html']) {
var resized = data['html'].replace(/width="[0-9]+"/, 'width="320"').replace(/height="[0-9]+"/, 'height="302"');
message.resize((function() {
message.bodyCell.insert({bottom: resized});
}).bind(this));
}
},
onEmbedDataFailed: function(messageID) {
/* No cleanup required, we only alter the HTML after we get back a succesful load from the data */
},
onMessagesInsertedBeforeDisplay: function(messages) {
for (var i = 0; i < messages.length; i++) {
this.detectSpeakerDeckURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectSpeakerDeckURL(message);
}
});
Campfire.Responders.push("SpeakerDeckExpander");
window.chat.installPropaneResponder("SpeakerDeckExpander", "speakerdeckexpander");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment