import './test-embed.scss'
+import { PeerTubeResolution, PlayerEventType } from '../player/definitions'
import { PeerTubePlayer } from '../player/player'
-import { PlayerEventType } from '../player/definitions'
+import { logger } from '../../root-helpers'
window.addEventListener('load', async () => {
const urlParts = window.location.href.split('/')
- const lastPart = urlParts[ urlParts.length - 1 ]
- const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[ 0 ]
+ const lastPart = urlParts[urlParts.length - 1]
- let iframe = document.createElement('iframe')
- iframe.src = `/videos/embed/${videoId}?autoplay=1&controls=0&api=1`
- let mainElement = document.querySelector('#host')
+ const isPlaylist = window.location.pathname.startsWith('/video-playlists/')
+
+ const elementId = !lastPart.includes('?') ? lastPart : lastPart.split('?')[0]
+
+ const iframe = document.createElement('iframe')
+ iframe.src = isPlaylist
+ ? `/video-playlists/embed/${elementId}?api=1`
+ : `/videos/embed/${elementId}?api=1`
+
+ iframe.sandbox.add('allow-same-origin', 'allow-scripts', 'allow-popups')
+
+ const mainElement = document.querySelector('#host')
mainElement.appendChild(iframe)
- console.log(`Document finished loading.`)
- let player = new PeerTubePlayer(document.querySelector('iframe'))
+ logger.info('Document finished loading.')
+ const player = new PeerTubePlayer(document.querySelector('iframe'))
- window[ 'player' ] = player
+ window['player'] = player
- console.log(`Awaiting player ready...`)
+ logger.info('Awaiting player ready...')
await player.ready
- console.log(`Player is ready.`)
+ logger.info('Player is ready.')
- let monitoredEvents = [
+ const monitoredEvents = [
'pause',
'play',
'playbackStatusUpdate',
]
monitoredEvents.forEach(e => {
- player.addEventListener(e as PlayerEventType, () => console.log(`PLAYER: event '${e}' received`))
- console.log(`PLAYER: now listening for event '${e}'`)
+ player.addEventListener(e as PlayerEventType, (param) => logger.info(`PLAYER: event '${e}' received`, { param }))
+ logger.info(`PLAYER: now listening for event '${e}'`)
+
+ player.getCurrentPosition()
+ .then(position => {
+ document.getElementById('playlist-position').innerHTML = position + ''
+ })
})
let playbackRates: number[] = []
let currentRate = await player.getPlaybackRate()
- let updateRates = async () => {
- let rateListEl = document.querySelector('#rate-list')
+ const updateRates = () => {
+ const rateListEl = document.querySelector('#rate-list')
rateListEl.innerHTML = ''
playbackRates.forEach(rate => {
if (currentRate === rate) {
- let itemEl = document.createElement('strong')
+ const itemEl = document.createElement('strong')
itemEl.innerText = `${rate} (active)`
itemEl.style.display = 'block'
rateListEl.appendChild(itemEl)
} else {
- let itemEl = document.createElement('a')
+ const itemEl = document.createElement('a')
itemEl.href = 'javascript:;'
itemEl.innerText = rate.toString()
itemEl.addEventListener('click', () => {
updateRates()
})
- let updateResolutions = ((resolutions: any) => {
- let resolutionListEl = document.querySelector('#resolution-list')
+ const updateCaptions = async () => {
+ const captions = await player.getCaptions()
+
+ const captionEl = document.querySelector('#caption-list')
+ captionEl.innerHTML = ''
+
+ captions.forEach(c => {
+ if (c.mode === 'showing') {
+ const itemEl = document.createElement('strong')
+ itemEl.innerText = `${c.label} (active)`
+ itemEl.style.display = 'block'
+ captionEl.appendChild(itemEl)
+ } else {
+ const itemEl = document.createElement('a')
+ itemEl.href = 'javascript:;'
+ itemEl.innerText = c.label
+ itemEl.addEventListener('click', () => {
+ player.setCaption(c.id)
+ updateCaptions()
+ })
+ itemEl.style.display = 'block'
+ captionEl.appendChild(itemEl)
+ }
+ })
+ }
+
+ updateCaptions()
+
+ const updateResolutions = (resolutions: PeerTubeResolution[]) => {
+ const resolutionListEl = document.querySelector('#resolution-list')
resolutionListEl.innerHTML = ''
- resolutions.forEach((resolution: any) => {
+ resolutions.forEach(resolution => {
if (resolution.active) {
- let itemEl = document.createElement('strong')
+ const itemEl = document.createElement('strong')
itemEl.innerText = `${resolution.label} (active)`
itemEl.style.display = 'block'
resolutionListEl.appendChild(itemEl)
} else {
- let itemEl = document.createElement('a')
+ const itemEl = document.createElement('a')
itemEl.href = 'javascript:;'
itemEl.innerText = resolution.label
itemEl.addEventListener('click', () => {
resolutionListEl.appendChild(itemEl)
}
})
- })
+ }
player.getResolutions().then(
resolutions => updateResolutions(resolutions))
player.addEventListener('resolutionUpdate',
resolutions => updateResolutions(resolutions))
+
+ const updateVolume = (volume: number) => {
+ const volumeEl = document.getElementById('volume')
+ volumeEl.innerText = (volume * 100) + '%'
+ }
+
+ player.getVolume().then(volume => updateVolume(volume))
+ player.addEventListener('volumeChange', volume => updateVolume(volume))
})