]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/standalone/videos/embed.ts
Add context menu to player
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed.ts
CommitLineData
202e7223
C
1import './embed.scss'
2
d1bd87e0
C
3import 'core-js/es6/symbol'
4import 'core-js/es6/object'
5import 'core-js/es6/function'
6import 'core-js/es6/parse-int'
7import 'core-js/es6/parse-float'
8import 'core-js/es6/number'
9import 'core-js/es6/math'
10import 'core-js/es6/string'
11import 'core-js/es6/date'
12import 'core-js/es6/array'
13import 'core-js/es6/regexp'
14import 'core-js/es6/map'
15import 'core-js/es6/weak-map'
16import 'core-js/es6/set'
17
cd4d7a2c
C
18// For google bot that uses Chrome 41 and does not understand fetch
19import 'whatwg-fetch'
20
63c4db6d 21import * as videojs from 'video.js'
c6352f2c 22
404b54e1 23import { VideoDetails } from '../../../../shared'
c6352f2c 24import { getVideojsOptions } from '../../assets/player/peertube-player'
202e7223 25
8cac1b64 26function getVideoUrl (id: string) {
7b0956ec 27 return window.location.origin + '/api/v1/videos/' + id
8cac1b64
C
28}
29
d4f3fea6
C
30function loadVideoInfo (videoId: string): Promise<Response> {
31 return fetch(getVideoUrl(videoId))
32}
33
34function removeElement (element: HTMLElement) {
35 element.parentElement.removeChild(element)
36}
37
38function displayError (videoElement: HTMLVideoElement, text: string) {
39 // Remove video element
40 removeElement(videoElement)
41
42 document.title = 'Sorry - ' + text
43
44 const errorBlock = document.getElementById('error-block')
45 errorBlock.style.display = 'flex'
46
47 const errorText = document.getElementById('error-content')
48 errorText.innerHTML = text
49}
50
51function videoNotFound (videoElement: HTMLVideoElement) {
52 const text = 'This video does not exist.'
53 displayError(videoElement, text)
54}
55
56function videoFetchError (videoElement: HTMLVideoElement) {
57 const text = 'We cannot fetch the video. Please try again later.'
58 displayError(videoElement, text)
202e7223
C
59}
60
202e7223
C
61const urlParts = window.location.href.split('/')
62const videoId = urlParts[urlParts.length - 1]
63
f2f1118f 64loadVideoInfo(videoId)
d4f3fea6 65 .then(async response => {
c6352f2c 66 const videoContainerId = 'video-container'
c6352f2c 67 const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
d4f3fea6
C
68
69 if (!response.ok) {
70 if (response.status === 404) return videoNotFound(videoElement)
71
72 return videoFetchError(videoElement)
73 }
74
75 const videoInfo: VideoDetails = await response.json()
76
da99ccf2 77 let autoplay = false
f37bad63 78 let startTime = 0
da99ccf2
C
79
80 try {
81 let params = new URL(window.location.toString()).searchParams
82 autoplay = params.has('autoplay') && (params.get('autoplay') === '1' || params.get('autoplay') === 'true')
f37bad63
C
83
84 const startTimeParamString = params.get('start')
85 const startTimeParamNumber = parseInt(startTimeParamString, 10)
86 if (isNaN(startTimeParamNumber) === false) startTime = startTimeParamNumber
da99ccf2
C
87 } catch (err) {
88 console.error('Cannot get params from URL.', err)
89 }
90
c6352f2c 91 const videojsOptions = getVideojsOptions({
da99ccf2 92 autoplay,
c6352f2c 93 inactivityTimeout: 1500,
960a11e8 94 videoEmbedUrl: window.location.origin + videoInfo.embedPath,
c6352f2c
C
95 videoViewUrl: getVideoUrl(videoId) + '/views',
96 playerElement: videoElement,
97 videoFiles: videoInfo.files,
98 videoDuration: videoInfo.duration,
99 enableHotkeys: true,
33d78552 100 peertubeLink: true,
f37bad63
C
101 poster: window.location.origin + videoInfo.previewPath,
102 startTime
c6352f2c
C
103 })
104 videojs(videoContainerId, videojsOptions, function () {
f2f1118f 105 const player = this
202e7223 106
f2f1118f 107 player.dock({
22b59e80 108 title: videoInfo.name,
606ca5bc 109 description: 'Uses P2P, others may know you are watching this video.'
f2f1118f 110 })
202e7223 111 })
202e7223 112 })
a16aee73 113 .catch(err => console.error(err))