1 | (function() { |
---|
2 | var settings = { |
---|
3 | maxWidth: null, |
---|
4 | maxHeight: null, |
---|
5 | embedMethod: 'auto', // auto|append|replace |
---|
6 | defaultOEmbedProvider: 'embed.ly', // oohembed|embed.ly|none |
---|
7 | allowedProviders: null, |
---|
8 | disallowedProviders: null, |
---|
9 | customProviders: null, // [ new $.oembed('OEmbedProvider','customprovider', null, ['customprovider\\.com/watch.+v=[\\w-]+&?']) ] |
---|
10 | greedy: false, |
---|
11 | onProviderNotFound: function() {}, |
---|
12 | beforeEmbed: function() {}, |
---|
13 | afterEmbed: function() {}, |
---|
14 | onEmbed: function() {}, |
---|
15 | onError: function() {}, |
---|
16 | ajaxOptions: {} |
---|
17 | }; |
---|
18 | |
---|
19 | var publicMethods = { |
---|
20 | init: function(url,options) { |
---|
21 | $.extend(settings,options); |
---|
22 | |
---|
23 | internalMethods._init(); |
---|
24 | |
---|
25 | return this.each(function(){ |
---|
26 | var container = $(this); |
---|
27 | var resourceURL = (url != null) ? url : container.attr('href'); |
---|
28 | var provider = null; |
---|
29 | |
---|
30 | if (!options.onEmbed) { |
---|
31 | settings.onEmbed = function (data) { |
---|
32 | internalMethods._insertCode(this,settings.embedMethod,data); |
---|
33 | }; |
---|
34 | } |
---|
35 | |
---|
36 | if (resourceURL != null) { |
---|
37 | provider = publicMethods.getOEmbedProvider(resourceURL); |
---|
38 | |
---|
39 | if (provider != null) { |
---|
40 | provider.params = internalMethods._getNormalizedParams(settings[provider.name]) || {}; |
---|
41 | provider.maxWidth = settings.maxWidth; |
---|
42 | provider.maxHeight = settings.maxHeight; |
---|
43 | internalMethods._doRequest(container,resourceURL,provider); |
---|
44 | } else { |
---|
45 | settings.onProviderNotFound.call(container, resourceURL); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | return container; |
---|
50 | }); |
---|
51 | }, |
---|
52 | |
---|
53 | OEmbedProvider: function(name,type,urlschemes,apiendpoint,callbackparameter) { |
---|
54 | this.name = name; |
---|
55 | this.type = type; // 'photo', 'video', 'link', 'rich', null |
---|
56 | this.urlschemes = getUrlSchemes(urlschemes); |
---|
57 | this.apiendpoint = apiendpoint; |
---|
58 | this.callbackparameter = callbackparameter; |
---|
59 | this.maxWidth = 500; |
---|
60 | this.maxHeight = 400; |
---|
61 | this.enable = true; |
---|
62 | var i, property, regExp; |
---|
63 | |
---|
64 | this.matches = function (externalUrl) { |
---|
65 | for (i = 0; i < this.urlschemes.length; i++) { |
---|
66 | regExp = new RegExp(this.urlschemes[i], 'i'); |
---|
67 | if (externalUrl.match(regExp) != null) { |
---|
68 | return true; |
---|
69 | } |
---|
70 | } |
---|
71 | return false; |
---|
72 | }; |
---|
73 | |
---|
74 | this.fromJSON = function (json) { |
---|
75 | for (property in json) { |
---|
76 | if (property != 'urlschemes') { |
---|
77 | this[property] = json[property]; |
---|
78 | } else { |
---|
79 | this[property] = getUrlSchemes(json[property]); |
---|
80 | } |
---|
81 | } |
---|
82 | return true; |
---|
83 | }; |
---|
84 | |
---|
85 | function getUrlSchemes(urls) { |
---|
86 | if (internalMethods._isNullOrEmpty(urls)) { |
---|
87 | return ['.']; |
---|
88 | } |
---|
89 | if ($.isArray(urls)) { |
---|
90 | return urls; |
---|
91 | } |
---|
92 | return urls.split(';'); |
---|
93 | } |
---|
94 | }, |
---|
95 | |
---|
96 | getOEmbedProvider: function(url) { |
---|
97 | for (var i = 0; i < providers.length; i++) { |
---|
98 | if (providers[i].matches(url)) { |
---|
99 | return providers[i]; |
---|
100 | } |
---|
101 | } |
---|
102 | return null; |
---|
103 | } |
---|
104 | }; |
---|
105 | |
---|
106 | var internalMethods = { |
---|
107 | _doRequest: function(container,url,embedProvider) { |
---|
108 | $.ajax($.extend({ |
---|
109 | url: internalMethods._getRequestUrl(embedProvider,url), |
---|
110 | type: 'get', |
---|
111 | dataType: 'json', |
---|
112 | cache: false, |
---|
113 | timeout: 10000, |
---|
114 | success: function (data) { |
---|
115 | var data = $.extend({ |
---|
116 | type: null, |
---|
117 | version: null, |
---|
118 | title: null, |
---|
119 | author_name: null, |
---|
120 | author_url: null, |
---|
121 | provider_name: null, |
---|
122 | provider_url: null, |
---|
123 | cache_age: null, |
---|
124 | thumbnail_url: null, |
---|
125 | thumbnail_width: null, |
---|
126 | thumbnail_height: null, |
---|
127 | url: null, |
---|
128 | width: null, |
---|
129 | height: null, |
---|
130 | html: null, |
---|
131 | code: null |
---|
132 | }, data); |
---|
133 | switch (data.type) { |
---|
134 | case 'photo': |
---|
135 | data.code = internalMethods._getPhotoCode(data); |
---|
136 | break; |
---|
137 | case 'video': |
---|
138 | data.code = internalMethods._getVideoCode(data); |
---|
139 | break; |
---|
140 | case 'rich': |
---|
141 | data.code = internalMethods._getRichCode(data); |
---|
142 | break; |
---|
143 | case 'link': |
---|
144 | data.code = internalMethods._getLinkCode(data); |
---|
145 | break; |
---|
146 | case 'error': |
---|
147 | settings.onError.call(null,data.error_code,data.error_message); |
---|
148 | break; |
---|
149 | } |
---|
150 | settings.beforeEmbed.call(container,data); |
---|
151 | settings.onEmbed.call(container,data); |
---|
152 | settings.afterEmbed.call(container,data); |
---|
153 | }, |
---|
154 | error: settings.onError |
---|
155 | },settings.ajaxOptions || {})); |
---|
156 | }, |
---|
157 | |
---|
158 | _insertCode: function(container,method,data) { |
---|
159 | if (data.type == null && data.code == null) { |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | switch (method) { |
---|
164 | case 'auto': |
---|
165 | if (container.attr('href') == null) { |
---|
166 | internalMethods._insertCode(container,'append',data); |
---|
167 | } |
---|
168 | else { |
---|
169 | internalMethods._insertCode(container,'replace',data); |
---|
170 | }; |
---|
171 | break; |
---|
172 | case 'replace': |
---|
173 | container.replaceWith(data.code); |
---|
174 | break; |
---|
175 | case 'append': |
---|
176 | container.html(data.code); |
---|
177 | break; |
---|
178 | } |
---|
179 | }, |
---|
180 | |
---|
181 | _getPhotoCode: function(data) { |
---|
182 | var href = null; |
---|
183 | var title = null; |
---|
184 | var alt = new Array(); |
---|
185 | |
---|
186 | href = data.thumbnail_url ? data.thumbnail_url : data.url; |
---|
187 | title = data.title ? data.title : ''; |
---|
188 | |
---|
189 | if (data.provider_name) alt.push(data.provider_name); |
---|
190 | if (data.author_name) alt.push(data.author_name); |
---|
191 | if (data.title) alt.push(data.title); |
---|
192 | |
---|
193 | return $('<div>').append($('<a>').attr({ |
---|
194 | 'href': data.url, |
---|
195 | 'title': title |
---|
196 | }).append($('<img>').attr({ |
---|
197 | 'src': href, |
---|
198 | 'title': title, |
---|
199 | 'alt': alt.join(' - ') |
---|
200 | }))).html(); |
---|
201 | }, |
---|
202 | |
---|
203 | _getVideoCode: function(data) { |
---|
204 | return internalMethods._getValidHXHTMLCode(data.html,data); |
---|
205 | }, |
---|
206 | |
---|
207 | _getRichCode: function(data) { |
---|
208 | return internalMethods._getValidHXHTMLCode(data.html,data); |
---|
209 | }, |
---|
210 | |
---|
211 | _getLinkCode: function(data) { |
---|
212 | var title = null; |
---|
213 | var alt = new Array(); |
---|
214 | |
---|
215 | title = data.title ? data.title : ''; |
---|
216 | |
---|
217 | if (data.provider_name) alt.push(data.provider_name); |
---|
218 | if (data.author_name) alt.push(data.author_name); |
---|
219 | if (data.title) alt.push(data.title); |
---|
220 | |
---|
221 | return $('<div>').append($('<a>').attr({ |
---|
222 | 'href': data.url, |
---|
223 | 'title': title |
---|
224 | }).append(title)).html(); |
---|
225 | }, |
---|
226 | |
---|
227 | _getValidHXHTMLCode: function(html,data) { |
---|
228 | var xhtml = ''; |
---|
229 | var alt = new Array(); |
---|
230 | if (data.provider_name) alt.push(data.provider_name); |
---|
231 | if (data.author_name) alt.push(data.author_name); |
---|
232 | if (data.title) alt.push(data.title); |
---|
233 | |
---|
234 | $(html).each(function() { |
---|
235 | if (this.tagName == 'IFRAME') { |
---|
236 | var attr = { |
---|
237 | 'src': 'data', |
---|
238 | 'width': 'width', |
---|
239 | 'height': 'height' |
---|
240 | }; |
---|
241 | var attributes = this.attributes; |
---|
242 | object = $('<object>'); |
---|
243 | object.attr('type','text/html'); |
---|
244 | for (i in attributes) { |
---|
245 | if (attr.hasOwnProperty(attributes[i].name)) { |
---|
246 | object.attr(attr[attributes[i].name],attributes[i].value); |
---|
247 | } |
---|
248 | } |
---|
249 | xhtml += $('<div>').append(object.html(alt.join(' - '))).html(); |
---|
250 | } |
---|
251 | else if (this.tagName == 'OBJECT') { |
---|
252 | if ($(this).find('embed').size() > 0) { |
---|
253 | var embed = $(this).find('embed').get(0); |
---|
254 | if ($.inArray('src',embed.attributes)) { |
---|
255 | $(this).attr('data',embed.attributes.src.nodeValue); |
---|
256 | } |
---|
257 | if ($.inArray('type',embed.attributes)) { |
---|
258 | $(this).attr('type',embed.attributes.type.nodeValue); |
---|
259 | } |
---|
260 | $(this).find('embed').remove(); |
---|
261 | } |
---|
262 | xhtml += $('<div>').append($(this).html(alt.join(' - '))).html(); |
---|
263 | } else { |
---|
264 | xhtml += internalMethods._getValidHXHTMLCode($('<div>').append($(html).find('iframe,object')).html(),data); |
---|
265 | } |
---|
266 | }); |
---|
267 | |
---|
268 | return xhtml; |
---|
269 | }, |
---|
270 | |
---|
271 | _init: function() { |
---|
272 | var provider; |
---|
273 | var i; |
---|
274 | |
---|
275 | // If there are allowed providers, jQuery oembed can not be greedy |
---|
276 | if (!internalMethods._isNullOrEmpty(settings.allowedProviders)) { |
---|
277 | settings.greedy = false; |
---|
278 | } |
---|
279 | |
---|
280 | // If there are allowed providers, jQuery oembed can not be greedy |
---|
281 | // Disabled also providers |
---|
282 | if (!internalMethods._isNullOrEmpty(settings.disallowedProviders)) { |
---|
283 | for (i = 0; i < providers.length; i++) { |
---|
284 | if ($.inArray(providers[i].name,settings.disallowedProviders)) { |
---|
285 | provider.enable = false; |
---|
286 | } |
---|
287 | } |
---|
288 | settings.greedy = false; |
---|
289 | } |
---|
290 | |
---|
291 | if (!internalMethods._isNullOrEmpty(settings.customProviders)) { |
---|
292 | $.each(settings.customProviders, function(n,customProvider) { |
---|
293 | if (customProvider instanceof publicMethods.OEmbedProvider) { |
---|
294 | providers.push(provider); |
---|
295 | } else { |
---|
296 | provider = new publicMethods.OEmbedProvider(); |
---|
297 | if (provider.fromJSON(customProvider)) { |
---|
298 | providers.push(provider); |
---|
299 | } |
---|
300 | } |
---|
301 | }); |
---|
302 | } |
---|
303 | |
---|
304 | // If in greedy mode, we add the default provider |
---|
305 | defaultProvider = internalMethods._getDefaultOEmbedProvider(settings.defaultOEmbedProvider); |
---|
306 | if (settings.greedy) { |
---|
307 | providers.push(defaultProvider); |
---|
308 | } |
---|
309 | |
---|
310 | // If any provider has no apiendpoint, we use the default provider endpoint |
---|
311 | for (i = 0; i < providers.length; i++) { |
---|
312 | if (providers[i].enable && providers[i].apiendpoint == null) { |
---|
313 | providers[i].apiendpoint = defaultProvider.apiendpoint; |
---|
314 | } |
---|
315 | } |
---|
316 | }, |
---|
317 | |
---|
318 | _getDefaultOEmbedProvider: function(defaultOEmbedProvider) { |
---|
319 | var url = 'http://oohembed.com/oohembed/'; |
---|
320 | if (defaultOEmbedProvider == 'embed.ly') { |
---|
321 | url = 'http://api.embed.ly/v1/api/oembed?'; |
---|
322 | } |
---|
323 | return new publicMethods.OEmbedProvider(defaultOEmbedProvider,null,null,url,'callback'); |
---|
324 | }, |
---|
325 | |
---|
326 | _getRequestUrl: function(provider,externalUrl) { |
---|
327 | var url = provider.apiendpoint, qs = "", callbackparameter = provider.callbackparameter || 'callback', i; |
---|
328 | |
---|
329 | if (url.indexOf('?') <= 0) { |
---|
330 | url = url + '?'; |
---|
331 | } else { |
---|
332 | url = url + '&'; |
---|
333 | } |
---|
334 | |
---|
335 | if (provider.maxWidth != null && provider.params['maxwidth'] == null) { |
---|
336 | provider.params['maxwidth'] = provider.maxWidth; |
---|
337 | } |
---|
338 | |
---|
339 | if (provider.maxHeight != null && provider.params['maxheight'] == null) { |
---|
340 | provider.params['maxheight'] = provider.maxHeight; |
---|
341 | } |
---|
342 | |
---|
343 | for (i in provider.params) { |
---|
344 | // We don't want them to jack everything up by changing the callback parameter |
---|
345 | if (i == provider.callbackparameter) { |
---|
346 | continue; |
---|
347 | } |
---|
348 | |
---|
349 | // allows the options to be set to null, don't send null values to the server as parameters |
---|
350 | if (provider.params[i] != null) { |
---|
351 | qs += '&' + escape(i) + '=' + provider.params[i]; |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | url += 'format=json&url=' + escape(externalUrl) + |
---|
356 | qs + |
---|
357 | '&' + callbackparameter + '=?'; |
---|
358 | |
---|
359 | return url; |
---|
360 | }, |
---|
361 | |
---|
362 | _getNormalizedParams: function(params) { |
---|
363 | if (params == null) { |
---|
364 | return null; |
---|
365 | } |
---|
366 | |
---|
367 | var key; |
---|
368 | var normalizedParams = {}; |
---|
369 | |
---|
370 | for (key in params) { |
---|
371 | if (key != null) { |
---|
372 | normalizedParams[key.toLowerCase()] = params[key]; |
---|
373 | } |
---|
374 | } |
---|
375 | return normalizedParams; |
---|
376 | }, |
---|
377 | |
---|
378 | _isNullOrEmpty: function(object) { |
---|
379 | if (typeof object == 'undefined') { |
---|
380 | return true; |
---|
381 | } else if (object == null) { |
---|
382 | return true; |
---|
383 | } else if ($.isArray(object) && object.length == 0) { |
---|
384 | return true; |
---|
385 | } else { |
---|
386 | return false; |
---|
387 | } |
---|
388 | } |
---|
389 | }; |
---|
390 | |
---|
391 | var providers = [ |
---|
392 | new publicMethods.OEmbedProvider('youtube', 'video', ['youtube\\.com/watch.+v=[\\w-]+&?', 'youtu\\.be/.*']), // 'http://www.youtube.com/oembed' (no jsonp) |
---|
393 | new publicMethods.OEmbedProvider('flickr', 'photo', ['flickr\\.com/photos/[-.\\w@]+/\\d+/?'], 'http://flickr.com/services/oembed', 'jsoncallback'), |
---|
394 | new publicMethods.OEmbedProvider('viddler', 'video', ['viddler\.com']), // 'http://lab.viddler.com/services/oembed/' (no jsonp) |
---|
395 | new publicMethods.OEmbedProvider('blip', 'video', ['blip\\.tv/.+'], 'http://blip.tv/oembed/'), |
---|
396 | new publicMethods.OEmbedProvider('hulu', 'video', ['hulu\\.com/watch/.*'], 'http://www.hulu.com/api/oembed.json'), |
---|
397 | new publicMethods.OEmbedProvider('vimeo', 'video', ['http:\/\/www\.vimeo\.com\/groups\/.*\/videos\/.*', 'http:\/\/www\.vimeo\.com\/.*', 'http:\/\/vimeo\.com\/groups\/.*\/videos\/.*', 'http:\/\/vimeo\.com\/.*'], 'http://vimeo.com/api/oembed.json'), |
---|
398 | new publicMethods.OEmbedProvider('dailymotion', 'video', ['dailymotion\\.com/.+']), // 'http://www.dailymotion.com/api/oembed/' (callback parameter does not return jsonp) |
---|
399 | new publicMethods.OEmbedProvider('scribd', 'rich', ['scribd\\.com/.+']), // ', 'http://www.scribd.com/services/oembed'' (no jsonp) |
---|
400 | new publicMethods.OEmbedProvider('slideshare', 'rich', ['slideshare\.net'], 'http://www.slideshare.net/api/oembed/1'), |
---|
401 | new publicMethods.OEmbedProvider('photobucket', 'photo', ['photobucket\\.com/(albums|groups)/.*'], 'http://photobucket.com/oembed/') |
---|
402 | ]; |
---|
403 | |
---|
404 | $.fn.oembed = function(method) { |
---|
405 | if (publicMethods[method]) { |
---|
406 | return publicMethods[method].apply(this,Array.prototype.slice.call(arguments,1)); |
---|
407 | } else if (typeof method === 'string' || !method) { |
---|
408 | return publicMethods.init.apply(this,arguments); |
---|
409 | } else { |
---|
410 | $.error('Method ' + method + ' does not exist on jQuery.oembed'); |
---|
411 | } |
---|
412 | }; |
---|
413 | })(jQuery) |
---|