Skip to content

Instantly share code, notes, and snippets.

@pjaspers
Created July 17, 2012 22:23
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 pjaspers/6dcf1366df5203157fda to your computer and use it in GitHub Desktop.
Save pjaspers/6dcf1366df5203157fda to your computer and use it in GitHub Desktop.
/*
* Flickr
* Adds support for Flickr links
*
*/
/*
Display Flick images inline.
*/
Campfire.FlickrExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectFlickrURL(messages[i]);
}
},
detectFlickrURL: 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?:\/\/(www.)?flickr.com\/[A-Za-z0-9]+\/[A-Za-z0-9]+\/[A-Za-z0-9]+\/?$/);
if (!match) return;
var ombed_url = "http://www.flickr.com/services/oembed/?url=" + match[0] + "&format=json";
window.propane.requestJSON(message.id(), ombed_url, 'window.chat.flickrexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
},
// {"version":"1.0","type":"photo","author_url":"http:\/\/www.flickr.com\/photos\/pjaspers\/","cache_age":3600,"provider_name":"Flickr","provider_url":"http:\/\/www.flickr.com\/","title":"Pier 39","author_name":"pjaspers","width":"500","height":"500","url":"http:\/\/farm6.static.flickr.com\/5304\/5600721689_fb9f300a33.jpg"}
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
if (data['type'] === 'photo') {
var imageURL = data['url'];
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(&quot;inlineImageLoaded&quot;, this)" onerror="$dispatch(&quot;inlineImageLoadFailed&quot;, 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.detectFlickrURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectFlickrURL(messages[i]);
}
});
Campfire.Responders.push("FlickrExpander");
window.chat.installPropaneResponder("FlickrExpander", "flickrexpander");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment