public
Last active

Propane caveatPatchor.js hack to display data:image urls inline as images

  • Download Gist
displayDataURLImages_fragment.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
if (displayDataURLImages) {
 
Campfire.DataImageURLExpander = Class.create({
initialize: function(chat) {
this.chat = chat;
var messages = this.chat.transcript.messages;
for (var i = 0; i < messages.length; i++) {
this.detectDataURLImage(messages[i]);
}
},
 
detectDataURLImage: function(message) {
if (!message.pending() && message.kind === 'text') {
var links = message.bodyElement().select('a');
if (links.length > 0) {
return;
}
var bodyContent = message.bodyElement().textContent;
var match = bodyContent.match(/^data:image\/\w+;base64/);
if (!match) return;
message.resize((function() {
message.bodyCell.insert({bottom: '<div style="width:100%; margin-top:5px; padding-top: 5px; border-top:1px dotted #ccc;"><img src="'+bodyContent+'" onload="$dispatch(&quot;inlineImageLoaded&quot;, this)" onerror="$dispatch(&quot;inlineImageLoadFailed&quot;, this)" /></div>'});
}).bind(this));
}
},
 
onMessagesInsertedBeforeDisplay: function(messages) {
for (var i = 0; i < messages.length; i++) {
this.detectDataURLImage(messages[i]);
}
},
 
onMessageAccepted: function(message, messageID) {
this.detectDataURLImage(message);
}
});
 
Campfire.Responders.push("DataImageURLExpander");
window.chat.installPropaneResponder("DataImageURLExpander", "dataimageurlexpander");
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.