-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.js
52 lines (42 loc) · 1.22 KB
/
reader.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
import Vtt from './vtt.js'
const textWrapper = document.querySelector('#text')
const trackElement = document.querySelector('video > track')
const start = document.querySelector('button')
const getId = cue => `cue-${cue.startTime}-${cue.endTime}`
const text = []
const trackLoaded = (trackElement.track.cues && trackElement.track.cues.length) ? Promise.resolve(trackElement.track) : new Promise(resolve => {
trackElement.addEventListener('load', () => {
resolve(trackElement.track)
})
})
trackLoaded.then(track => {
start.removeAttribute('disabled')
for (let cue of track.cues) {
const p = document.createElement('p')
p.innerHTML = cue.text
p.id = getId(cue)
text.push(p)
textWrapper.appendChild(p)
}
const vtt = new Vtt(track.cues)
vtt.oncuechange = function (cue) {
if (cue) {
let actual = null
const currentId = getId(cue)
text.forEach(p => {
p.classList.remove('actual')
if (p.id === currentId) {
p.classList.add('actual')
actual = p
}
})
if (actual) {
actual.scrollIntoViewIfNeeded(true)
}
}
}
start.addEventListener('click', e => {
vtt.start()
document.body.classList.remove('start')
})
})