forked from alanshaw/embed-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
321 lines (260 loc) · 7.34 KB
/
index.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
const URL = require('url')
const promise = require('promise-polyfill')
const {fetch} = require('fetch-ponyfill')({ Promise: promise })
const escape = require('lodash.escape')
const YOUTUBE = 'youtube'
const VIMEO = 'vimeo'
const DAILYMOTION = 'dailymotion'
const LOOM = 'loom'
const PANOPTO = 'panopto'
const validVimeoOpts = [
'thumbnail_small',
'thumbnail_medium',
'thumbnail_large'
]
const validYouTubeOpts = [
'default',
'mqdefault',
'hqdefault',
'sddefault',
'maxresdefault'
]
const validDailyMotionOpts = [
'thumbnail_60_url',
'thumbnail_120_url',
'thumbnail_180_url',
'thumbnail_240_url',
'thumbnail_360_url',
'thumbnail_480_url',
'thumbnail_720_url',
'thumbnail_1080_url'
]
const validLoomOpts = [
'hide_owner',
'hide_share',
'hide_title',
'hideEmbedTopBar'
]
const validPanoptoOpts = [
'autoplay',
'offerviewer',
'showtitle',
'showbrand',
'captions',
'interactivity'
];
const VIMEO_MATCH_RE = /^(?:\/video|\/channels\/[\w-]+|\/groups\/[\w-]+\/videos)?\/(\d+)/
const YOUTUBE_SHORTS_MATCH_RE = /^\/shorts\/([^/?]+)/
function embed (url, opts) {
var res = embed.info(url)
return res && embed[res.source] && embed[res.source](res.id, opts, url)
}
embed.info = function (url) {
url = URL.parse(url, true)
var id
id = detectYoutube(url)
if (id) {
return {
id: id,
source: YOUTUBE,
url: url.href
}
}
id = detectVimeo(url)
if (id) {
return {
id: id,
source: VIMEO,
url: url.href
}
}
id = detectDailymotion(url)
if (id) {
return {
id: id,
source: DAILYMOTION,
url: url.href
}
}
id = detectLoom(url)
if (id) {
return {
id: id,
source: LOOM,
url: url.href
}
}
id = detectPanopto(url)
if (id) {
return {
id: id,
source: PANOPTO,
url: url.href
}
}
}
// For compat with <=2.0.1
embed.videoSource = embed.info
embed.image = function (url, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = opts || {}
var res = embed.info(url)
if (!res && cb) return setTimeout(function () { cb() })
return res && embed[res.source].image(res.id, opts, cb)
}
function detectVimeo (url) {
var match
return (url.hostname === 'vimeo.com' && (match = VIMEO_MATCH_RE.exec(url.pathname))) ? match[1] : null
}
function detectYoutube (url) {
if (url.hostname.indexOf('youtube.com') > -1) {
const matchShorts = url.pathname.match(YOUTUBE_SHORTS_MATCH_RE);
if (matchShorts) {
return matchShorts[1];
}
return url.query.v
}
if (url.hostname === 'youtu.be') {
return url.pathname.split('/')[1]
}
return null
}
function detectDailymotion (url) {
if (url.hostname.indexOf('dailymotion.com') > -1) {
return url.pathname.split('/')[2].split('_')[0]
}
if (url.hostname === 'dai.ly') {
return url.pathname.split('/')[1]
}
return null
}
function detectLoom (url) {
if (url.hostname.indexOf('loom.com') > -1) {
return url.pathname.split('/')[2];
}
return null
}
function detectPanopto (url) {
if (url.hostname.endsWith('panopto.com')) {
return url.query && url.query.tid;
}
return null
}
embed.vimeo = function (id, opts) {
opts = parseOptions(opts)
return '<iframe title="Vimeo Media Player" src="//player.vimeo.com/video/' + id + opts.query + '"' + opts.attr + ' frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
}
embed.youtube = function (id, opts) {
opts = parseOptions(opts)
return '<iframe title="YouTube Media Player" src="//www.youtube-nocookie.com/embed/' + id + opts.query + '"' + opts.attr + ' frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
}
embed.dailymotion = function (id, opts) {
opts = parseOptions(opts)
return '<iframe title="Dailymotion Media Player" src="//www.dailymotion.com/embed/video/' + id + opts.query + '"' + opts.attr + ' frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
}
embed.loom = function (id, opts) {
opts = parseOptions(opts)
return '<iframe title="Loom Media Player" src="https://www.loom.com/embed/' + id + opts.query + '"' + opts.attr + ' frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
}
embed.panopto = function (id, opts, url) {
opts = { offerviewer: true, ...parseOptions(opts) }
const { hostname } = URL.parse(url, true);
return '<iframe src="https://' + hostname + '/Panopto/Pages/Embed.aspx?tid=' + id + opts.query + '"' + opts.attr + ' frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>'
}
embed.youtube.image = function (id, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
opts = opts || {}
opts.image = validYouTubeOpts.indexOf(opts.image) > 0 ? opts.image : 'default'
var src = '//img.youtube.com/vi/' + id + '/' + opts.image + '.jpg'
var result = {
src: src,
html: '<img src="' + src + '"/>'
}
if (!cb) return result.html
setTimeout(function () { cb(null, result) })
}
embed.vimeo.image = function (id, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!cb) throw new Error('must pass embed.vimeo.image a callback')
opts = opts || {}
opts.image = validVimeoOpts.indexOf(opts.image) >= 0 ? opts.image : 'thumbnail_large'
fetch('https://vimeo.com/api/v2/video/' + id + '.json')
.then(function (res) {
if (res.status !== 200) {
throw new Error('unexpected response from vimeo')
}
return res.json()
})
.then(function (body) {
if (!body || !body[0] || !body[0][opts.image]) {
throw new Error('no image found for vimeo.com/' + id)
}
var src = body[0][opts.image].split(':')[1]
var result = {
src: src,
html: '<img src="' + src + '"/>'
}
cb(null, result)
})
.catch(function (err) {
cb(err)
})
}
embed.dailymotion.image = function (id, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (!cb) throw new Error('must pass embed.dailymotion.image a callback')
opts = opts || {}
opts.image = validDailyMotionOpts.indexOf(opts.image) >= 0 ? opts.image : 'thumbnail_480_url'
fetch('https://api.dailymotion.com/video/' + id + '?fields=' + opts.image)
.then(function (res) {
if (res.status !== 200) {
throw new Error('unexpected response from dailymotion')
}
return res.json()
})
.then(function (body) {
if (!body || !body[opts.image]) {
throw new Error('no image found for dailymotion.com/' + id)
}
var src = body[opts.image]
var result = {
src: src,
html: '<img src="' + src + '"/>'
}
cb(null, result)
})
.catch(function (err) {
cb(err)
})
}
function serializeQuery (query) {
return Object.keys(query).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(query[key])
}).join('&')
}
function parseOptions (opts) {
var queryString = ''
var attributes = ''
if (opts && opts.hasOwnProperty('query')) {
queryString = '?' + serializeQuery(opts.query)
}
if (opts && opts.hasOwnProperty('attr')) {
attributes = ' ' + Object.keys(opts.attr).map(function (key) {
return key + '="' + escape(opts.attr[key]) + '"'
}).join(' ')
}
return { query: queryString, attr: attributes }
}
module.exports = embed