]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/standalone/videos/test-embed.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / test-embed.ts
index a4b54782c7e8927ab078c4750ba0bf3f1eb742ad..18c338a2da583d303c259fe2cb49a8b89e2a3d6f 100644 (file)
@@ -1,26 +1,33 @@
 import './test-embed.scss'
-import { PeerTubePlayer } from '../player/player'
 import { PeerTubeResolution, PlayerEventType } from '../player/definitions'
+import { PeerTubePlayer } from '../player/player'
 
 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]
+
+  const isPlaylist = window.location.pathname.startsWith('/video-playlists/')
+
+  const elementId = !lastPart.includes('?') ? lastPart : lastPart.split('?')[0]
 
   const iframe = document.createElement('iframe')
-  iframe.src = `/videos/embed/${videoId}?api=1`
+  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.`)
+  console.log('Document finished loading.')
   const player = new PeerTubePlayer(document.querySelector('iframe'))
 
-  window[ 'player' ] = player
+  window['player'] = player
 
-  console.log(`Awaiting player ready...`)
+  console.log('Awaiting player ready...')
   await player.ready
-  console.log(`Player is ready.`)
+  console.log('Player is ready.')
 
   const monitoredEvents = [
     'pause',
@@ -32,12 +39,17 @@ window.addEventListener('load', async () => {
   monitoredEvents.forEach(e => {
     player.addEventListener(e as PlayerEventType, (param) => console.log(`PLAYER: event '${e}' received`, param))
     console.log(`PLAYER: now listening for event '${e}'`)
+
+    player.getCurrentPosition()
+      .then(position => {
+        document.getElementById('playlist-position').innerHTML = position + ''
+      })
   })
 
   let playbackRates: number[] = []
   let currentRate = await player.getPlaybackRate()
 
-  const updateRates = async () => {
+  const updateRates = () => {
     const rateListEl = document.querySelector('#rate-list')
     rateListEl.innerHTML = ''
 
@@ -67,7 +79,35 @@ window.addEventListener('load', async () => {
     updateRates()
   })
 
-  const updateResolutions = ((resolutions: PeerTubeResolution[]) => {
+  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 = ''
 
@@ -88,7 +128,7 @@ window.addEventListener('load', async () => {
         resolutionListEl.appendChild(itemEl)
       }
     })
-  })
+  }
 
   player.getResolutions().then(
     resolutions => updateResolutions(resolutions))