Created
November 6, 2013 15:54
-
-
Save benvisser/7338546 to your computer and use it in GitHub Desktop.
Add inline Droplr previews to Propane for mac. Go to the propane application, command click and choose show package contents. Click Contents > Resources > enhancer.js. Add the code to the bottom of this file inside of window.propane. Just know that when you update propane this file will get overwritten and you'll have to do this again.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Display Droplr images inline. | |
This responder illustrates using Propane's requestJSON service to request | |
JSON from remote (non-authenticated) servers and have the results passed | |
to a callback of your choosing. | |
*/ | |
var displayDroplrImages = true; | |
if (displayDroplrImages) { | |
Campfire.DroplrExpander = Class.create({ | |
initialize: function(chat) { | |
this.chat = chat; | |
var messages = this.chat.transcript.messages; | |
for (var i = 0; i < messages.length; i++) { | |
this.detectDroplrURL(messages[i]); | |
} | |
}, | |
detectDroplrURL: 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(/^http?:\/\/d.pr\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\/?$/); | |
if (!match) return; | |
//alert(match[2]); | |
//var url = "http://droplr-json.heroku.com/"+match[2]; | |
var url = "http://dev.socialdesignhouse.com/droplr/droplr-json.php?type="+match[1]+"&drop="+match[2]; | |
window.propane.requestJSON(message.id(), url, 'window.chat.droplrexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed'); | |
} | |
}, | |
onEmbedDataLoaded: function(messageID, data) { | |
//alert(data.image); | |
var message = window.chat.transcript.getMessageById(messageID); | |
if (!message) return; | |
if (data['type'] === 'image') { | |
var imageURL = data.src; | |
message.resize((function() { | |
message.bodyCell.insert({bottom: '<div style="width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;"><a href="'+imageURL+'" class="image loading" target="_blank">' + '<img src="'+imageURL+'" onload="$dispatch("inlineImageLoaded", this)" onerror="$dispatch("inlineImageLoadFailed", this)" /></a></div>'}); | |
}).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.detectDroplrURL(messages[i]); | |
} | |
}, | |
onMessageAccepted: function(message, messageID) { | |
this.detectDroplrURL(message); | |
} | |
}); | |
Campfire.Responders.push("DroplrExpander"); | |
window.chat.installPropaneResponder("DroplrExpander", "droplrexpander"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment