-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembeds.js
77 lines (68 loc) · 2.32 KB
/
embeds.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
/*global $:true*/
$.embedly.defaults.key = '1d5c48f7edc34c54bdae4c37b681ea2b';
var urls = [
'https://www.youtube.com/watch?v=rVlhMGQgDkY',
'https://vine.co/v/irMdgU253HU',
'https://streamable.com/25ws',
'https://www.instagram.com/p/BA4-invoXXH/',
'http://vimeo.com/18150336'
];
$.embedly.oembed(urls)
.then(function(results) {
// Reduce the response to only video, rich or photo types and remove
// link or error responses.
return results.reduce(function(array, oembed) {
var html;
if (oembed.type === 'rich' || oembed.type === 'video') {
// Use the amp-frame if it's a
// We need to pull the src out of the iframe.
var match = (/src=\"([^\"]+)\"/).exec(oembed.html);
if (match.length === 2) {
// Grab the src.
var src = 'https://'+match[1];
// We should always have a placeholder, otherwise we need to worry about the
// position of the iframe in relationship to the top of the page. If there
// is no thumbnail_url then we should use a default placeholder. Please
// add your own placeholder.
var placeholder = oembed.thumbnail_url;
if (!placeholder) {
placeholder = 'https://cdn.embed.ly/logos/embedly-powered-large-light.png';
}
// Build the amp-frame.
html = [
'<amp-iframe width=' + oembed.width,
'height=' + oembed.height,
'layout="responsive"',
'frameborder="0"',
'sandbox="allow-scripts allow-same-origin allow-popups"',
'src="' + src + '">',
'<amp-img layout="fill" src="' + placeholder + '" placeholder></amp-img>',
'</amp-iframe>'
].join(' ');
}
} else if (oembed.type === 'photo') {
// Use the amp-img here.
html = [
'<amp-img src="' + oembed.url + '"',
'width=' + oembed.width,
'height=' + oembed.height,
'layout="responsive" ></amp-img>'
].join(' ');
}
if (html) {
array.push(html);
}
return array;
}, []);
})
.then(function(results){
// add the emebds.
var $embeds = $('.embeds');
results.forEach(function(html){
$embeds.append([
'<div class="embed">',
html,
'</div>'
].join(''));
});
});