]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/embed.ts
Add ability to set a start time
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed.ts
CommitLineData
202e7223
C
1import './embed.scss'
2
63c4db6d 3import * as videojs from 'video.js'
c6352f2c 4
404b54e1 5import { VideoDetails } from '../../../../shared'
c6352f2c 6import { getVideojsOptions } from '../../assets/player/peertube-player'
202e7223 7
8cac1b64 8function getVideoUrl (id: string) {
7b0956ec 9 return window.location.origin + '/api/v1/videos/' + id
8cac1b64
C
10}
11
a16aee73 12async function loadVideoInfo (videoId: string): Promise<VideoDetails> {
8cac1b64 13 const response = await fetch(getVideoUrl(videoId))
a16aee73 14 return response.json()
202e7223
C
15}
16
202e7223
C
17const urlParts = window.location.href.split('/')
18const videoId = urlParts[urlParts.length - 1]
19
f2f1118f
F
20loadVideoInfo(videoId)
21 .then(videoInfo => {
c6352f2c
C
22 const videoContainerId = 'video-container'
23
24 const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
da99ccf2 25 let autoplay = false
f37bad63 26 let startTime = 0
da99ccf2
C
27
28 try {
29 let params = new URL(window.location.toString()).searchParams
30 autoplay = params.has('autoplay') && (params.get('autoplay') === '1' || params.get('autoplay') === 'true')
f37bad63
C
31
32 const startTimeParamString = params.get('start')
33 const startTimeParamNumber = parseInt(startTimeParamString, 10)
34 if (isNaN(startTimeParamNumber) === false) startTime = startTimeParamNumber
da99ccf2
C
35 } catch (err) {
36 console.error('Cannot get params from URL.', err)
37 }
38
c6352f2c 39 const videojsOptions = getVideojsOptions({
da99ccf2 40 autoplay,
c6352f2c
C
41 inactivityTimeout: 1500,
42 videoViewUrl: getVideoUrl(videoId) + '/views',
43 playerElement: videoElement,
44 videoFiles: videoInfo.files,
45 videoDuration: videoInfo.duration,
46 enableHotkeys: true,
33d78552 47 peertubeLink: true,
f37bad63
C
48 poster: window.location.origin + videoInfo.previewPath,
49 startTime
c6352f2c
C
50 })
51 videojs(videoContainerId, videojsOptions, function () {
f2f1118f 52 const player = this
202e7223 53
f2f1118f 54 player.dock({
22b59e80 55 title: videoInfo.name,
606ca5bc 56 description: 'Uses P2P, others may know you are watching this video.'
f2f1118f 57 })
202e7223 58 })
202e7223 59 })
a16aee73 60 .catch(err => console.error(err))