Skip to content

Instantly share code, notes, and snippets.

@zalary
Forked from rickbenavidez/caveatPatchor.js
Created September 16, 2011 17:55
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 zalary/1222697 to your computer and use it in GitHub Desktop.
Save zalary/1222697 to your computer and use it in GitHub Desktop.
Propane caveatPatchor
/**
Sample Propane caveatPatchor.js file based on tmm1's avatar hack.
You'll need at least version 1.1.1 to experiment with this:
http://propaneapp.com/appcast/Propane.1.1.1.zip
Once I'm sure exposing this hack-injection point doesn't cause problems
I'll do an official auto-updating version.
As of version 1.1.1, 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.
Please don't tinker with enhancer.js - it's the glue that binds Campfire's
javascript to Propane's UI and features.
Also: this is totally and utterly unsupported. By all means you can
ask me questions via Propane's help forums but I can't debug your scripts
for you. My javascript isn't that good, trust me.
Finally, if Propane is acting broken in general and you've got hacks in
caveatPatchor.js, *please* tell me about your hacks up-front when opening
an issue on the support site.
*/
var displayAvatars = true;
var displayCloudAppImages = true;
var displayExpandedGists = true;
var githubber = true;
var HTML5Audio = true;
if (displayAvatars) {
Object.extend(Campfire.Message.prototype, {
addAvatar: function() {
if (this.actsLikeTextMessage()) {
var author = this.authorElement();
if (author.visible()) {
author.hide()
if (this.bodyCell.select('strong').length == 0) {
this.bodyCell.insert({top: '<strong>'+this.author()+'</strong><br>'})
author.insert({after: '<img alt="'+this.author()+'" width="32" height="32" align="top" style="margin-left: 5px; border-radius:3px" src="'+author.getAttribute('data-avatar')+'">'});
}
}
}
},
});
/* if you can wrap rather than rewrite, use swizzle like this: */
swizzle(Campfire.Message, {
setAuthorVisibilityInRelationTo: function($super, message) {
$super(message)
this.addAvatar();
},
});
/* defining a new responder is probably the best way to insulate your hacks from Campfire and Propane */
Campfire.AvatarMangler = Class.create({
initialize: function(chat) {
this.chat = chat;
chat.transcript.element.childElements().each(function(elem){
if (elem.match('tr')) {
var msg = new Campfire.Message(chat, elem)
msg.addAvatar()
}
})
this.chat.layoutmanager.layout();
this.chat.windowmanager.scrollToBottom();
},
onMessagesInserted: function(messages) {
var scrolledToBottom = this.chat.windowmanager.isScrolledToBottom();
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
message.addAvatar();
}
if (scrolledToBottom)
this.chat.windowmanager.scrollToBottom();
},
});
/* Here is how to install your responder into the running chat */
Campfire.Responders.push("AvatarMangler");
window.chat.installPropaneResponder("AvatarMangler", "avatarmangler");
}
/*
Display CloudApp 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.
*/
if (displayCloudAppImages) {
Campfire.CloudAppExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectCloudAppURL(messages[i]);
}
},
detectCloudAppURL: 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?:\/\/cl.ly\/[A-Za-z0-9]+\/?$/);
if (!match) return;
window.propane.requestJSON(message.id(), href, 'window.chat.cloudappexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
},
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
if (data['item_type'] === 'image') {
var imageURL = data['content_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.detectCloudAppURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectCloudAppURL(message);
}
});
Campfire.Responders.push("CloudAppExpander");
window.chat.installPropaneResponder("CloudAppExpander", "cloudappexpander");
}
if (displayExpandedGists) {
Campfire.GistExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectGistURL(messages[i]);
}
},
detectGistURL: 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:\/\/gist.github.com\/([0-9]+)\/?$/);
if (!match) return;
href = 'https://api.github.com/gists/' + match[1];
window.propane.requestJSON(message.id(), href, 'window.chat.gistexpander', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
},
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
var payload = '';
var description = data['description'];
if (description) {
payload += '<strong>Gist:</strong> ' + description + '<br />';
}
var files = data['files'];
if (files) {
payload += '<strong>Files:</strong> ';
for (var fileKey in files) {
var file = files[fileKey];
payload += '<a target="_blank" href="' + file['raw_url'] + '">' + fileKey + '</a> ';
}
}
message.resize((function() {
message.bodyCell.insert({bottom: '<div style="width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;">' + payload + '</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.detectGistURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectGistURL(message);
}
});
Campfire.Responders.push("GistExpander");
window.chat.installPropaneResponder("GistExpander", "gistexpander");
}
/*
Add github repo descriptions to your Campfire rooms in Propane (propaneapp.com).
Adapted from protocool's https://gist.github.com/825404
*/
if (githubber) {
// github repo info inline
Campfire.GitHubber = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for(var i = 0; i < messages.length; i++) {
this.detectGithubURL(messages[i]);
}
},
detectGithubURL: function(message) {
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'),
match = href.match(/^https?:\/\/github.com\//),
api = 'http://github.com/api/v2/json/repos/show/';
if (!match) return;
api += href.replace(/^https?:\/\/github.com\//,'');
window.propane.requestJSON(message.id(), api, 'window.chat.githubber', 'onEmbedDataLoaded', 'onEmbedDataFailed');
}
},
onEmbedDataLoaded: function(messageID, data) {
var message = window.chat.transcript.getMessageById(messageID);
if (!message) return;
message.resize((function() {
message.bodyCell.insert({bottom: '<div style="color: red; width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;">'+data.repository.description+'</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.detectGithubURL(messages[i]);
}
},
onMessageAccepted: function(message, messageID) {
this.detectGithubURL(message);
}
});
Campfire.Responders.push("GitHubber");
window.chat.installPropaneResponder("GitHubber", "githubber");
}
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