diff --git a/jquery.oembed.js b/jquery.oembed.js index 4643552..72ea3ac 100644 --- a/jquery.oembed.js +++ b/jquery.oembed.js @@ -1,4 +1,4 @@ -/*! +/*! * jquery oembed plugin * * Copyright (c) 2009 Richard Chamorro @@ -8,8 +8,8 @@ * Forked by Andrew Mee to Provide a slightly diffent kind of embedding * experience */ -(function($) { - $.fn.oembed = function(url, options, embedAction) { +(function($) { + $.fn.oembed = function(url, options, embedAction) { settings = $.extend(true, $.fn.oembed.defaults, options); var shortURLList = ["0rz.tw","1link.in","1url.com","2.gp","2big.at","2tu.us","3.ly","307.to","4ms.me","4sq.com","4url.cc","6url.com","7.ly","a.gg","a.nf","aa.cx","abcurl.net", @@ -35,21 +35,21 @@ "url.az", "url.co.uk", "url.ie", "url360.me", "url4.eu", "urlborg.com", "urlbrief.com", "urlcover.com", "urlcut.com", "urlenco.de", "urli.nl", "urls.im", "urlshorteningservicefortwitter.com", "urlx.ie", "urlzen.com", "usat.ly", "use.my", "vb.ly", "vgn.am", "vl.am", "vm.lc", "w55.de", "wapo.st", "wapurl.co.uk", "wipi.es", "wp.me", "x.vu", "xr.com", "xrl.in", "xrl.us", "xurl.es", "xurl.jp", "y.ahoo.it", "yatuc.com", "ye.pe", "yep.it", "yfrog.com", "yhoo.it", "yiyd.com", "youtu.be", "yuarel.com", - "z0p.de", "zi.ma", "zi.mu", "zipmyurl.com", "zud.me", "zurl.ws", "zz.gd", "zzang.kr", "›.ws", "✩.ws", "✿.ws", "❥.ws", "➔.ws", "➞.ws", "➡.ws", "➨.ws", "➯.ws", "➹.ws", "➽.ws"]; + "z0p.de", "zi.ma", "zi.mu", "zipmyurl.com", "zud.me", "zurl.ws", "zz.gd", "zzang.kr", "›.ws", "✩.ws", "✿.ws", "❥.ws", "➔.ws", "➞.ws", "➡.ws", "➨.ws", "➯.ws", "➹.ws", "➽.ws"]; - if ($('#jqoembeddata').length === 0) $('').appendTo('body'); + if ($('#jqoembeddata').length === 0) $('').appendTo('body'); - return this.each(function() { + return this.each(function() { var container = $(this), - resourceURL = (url && (!url.indexOf('http://') || !url.indexOf('https://'))) ? url : container.attr("href"), - provider; + resourceURL = (url && (!url.indexOf('http://') || !url.indexOf('https://'))) ? url : container.attr("href"), + provider; if (embedAction) { settings.onEmbed = embedAction; - } - else if (!settings.onEmbed){ - settings.onEmbed = function(oembedData) { + } + else if (!settings.onEmbed){ + settings.onEmbed = function(oembedData) { $.fn.oembed.insertCode(this, settings.embedMethod, oembedData); }; } @@ -70,7 +70,7 @@ }, success: function(data) { //this = $.fn.oembed; - resourceURL = data['long-url']; + resourceURL = data['long-url']; provider = $.fn.oembed.getOEmbedProvider(data['long-url']); if (provider !== null) { @@ -91,12 +91,12 @@ } provider = $.fn.oembed.getOEmbedProvider(resourceURL); - if (provider !== null) { + if (provider !== null) { provider.params = getNormalizedParams(settings[provider.name]) || {}; provider.maxWidth = settings.maxWidth; provider.maxHeight = settings.maxHeight; embedCode(container, resourceURL, provider); - } else { + } else { settings.onProviderNotFound.call(container, resourceURL); } } @@ -107,19 +107,19 @@ }; - var settings; + var settings; // Plugin defaults $.fn.oembed.defaults = { maxWidth: null, maxHeight: null, - includeHandle: true, - embedMethod: 'auto', - // "auto", "append", "fill" - onProviderNotFound: function() {}, - beforeEmbed: function() {}, - afterEmbed: function() {}, - onEmbed: false, + includeHandle: true, + embedMethod: 'auto', + // "auto", "append", "fill" + onProviderNotFound: function() {}, + beforeEmbed: function() {}, + afterEmbed: function() {}, + onEmbed: false, onError: function() {}, ajaxOptions: {} }; @@ -131,11 +131,11 @@ } function getRequestUrl(provider, externalUrl) { - var url = provider.apiendpoint, - qs = "", - i; + var url = provider.apiendpoint, + qs = "", + i; url += (url.indexOf("?") <= 0) ? "?" : "&"; - url = url.replace('#','%23'); + url = url.replace('#','%23'); if (provider.maxWidth !== null && (typeof provider.params.maxwidth === 'undefined' || provider.params.maxwidth === null)) { provider.params.maxwidth = provider.maxWidth; @@ -147,89 +147,89 @@ for (i in provider.params) { // We don't want them to jack everything up by changing the callback parameter - if (i == provider.callbackparameter) continue; + if (i == provider.callbackparameter) continue; // allows the options to be set to null, don't send null values to the server as parameters - if (provider.params[i] !== null) qs += "&" + escape(i) + "=" + provider.params[i]; + if (provider.params[i] !== null) qs += "&" + escape(i) + "=" + provider.params[i]; } url += "format=" + provider.format + "&url=" + escape(externalUrl) + qs; - if(provider.dataType!='json') url += "&" + provider.callbackparameter + "=?"; + if(provider.dataType!='json') url += "&" + provider.callbackparameter + "=?"; return url; - } - function success(oembedData, externalUrl, container) { - $('#jqoembeddata').data(externalUrl, oembedData.code); - settings.beforeEmbed.call(container, oembedData); - settings.onEmbed.call(container, oembedData); - settings.afterEmbed.call(container, oembedData); + } + function success(oembedData, externalUrl, container) { + $('#jqoembeddata').data(externalUrl, oembedData.code); + settings.beforeEmbed.call(container, oembedData); + settings.onEmbed.call(container, oembedData); + settings.afterEmbed.call(container, oembedData); } function embedCode(container, externalUrl, embedProvider) { if ($('#jqoembeddata').data(externalUrl)!=undefined && embedProvider.embedtag.tag!='iframe'){ - var oembedData = {code: $('#jqoembeddata').data(externalUrl)}; + var oembedData = {code: $('#jqoembeddata').data(externalUrl)}; success(oembedData, externalUrl, container); }else if (embedProvider.yql) { - var from = embedProvider.yql.from || 'htmlstring'; - var url = embedProvider.yql.url ? embedProvider.yql.url(externalUrl) : externalUrl; + var from = embedProvider.yql.from || 'htmlstring'; + var url = embedProvider.yql.url ? embedProvider.yql.url(externalUrl) : externalUrl; var query = 'SELECT * FROM ' + from + ' WHERE url="' + (url) + '"' + " and " + (/html/.test(from) ? 'xpath' : 'itemPath') + "='" + (embedProvider.yql.xpath || '/')+"'" ; - if(from=='html') query += " and compat='html5'"; - var ajaxopts = $.extend({ - url: "http://query.yahooapis.com/v1/public/yql", - dataType: 'jsonp', - data: { - q: query, - format: "json", - env: 'store://datatables.org/alltableswithkeys', - callback: "?" + if(from=='html') query += " and compat='html5'"; + var ajaxopts = $.extend({ + url: "http://query.yahooapis.com/v1/public/yql", + dataType: 'jsonp', + data: { + q: query, + format: "json", + env: 'store://datatables.org/alltableswithkeys', + callback: "?" }, success: function(data) { var result; if(embedProvider.yql.xpath && embedProvider.yql.xpath=='//meta|//title|//link'){ var meta={}; - if (data.query.results == null) { - data.query.results = {"meta": []}; - } + if (data.query.results == null) { + data.query.results = {"meta": []}; + } for(var i=0, l=data.query.results.meta.length; i'); - var oembedContainer = container.parent(); - if (settings.includeHandle) { - $('').insertBefore(container).click(function() { - var encodedString = encodeURIComponent($(this).text()); - $(this).html((encodedString == '%E2%86%91') ? '↓' : '↑'); - $(this).parent().children().last().toggle(); - }); - } - oembedContainer.append('
'); - try { - oembedData.code.clone().appendTo(oembedContainer); - } catch(e) { + switch (embedMethod) { + case "replace": + container.replaceWith(oembedData.code); + break; + case "fill": + container.html(oembedData.code); + break; + case "append": + container.wrap('
'); + var oembedContainer = container.parent(); + if (settings.includeHandle) { + $('').insertBefore(container).click(function() { + var encodedString = encodeURIComponent($(this).text()); + $(this).html((encodedString == '%E2%86%91') ? '↓' : '↑'); + $(this).parent().children().last().toggle(); + }); + } + oembedContainer.append('
'); + try { + oembedData.code.clone().appendTo(oembedContainer); + } catch(e) { oembedContainer.append(oembedData.code); - } + } /* Make videos semi-responsive * If parent div width less than embeded iframe video then iframe gets shrunk to fit smaller width * If parent div width greater thans embed iframe use the max widht @@ -370,46 +370,46 @@ } } } - break; + break; } }; - $.fn.oembed.getPhotoCode = function(url, oembedData) { + $.fn.oembed.getPhotoCode = function(url, oembedData) { var code, alt = oembedData.title ? oembedData.title : ''; alt += oembedData.author_name ? ' - ' + oembedData.author_name : ''; alt += oembedData.provider_name ? ' - ' + oembedData.provider_name : ''; code = '
' + alt + '
'; - if (oembedData.html) code += "
" + oembedData.html + "
"; + if (oembedData.html) code += "
" + oembedData.html + "
"; return code; }; - $.fn.oembed.getRichCode = function(url, oembedData) { + $.fn.oembed.getRichCode = function(url, oembedData) { var code = oembedData.html; return code; }; - $.fn.oembed.getGenericCode = function(url, oembedData) { - var title = (oembedData.title !== null) ? oembedData.title : url, - code = '' + title + ''; - if (oembedData.html) code += "
" + oembedData.html + "
"; + $.fn.oembed.getGenericCode = function(url, oembedData) { + var title = (oembedData.title !== null) ? oembedData.title : url, + code = '' + title + ''; + if (oembedData.html) code += "
" + oembedData.html + "
"; return code; }; - $.fn.oembed.getOEmbedProvider = function(url) { + $.fn.oembed.getOEmbedProvider = function(url) { for (var i = 0; i < $.fn.oembed.providers.length; i++) { for (var j = 0, l =$.fn.oembed.providers[i].urlschemes.length; j < l; j++) { var regExp = new RegExp($.fn.oembed.providers[i].urlschemes[j], "i"); if (url.match(regExp) !== null) return $.fn.oembed.providers[i]; - } + } } return null; }; - $.fn.oembed.OEmbedProvider = function(name, type, urlschemesarray, apiendpoint, extraSettings) { + $.fn.oembed.OEmbedProvider = function(name, type, urlschemesarray, apiendpoint, extraSettings) { this.name = name; this.type = type; // "photo", "video", "link", "rich", null this.urlschemes = urlschemesarray; - this.apiendpoint = apiendpoint; + this.apiendpoint = apiendpoint; this.maxWidth = 500; this.maxHeight = 400; extraSettings =extraSettings ||{}; @@ -426,11 +426,11 @@ extraSettings.yql = {from:'json' , apiendpoint: this.apiendpoint , url: function(externalurl){return this.apiendpoint+'?format=json&url='+externalurl} - , datareturn:function(results){ - if ("url" in results.json) { - return ''; - } - return results.json.html || '' + , datareturn:function(results){ + if (results.json.url || results.json.thumbnail_url) { + return ''; + } + return results.json.html || '' } }; } @@ -441,7 +441,7 @@ for (var property in extraSettings) { this[property] = extraSettings[property]; } - + this.format = this.format || 'json'; this.callbackparameter = this.callbackparameter || "callback"; this.embedtag = this.embedtag || {tag:""}; @@ -457,15 +457,15 @@ new $.fn.oembed.OEmbedProvider("youtubeiframe", "video", ["youtube.com/embed"], "$1?wmode=transparent", {templateRegex:/(.*)/,embedtag : {tag: 'iframe', width:'425',height: '349'}}), new $.fn.oembed.OEmbedProvider("wistia", "video", ["wistia.com/m/.+", "wistia.com/embed/.+","wi.st/m/.+","wi.st/embed/.+"], 'http://fast.wistia.com/oembed', {useYQL:'json'}), - new $.fn.oembed.OEmbedProvider("xtranormal", "video", ["xtranormal\\.com/watch/.+"], "http://www.xtranormal.com/xtraplayr/$1/$2", { + new $.fn.oembed.OEmbedProvider("xtranormal", "video", ["xtranormal\\.com/watch/.+"], "http://www.xtranormal.com/xtraplayr/$1/$2", { templateRegex: /.*com\/watch\/([\w\-]+)\/([\w\-]+).*/,embedtag: {tag: 'iframe',width: '320',height: '269'}}), new $.fn.oembed.OEmbedProvider("scivee", "video", ["scivee.tv/node/.+"], "http://www.scivee.tv/flash/embedCast.swf?", { templateRegex: /.*tv\/node\/(.+)/,embedtag: {width: '480',height: '400',flashvars:"id=$1&type=3"}}), new $.fn.oembed.OEmbedProvider("veoh", "video", ["veoh.com/watch/.+"], "http://www.veoh.com/swf/webplayer/WebPlayer.swf?version=AFrontend.5.7.0.1337&permalinkId=$1&player=videodetailsembedded&videoAutoPlay=0&id=anonymous", { templateRegex: /.*watch\/([^\?]+).*/,embedtag: {width: '410',height: '341'}}), - new $.fn.oembed.OEmbedProvider("gametrailers", "video", ["gametrailers\\.com/video/.+"], "http://media.mtvnservices.com/mgid:moses:video:gametrailers.com:$2", { + new $.fn.oembed.OEmbedProvider("gametrailers", "video", ["gametrailers\\.com/video/.+"], "http://media.mtvnservices.com/mgid:moses:video:gametrailers.com:$2", { templateRegex: /.*com\/video\/([\w\-]+)\/([\w\-]+).*/,embedtag: {width: '512',height: '288' }}), - new $.fn.oembed.OEmbedProvider("funnyordie", "video", ["funnyordie\\.com/videos/.+"], "http://player.ordienetworks.com/flash/fodplayer.swf?", { + new $.fn.oembed.OEmbedProvider("funnyordie", "video", ["funnyordie\\.com/videos/.+"], "http://player.ordienetworks.com/flash/fodplayer.swf?", { templateRegex: /.*videos\/([^\/]+)\/([^\/]+)?/,embedtag: {width: 512,height: 328,flashvars: "key=$1"}}), new $.fn.oembed.OEmbedProvider("colledgehumour", "video", ["collegehumor\\.com/video/.+"],"http://www.collegehumor.com/moogaloop/moogaloop.swf?clip_id=$1&use_node_id=true&fullscreen=1", {templateRegex:/.*video\/([^\/]+).*/ , embedtag : {width:600,height: 338}}), @@ -537,7 +537,7 @@ new $.fn.oembed.OEmbedProvider("flickr", "photo", ["flickr\\.com/photos/.+"], "http://flickr.com/services/oembed",{callbackparameter:'jsoncallback'}), new $.fn.oembed.OEmbedProvider("photobucket", "photo", ["photobucket\\.com/(albums|groups)/.+"], "http://photobucket.com/oembed/"), new $.fn.oembed.OEmbedProvider("instagram", "photo", ["instagr\\.?am(\\.com)?/.+"], "http://api.instagram.com/oembed"), - new $.fn.oembed.OEmbedProvider("yfrog", "photo", ["yfrog\\.(com|ru|com\\.tr|it|fr|co\\.il|co\\.uk|com\\.pl|pl|eu|us)/.+"], "http://www.yfrog.com/api/oembed",{useYQL:"json"}), + //new $.fn.oembed.OEmbedProvider("yfrog", "photo", ["yfrog\\.(com|ru|com\\.tr|it|fr|co\\.il|co\\.uk|com\\.pl|pl|eu|us)/.+"], "http://www.yfrog.com/api/oembed",{useYQL:"json"}), new $.fn.oembed.OEmbedProvider("SmugMug", "photo", ["smugmug.com/[-.\\w@]+/.+"], "http://api.smugmug.com/services/oembed/"), new $.fn.oembed.OEmbedProvider("dribbble", "photo", ["dribbble.com/shots/.+"], "http://api.dribbble.com/shots/$1?callback=?", @@ -546,18 +546,18 @@ return ''; } }), - new $.fn.oembed.OEmbedProvider("chart.ly", "photo", ["chart\\.ly/[a-z0-9]{6,8}"],"http://chart.ly/uploads/large_$1.png?", - {templateRegex:/.*ly\/([^\/]+).*/ , embedtag : {tag:'img'}}), - new $.fn.oembed.OEmbedProvider("circuitlab", "photo", ["circuitlab.com/circuit/.+"],"https://www.circuitlab.com/circuit/$1/screenshot/540x405/?", - {templateRegex:/.*circuit\/([^\/]+).*/ , embedtag : {tag:'img'}}), + new $.fn.oembed.OEmbedProvider("chart.ly", "photo", ["chart\\.ly/[a-z0-9]{6,8}"],"http://chart.ly/uploads/large_$1.png", + {templateRegex:/.*ly\/([^\/]+).*/ , embedtag : {tag:'img'},nocache:1}), + new $.fn.oembed.OEmbedProvider("circuitlab", "photo", ["circuitlab.com/circuit/.+"],"https://www.circuitlab.com/circuit/$1/screenshot/540x405/", + {templateRegex:/.*circuit\/([^\/]+).*/ , embedtag : {tag:'img'},nocache:1}), new $.fn.oembed.OEmbedProvider("23hq", "photo", ["23hq.com/[-.\\w@]+/photo/.+"],"http://www.23hq.com/23/oembed",{useYQL:"json"}), - new $.fn.oembed.OEmbedProvider("img.ly", "photo", ["img\\.ly/.+"],"http://img.ly/show/thumb/$1?", - {templateRegex:/.*com\/([^\/]+).*/ , embedtag : {tag:'img'} + new $.fn.oembed.OEmbedProvider("img.ly", "photo", ["img\\.ly/.+"],"http://img.ly/show/thumb/$1", + {templateRegex:/.*ly\/([^\/]+).*/ , embedtag : {tag:'img'},nocache:1 }), - new $.fn.oembed.OEmbedProvider("twitgoo.com", "photo", ["twitgoo\\.com/.+"],"http://twitgoo.com/show/thumb/$1?", - {templateRegex:/.*com\/([^\/]+).*/ , embedtag : {tag:'img'}}), - new $.fn.oembed.OEmbedProvider("imgur.com", "photo", ["imgur\\.com/gallery/.+"],"http://imgur.com/$1l.jpg?", - {templateRegex:/.*gallery\/([^\/]+).*/ , embedtag : {tag:'img'}}), + new $.fn.oembed.OEmbedProvider("twitgoo.com", "photo", ["twitgoo\\.com/.+"],"http://twitgoo.com/show/thumb/$1", + {templateRegex:/.*com\/([^\/]+).*/ , embedtag : {tag:'img'},nocache:1}), + new $.fn.oembed.OEmbedProvider("imgur.com", "photo", ["imgur\\.com/gallery/.+"],"http://imgur.com/$1l.jpg", + {templateRegex:/.*gallery\/([^\/]+).*/ , embedtag : {tag:'img'},nocache:1}), new $.fn.oembed.OEmbedProvider("visual.ly", "rich", ["visual\\.ly/.+"], null, {yql:{xpath:"//a[@id=\\'gc_article_graphic_image\\']/img", from:'htmlstring'} }), @@ -717,7 +717,7 @@ new $.fn.oembed.OEmbedProvider("amazon", "rich", ["amzn.com/B+","amazon.com.*/(B\\S+)($|\\/.*)"], "http://rcm.amazon.com/e/cm?t=_APIKEY_&o=1&p=8&l=as1&asins=$1&ref=qf_br_asin_til&fc1=000000&IS2=1<1=_blank&m=amazon&lc1=0000FF&bc1=000000&bg1=FFFFFF&f=ifr" ,{apikey: true,templateRegex:/.*\/(B[0-9A-Z]+)($|\/.*)/, embedtag : {tag: 'iframe', width:'120px',height: '240px'} - }), + }), new $.fn.oembed.OEmbedProvider("slideshare", "rich", ["slideshare\.net"], "http://www.slideshare.net/api/oembed/2",{format:'jsonp'}), new $.fn.oembed.OEmbedProvider("roomsharejp", "rich", ["roomshare\\.jp/(en/)?post/.*"], "http://roomshare.jp/oembed.json"), @@ -772,9 +772,9 @@ return code; } } - }) + }) - ]; + ]; })(jQuery); //This is needed for gravatar :(