Skip to content

Instantly share code, notes, and snippets.

@cheeaun
Created November 29, 2009 03:06
Show Gist options
  • Save cheeaun/244773 to your computer and use it in GitHub Desktop.
Save cheeaun/244773 to your computer and use it in GitHub Desktop.
oohEmbed plugin for Talkerapp
// https://cheeaun.talkerapp.com/plugins/71
// http://code.google.com/p/jquery-oembed/
// Closure compiled jquery.oembed.js (simple)
(function(e){function n(b){for(var a=0;a<m.length;a++)if(m[a].matches(b))return m[a];return null}function f(b,a,c,i){this.name=b;this.urlPattern=a;this.oEmbedUrl=c!=null?c:"http://oohembed.com/oohembed/";this.callbackparameter=i!=null?i:"callback";this.maxWidth=500;this.maxHeight=400;this.matches=function(g){return g.indexOf(this.urlPattern)>=0};this.getRequestUrl=function(g){var d=this.oEmbedUrl;if(d.indexOf("?")<=0)d+="?";var j="";for(var h in this.params)if(h!=this.callbackparameter)if(this.params[h]!= null)j+="&"+escape(h)+"="+this.params[h];d+="format=json";if(this.maxWidth!=null)d+="&maxwidth="+this.maxWidth;if(this.maxHeight!=null)d+="&maxheight="+this.maxHeight;d+="&url="+escape(g)+j+"&"+this.callbackparameter+"=?";return d};this.embedCode=function(g,d,j){var h=this.getRequestUrl(d);e.getJSON(h,function(k){var l=e.extend(k),o=k.type;switch(o){case "photo":l.code=e.fn.oembed.getPhotoCode(d,k);break;case "video":l.code=e.fn.oembed.getVideoCode(d,k);break;case "rich":l.code=e.fn.oembed.getRichCode(d, k);break;default:l.code=e.fn.oembed.getGenericCode(d,k);break}j(g,l)})}}e.fn.oembed=function(b,a,c){a=e.extend(true,e.fn.oembed.defaults,a);return this.each(function(){var i=e(this),g=b!=null?b:i.attr("href"),d;c||(c=function(j,h){e.fn.oembed.insertCode(j,a.embedMethod,h)});if(g!=null){d=n(g);if(d!=null){d.maxWidth=a.maxWidth;d.maxHeight=a.maxHeight;d.params=a[d.name]||{};d.embedCode(i,g,c);return}}c(i,null)})};e.fn.oembed.defaults={maxWidth:null,maxHeight:null,embedMethod:"replace"};e.fn.oembed.insertCode= function(b,a,c){switch(a){case "auto":b.attr("href")!=null?insertCode(b,"append",c):insertCode(b,"replace",c);break;case "replace":b.replaceWith(c.code);break;case "fill":b.html(c.code);break;case "append":a=b.next();if(a==null||!a.hasClass("oembed-container")){a=b.after('<div class="oembed-container"></div>').next(".oembed-container");c!=null&&c.provider_name!=null&&a.toggleClass("oembed-container-"+c.provider_name)}a.html(c.code);break}};e.fn.oembed.getPhotoCode=function(b,a){var c=a.title?a.title: "";c+=a.author_name?" - "+a.author_name:"";c+=a.provider_name?" - "+a.provider_name:"";b='<div><a href="'+b+'" target="_blank"><img src="'+a.url+'" alt="'+c+'"/></a></div>';if(a.html)b+="<div>"+a.html+"</div>";return b};e.fn.oembed.getVideoCode=function(b,a){return b=a.html};e.fn.oembed.getRichCode=function(b,a){return b=a.html};e.fn.oembed.getGenericCode=function(b,a){var c=a.title!=null?a.title:b;b='<a href="'+b+'">'+c+"</a>";if(a.html)b+="<div>"+a.html+"</div>";return b};e.fn.oembed.isAvailable= function(b){b=n(b);return b!=null};var m=[new f("fivemin","5min.com"),new f("amazon","amazon.com"),new f("bliptv","blip.tv"),new f("collegehumor", "collegehumor.com/video"),new f("thedailyshow", "thedailyshow.com/"),new f("dailymotion", "dailymotion.com"),new f("flickr","flickr","http://flickr.com/services/oembed","jsoncallback"),new f("funnyordie", "funnyordie.com/videos"),new f("googlevideo","video.google."),new f("hulu","hulu.com"),new f("livejournaluserpic", ".livejournal.com"),new f("imdb","imdb.com"),new f("metacafe","metacafe.com/watch"),new f("nfbca", "nfb.ca/film"),new f("phodroid", "phodroid.com"),new f("qik","qik.com"),new f("revision3","revision3.com"),new f("scribd", "scribd.com"),new f("slideshare","slideshare.net"),new f("twitpic","twitpic.com"),new f("viddler","viddler.com"),new f("vimeo","vimeo.com","http://vimeo.com/api/oembed.json"), new f("wikipedia","wikipedia.org/wiki"),new f("wordpress",".wordpress.com"),new f("xkcd","xkcd.com"),new f("yfrog","yfrog."),new f("youtube","youtube.com/watch")]})(jQuery);
var pattern = /^(https?:\/\/[\w\-:;?&=+.%#\/]+)$/gi;
plugin.onMessageInsertion = function(event){
if (event.type != 'message') return;
var lastIns = Talker.getLastInsertion();
var embedMethod = 'fill';
var content = jQuery.trim(lastIns.text());
if (!content.match(pattern)){
var links = lastIns.find('a[href]:first');
if (!links.length) return;
var content = links.attr('href');
if (!content.match(pattern)) return;
embedMethod = 'append';
}
try { // exception if url is not oembedded
lastIns.oembed(content, {embedMethod: embedMethod});
setTimeout(function(){
// Handle the super huge videos
var max = 800;
$('.message object[width]:not(._oohembed_small), .message embed[width]:not(._oohembed_small)').each(function(){
var el = $(this).addClass('_oohembed_small');
var width = el.attr('width');
if (width<=max) return;
var divisor = Math.ceil(width/max);
var w = width/divisor;
var height = el.attr('height');
var h = height/divisor;
el.attr('width', w).attr('height', h);
});
}, 5000);
} catch(e) {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment