Skip to content

Instantly share code, notes, and snippets.

@protocool
Created May 26, 2011 19:17
Show Gist options
  • Save protocool/993836 to your computer and use it in GitHub Desktop.
Save protocool/993836 to your computer and use it in GitHub Desktop.

Revisions

  1. protocool created this gist May 26, 2011.
    40 changes: 40 additions & 0 deletions displayDataURLImages_fragment.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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");
    }