-
Notifications
You must be signed in to change notification settings - Fork 4
/
embed.js
76 lines (63 loc) · 1.73 KB
/
embed.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
class Embed {
constructor (data = { height: 315, width: 500 }) {
this.id = data.id
this.target = data.target
this.source = data.source
this.width = data.width
this.height = data.height
}
getEmbed() {
switch(this.source) {
case 'youtube':
return this.createEmbed({
src: `https://www.youtube.com/embed/${this.id}?ecver=1`
})
case 'dailymotion':
return this.createEmbed({
src: `https://www.dailymotion.com/embed/video/${this.id}`
})
case 'facebook':
return this.createEmbed({
src: `http://www.facebook.com/video/embed?video_id=${this.id}`
})
case 'instagram':
return this.createEmbed({
src: `http://instagram.com/p/${this.id}/embed`
})
case 'soundcloud':
return this.createEmbed({
src: `https://w.soundcloud.com/player/?url=https://api.soundcloud.com/tracks/${this.id}&color=0066c`
})
case 'vimeo':
return this.createEmbed({
src: `https://player.vimeo.com/video/${this.id}?title=0&byline=0&portrait=0`
})
case 'metacafe':
return this.createEmbed({
src: `http://www.metacafe.com/embed/${this.id}/`
})
default:
throw 'There is no such source'
}
}
createEmbed({src, width = this.width, height = this.height}) {
let iframe = document.createElement('iframe')
iframe.src = src
iframe.width = width
iframe.height = height
iframe.frameBorder = 0
iframe.scrolling = 'no'
iframe.setAttribute('webkitallowfullscreen', '')
iframe.setAttribute('mozallowfullscreen', '')
iframe.setAttribute('allowfullscreen', '')
return iframe
}
run() {
let videoArea = document.querySelector(this.target)
if(videoArea) {
videoArea.appendChild(this.getEmbed())
} else {
throw 'Target is not found'
}
}
}