Skip to content

Instantly share code, notes, and snippets.

@phred
Forked from protocool/caveatPatchor.js
Created October 6, 2011 17:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phred/1268036 to your computer and use it in GitHub Desktop.
Save phred/1268036 to your computer and use it in GitHub Desktop.
caveatPatchor.js with barebones oohembed (now embedly?!) support for Propane 1.1.2 and above
/*
As of version 1.1.2, Propane will load and execute the contents of
~Library/Application Support/Propane/unsupported/caveatPatchor.js
immediately following the execution of its own enhancer.js file.
You can use this mechanism to add your own customizations to Campfire
in Propane.
Below you'll find two customization examples.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
var displayEmbedly = true;
var embedlyAPIKey = "1996c0f2f03811e0bfbf4040d3dc5c07"; // Take my API key, please!
/*
Display Embedly images, videos, and other embeds 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.
*/
if (displayEmbedly) {
var embedlyURL = 'http://api.embed.ly/1/oembed?key=' + embedlyAPIKey + '&url=';
Campfire.EmbedlyExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectEmbedlyURL(messages[i]);
}
},
detectEmbedlyURL: 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');
window.propane.requestJSON(message.id(), embedlyURL + encodeURIComponent(href), 'window.chat.embedlyexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
},
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
var links = message.bodyElement().select('a:not(image)');
var href = links[0].getAttribute('href'); // Link back to the original URL
if (data['thumbnail_url']) {
var imageURL = data['thumbnail_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="'+href+'" 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.detectEmbedlyURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectEmbedlyURL(message);
}
});
Campfire.Responders.push("EmbedlyExpander");
window.chat.installPropaneResponder("EmbedlyExpander", "embedlyexpander");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment