1 import './test-embed.scss'
2 import { PeerTubeResolution, PlayerEventType } from '../player/definitions'
3 import { PeerTubePlayer } from '../player/player'
5 window.addEventListener('load', async () => {
6 const urlParts = window.location.href.split('/')
7 const lastPart = urlParts[ urlParts.length - 1 ]
9 const isPlaylist = window.location.pathname.startsWith('/video-playlists/')
11 const elementId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ]
13 const iframe = document.createElement('iframe')
14 iframe.src = isPlaylist
15 ? `/videos/embed/${elementId}?api=1`
16 : `/video-playlists/embed/${elementId}?api=1`
18 const mainElement = document.querySelector('#host')
19 mainElement.appendChild(iframe)
21 console.log(`Document finished loading.`)
22 const player = new PeerTubePlayer(document.querySelector('iframe'))
24 window[ 'player' ] = player
26 console.log(`Awaiting player ready...`)
28 console.log(`Player is ready.`)
30 const monitoredEvents = [
33 'playbackStatusUpdate',
34 'playbackStatusChange'
37 monitoredEvents.forEach(e => {
38 player.addEventListener(e as PlayerEventType, (param) => console.log(`PLAYER: event '${e}' received`, param))
39 console.log(`PLAYER: now listening for event '${e}'`)
41 player.getCurrentPosition()
42 .then(position => document.getElementById('playlist-position').innerHTML = position + '')
45 let playbackRates: number[] = []
46 let currentRate = await player.getPlaybackRate()
48 const updateRates = async () => {
49 const rateListEl = document.querySelector('#rate-list')
50 rateListEl.innerHTML = ''
52 playbackRates.forEach(rate => {
53 if (currentRate === rate) {
54 const itemEl = document.createElement('strong')
55 itemEl.innerText = `${rate} (active)`
56 itemEl.style.display = 'block'
57 rateListEl.appendChild(itemEl)
59 const itemEl = document.createElement('a')
60 itemEl.href = 'javascript:;'
61 itemEl.innerText = rate.toString()
62 itemEl.addEventListener('click', () => {
63 player.setPlaybackRate(rate)
67 itemEl.style.display = 'block'
68 rateListEl.appendChild(itemEl)
73 player.getPlaybackRates().then(rates => {
78 const updateCaptions = async () => {
79 const captions = await player.getCaptions()
81 const captionEl = document.querySelector('#caption-list')
82 captionEl.innerHTML = ''
84 captions.forEach(c => {
87 if (c.mode === 'showing') {
88 const itemEl = document.createElement('strong')
89 itemEl.innerText = `${c.label} (active)`
90 itemEl.style.display = 'block'
91 captionEl.appendChild(itemEl)
93 const itemEl = document.createElement('a')
94 itemEl.href = 'javascript:;'
95 itemEl.innerText = c.label
96 itemEl.addEventListener('click', () => {
97 player.setCaption(c.id)
100 itemEl.style.display = 'block'
101 captionEl.appendChild(itemEl)
108 const updateResolutions = ((resolutions: PeerTubeResolution[]) => {
109 const resolutionListEl = document.querySelector('#resolution-list')
110 resolutionListEl.innerHTML = ''
112 resolutions.forEach(resolution => {
113 if (resolution.active) {
114 const itemEl = document.createElement('strong')
115 itemEl.innerText = `${resolution.label} (active)`
116 itemEl.style.display = 'block'
117 resolutionListEl.appendChild(itemEl)
119 const itemEl = document.createElement('a')
120 itemEl.href = 'javascript:;'
121 itemEl.innerText = resolution.label
122 itemEl.addEventListener('click', () => {
123 player.setResolution(resolution.id)
125 itemEl.style.display = 'block'
126 resolutionListEl.appendChild(itemEl)
131 player.getResolutions().then(
132 resolutions => updateResolutions(resolutions))
133 player.addEventListener('resolutionUpdate',
134 resolutions => updateResolutions(resolutions))
136 const updateVolume = (volume: number) => {
137 const volumeEl = document.getElementById('volume')
138 volumeEl.innerText = (volume * 100) + '%'
141 player.getVolume().then(volume => updateVolume(volume))
142 player.addEventListener('volumeChange', volume => updateVolume(volume))