-
Notifications
You must be signed in to change notification settings - Fork 3
/
jquery.oembed.js
351 lines (302 loc) · 14.3 KB
/
jquery.oembed.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*!
* jquery oembed plugin
*
* Copyright (c) 2009 Richard Chamorro
* Licensed under the MIT license
*
* Author: Richard Chamorro
*/
(function ($) {
$.fn.oembed = function (url, options, embedAction) {
settings = $.extend(true, $.fn.oembed.defaults, options);
initializeProviders();
return this.each(function () {
var container = $(this),
resourceURL = (url != null) ? url : container.attr("href"),
provider;
if (embedAction) {
settings.onEmbed = embedAction;
} else {
settings.onEmbed = function (oembedData) {
$.fn.oembed.insertCode(this, settings.embedMethod, oembedData);
};
}
if (resourceURL != null) {
provider = $.fn.oembed.getOEmbedProvider(resourceURL);
if (provider != null) {
provider.params = getNormalizedParams(settings[provider.name]) || {};
provider.maxWidth = settings.maxWidth;
provider.maxHeight = settings.maxHeight;
embedCode(container, resourceURL, provider);
} else {
settings.onProviderNotFound.call(container, resourceURL);
}
}
return container;
});
};
var settings, activeProviders = [];
// Plugin defaults
$.fn.oembed.defaults = {
maxWidth: null,
maxHeight: null,
embedMethod: "replace", // "auto", "append", "fill"
defaultOEmbedProvider: "oohembed", // "oohembed", "embed.ly", "none"
allowedProviders: null,
disallowedProviders: null,
customProviders: null, // [ new $.fn.oembed.OEmbedProvider("customprovider", null, ["customprovider\\.com/watch.+v=[\\w-]+&?"]) ]
defaultProvider: null,
greedy: true,
onProviderNotFound: function () { },
beforeEmbed: function () { },
afterEmbed: function () { },
onEmbed: function () { },
onError: function() {},
ajaxOptions: {}
};
/* Private functions */
function getRequestUrl(provider, externalUrl) {
var url = provider.apiendpoint, qs = "", callbackparameter = provider.callbackparameter || "callback", i;
if (url.indexOf("?") <= 0)
url = url + "?";
else
url = url + "&";
if (provider.maxWidth != null && provider.params["maxwidth"] == null)
provider.params["maxwidth"] = provider.maxWidth;
if (provider.maxHeight != null && provider.params["maxheight"] == null)
provider.params["maxheight"] = provider.maxHeight;
for (i in provider.params) {
// We don't want them to jack everything up by changing the callback parameter
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];
}
url += "format=json&url=" + escape(externalUrl) +
qs +
"&" + callbackparameter + "=?";
return url;
};
function embedCode(container, externalUrl, embedProvider) {
var requestUrl = getRequestUrl(embedProvider, externalUrl),
ajaxopts = $.extend({
url: requestUrl,
type: 'get',
dataType: 'jsonp json',
// error: jsonp request doesnt' support error handling
success: function (data) {
console.log("got data");
console.log(data);
var oembedData = $.extend({}, data);
switch (oembedData.type) {
case "photo":
oembedData.code = $.fn.oembed.getPhotoCode(externalUrl, oembedData);
break;
case "video":
oembedData.code = $.fn.oembed.getVideoCode(externalUrl, oembedData);
break;
case "rich":
oembedData.code = $.fn.oembed.getRichCode(externalUrl, oembedData);
break;
default:
oembedData.code = $.fn.oembed.getGenericCode(externalUrl, oembedData);
break;
}
settings.beforeEmbed.call(container, oembedData);
settings.onEmbed.call(container, oembedData);
settings.afterEmbed.call(container, oembedData);
},
error: function() { console.log("error!"); console.log(arguments); } //settings.onError.call(container, externalUrl, embedProvider)
}, settings.ajaxOptions || { } );
$.ajax( ajaxopts );
};
function initializeProviders() {
activeProviders = [];
var defaultProvider, restrictedProviders = [], i, provider;
if (!isNullOrEmpty(settings.allowedProviders)) {
for (i = 0; i < $.fn.oembed.providers.length; i++) {
if ($.inArray($.fn.oembed.providers[i].name, settings.allowedProviders) >= 0)
activeProviders.push($.fn.oembed.providers[i]);
}
// If there are allowed providers, jquery-oembed cannot be greedy
settings.greedy = false;
} else {
activeProviders = $.fn.oembed.providers;
}
if (!isNullOrEmpty(settings.disallowedProviders)) {
for (i = 0; i < activeProviders.length; i++) {
if ($.inArray(activeProviders[i].name, settings.disallowedProviders) < 0)
restrictedProviders.push(activeProviders[i]);
}
activeProviders = restrictedProviders;
// If there are allowed providers, jquery-oembed cannot be greedy
settings.greedy = false;
}
if (!isNullOrEmpty(settings.customProviders)) {
$.each(settings.customProviders, function (n, customProvider) {
if (customProvider instanceof $.fn.oembed.OEmbedProvider) {
activeProviders.push(provider);
} else {
provider = new $.fn.oembed.OEmbedProvider();
if (provider.fromJSON(customProvider))
activeProviders.push(provider);
}
});
}
// If in greedy mode, we add the default provider
defaultProvider = getDefaultOEmbedProvider(settings.defaultOEmbedProvider);
if (settings.greedy == true) {
activeProviders.push(defaultProvider);
}
// If any provider has no apiendpoint, we use the default provider endpoint
for (i = 0; i < activeProviders.length; i++) {
if (activeProviders[i].apiendpoint == null)
activeProviders[i].apiendpoint = defaultProvider.apiendpoint;
}
}
function getDefaultOEmbedProvider(defaultOEmbedProvider) {
var url = "http://oohembed.com/oohembed/";
if (defaultOEmbedProvider == "embed.ly")
url = "http://api.embed.ly/v1/api/oembed?";
return new $.fn.oembed.OEmbedProvider(defaultOEmbedProvider, null, null, url, "callback");
}
function getNormalizedParams(params) {
if (params == null)
return null;
var key, normalizedParams = {};
for (key in params) {
if (key != null)
normalizedParams[key.toLowerCase()] = params[key];
}
return normalizedParams;
}
function isNullOrEmpty(object) {
if (typeof object == "undefined")
return true;
if (object == null)
return true;
if ($.isArray(object) && object.length == 0)
return true;
return false;
}
/* Public functions */
$.fn.oembed.insertCode = function (container, embedMethod, oembedData) {
if (oembedData == null)
return;
switch (embedMethod) {
case "auto":
if (container.attr("href") != null) {
$.fn.oembed.insertCode(container, "append", oembedData);
}
else {
$.fn.oembed.insertCode(container, "replace", oembedData);
};
break;
case "replace":
container.replaceWith(oembedData.code);
break;
case "fill":
container.html(oembedData.code);
break;
case "append":
var oembedContainer = container.next();
if (oembedContainer == null || !oembedContainer.hasClass("oembed-container")) {
oembedContainer = container
.after('<div class="oembed-container"></div>')
.next(".oembed-container");
if (oembedData != null && oembedData.provider_name != null)
oembedContainer.toggleClass("oembed-container-" + oembedData.provider_name);
}
oembedContainer.html(oembedData.code);
break;
}
};
$.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 = '<div><a href="' + url + '" target=\'_blank\'><img src="' + oembedData.url + '" alt="' + alt + '"/></a></div>';
if (oembedData.html)
code += "<div>" + oembedData.html + "</div>";
return code;
};
$.fn.oembed.getVideoCode = function (url, oembedData) {
var code = oembedData.html;
return code;
};
$.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 = '<a href="' + url + '">' + title + '</a>';
if (oembedData.html)
code += "<div>" + oembedData.html + "</div>";
return code;
};
$.fn.oembed.isProviderAvailable = function (url) {
var provider = getOEmbedProvider(url);
return (provider != null);
};
$.fn.oembed.getOEmbedProvider = function (url) {
for (var i = 0; i < activeProviders.length; i++) {
if (activeProviders[i].matches(url))
return activeProviders[i];
}
return null;
};
$.fn.oembed.OEmbedProvider = function (name, type, urlschemesarray, apiendpoint, callbackparameter) {
this.name = name;
this.type = type; // "photo", "video", "link", "rich", null
this.urlschemes = getUrlSchemes(urlschemesarray);
this.apiendpoint = apiendpoint;
this.callbackparameter = callbackparameter;
this.maxWidth = 500;
this.maxHeight = 400;
var i, property, regExp;
this.matches = function (externalUrl) {
for (i = 0; i < this.urlschemes.length; i++) {
regExp = new RegExp(this.urlschemes[i], "i");
if (externalUrl.match(regExp) != null)
return true;
}
return false;
};
this.fromJSON = function (json) {
for (property in json) {
if (property != "urlschemes")
this[property] = json[property];
else
this[property] = getUrlSchemes(json[property]);
}
return true;
};
function getUrlSchemes(urls) {
if (isNullOrEmpty(urls))
return ["."];
if ($.isArray(urls))
return urls;
return urls.split(";");
}
};
/* Native & common providers */
$.fn.oembed.providers = [
new $.fn.oembed.OEmbedProvider("youtube", "video", ["youtube\\.com/watch.+v=[\\w-]+&?"]), // "http://www.youtube.com/oembed" (no jsonp)
new $.fn.oembed.OEmbedProvider("flickr", "photo", ["flickr\\.com/photos/[-.\\w@]+/\\d+/?"], "http://flickr.com/services/oembed", "jsoncallback"),
new $.fn.oembed.OEmbedProvider("viddler", "video", ["viddler\.com"]), // "http://lab.viddler.com/services/oembed/" (no jsonp)
new $.fn.oembed.OEmbedProvider("blip", "video", ["blip\\.tv/.+"], "http://blip.tv/oembed/"),
new $.fn.oembed.OEmbedProvider("hulu", "video", ["hulu\\.com/watch/.*"], "http://www.hulu.com/api/oembed.json"),
new $.fn.oembed.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"),
new $.fn.oembed.OEmbedProvider("dailymotion", "video", ["dailymotion\\.com/.+"]), // "http://www.dailymotion.com/api/oembed/" (callback parameter does not return jsonp)
new $.fn.oembed.OEmbedProvider("scribd", "rich", ["scribd\\.com/.+"]), // ", "http://www.scribd.com/services/oembed"" (no jsonp)
new $.fn.oembed.OEmbedProvider("slideshare", "rich", ["slideshare\.net"], "http://www.slideshare.net/api/oembed/1"),
new $.fn.oembed.OEmbedProvider("photobucket", "photo", ["photobucket\\.com/(albums|groups)/.*"], "http://photobucket.com/oembed/"),
new $.fn.oembed.OEmbedProvider("mlg-tv", "video", ["tv.majorleaguegaming.com/video/.*"], "http://tv.majorleaguegaming.com/oembed.json")
// new $.fn.oembed.OEmbedProvider("vids.myspace.com", "video", ["vids\.myspace\.com"]), // "http://vids.myspace.com/index.cfm?fuseaction=oembed" (not working)
// new $.fn.oembed.OEmbedProvider("screenr", "rich", ["screenr\.com"], "http://screenr.com/api/oembed.json") (error)
// new $.fn.oembed.OEmbedProvider("qik", "video", ["qik\\.com/\\w+"], "http://qik.com/api/oembed.json"),
// new $.fn.oembed.OEmbedProvider("revision3", "video", ["revision3\.com"], "http://revision3.com/api/oembed/")
];
})(jQuery);