3 import 'core-js/es6/symbol'
4 import 'core-js/es6/object'
5 import 'core-js/es6/function'
6 import 'core-js/es6/parse-int'
7 import 'core-js/es6/parse-float'
8 import 'core-js/es6/number'
9 import 'core-js/es6/math'
10 import 'core-js/es6/string'
11 import 'core-js/es6/date'
12 import 'core-js/es6/array'
13 import 'core-js/es6/regexp'
14 import 'core-js/es6/map'
15 import 'core-js/es6/weak-map'
16 import 'core-js/es6/set'
17 // For google bot that uses Chrome 41 and does not understand fetch
20 import * as videojs from 'video.js'
22 import { VideoDetails } from '../../../../shared'
23 import { addContextMenu, getVideojsOptions, loadLocale } from '../../assets/player/peertube-player'
25 function getVideoUrl (id: string) {
26 return window.location.origin + '/api/v1/videos/' + id
29 function loadVideoInfo (videoId: string): Promise<Response> {
30 return fetch(getVideoUrl(videoId))
33 function removeElement (element: HTMLElement) {
34 element.parentElement.removeChild(element)
37 function displayError (videoElement: HTMLVideoElement, text: string) {
38 // Remove video element
39 removeElement(videoElement)
41 document.title = 'Sorry - ' + text
43 const errorBlock = document.getElementById('error-block')
44 errorBlock.style.display = 'flex'
46 const errorText = document.getElementById('error-content')
47 errorText.innerHTML = text
50 function videoNotFound (videoElement: HTMLVideoElement) {
51 const text = 'This video does not exist.'
52 displayError(videoElement, text)
55 function videoFetchError (videoElement: HTMLVideoElement) {
56 const text = 'We cannot fetch the video. Please try again later.'
57 displayError(videoElement, text)
60 const urlParts = window.location.href.split('/')
61 const lastPart = urlParts[urlParts.length - 1]
62 const videoId = lastPart.indexOf('?') === -1 ? lastPart : lastPart.split('?')[0]
64 loadLocale(window.location.origin, videojs, navigator.language)
65 .then(() => loadVideoInfo(videoId))
66 .then(async response => {
67 const videoContainerId = 'video-container'
68 const videoElement = document.getElementById(videoContainerId) as HTMLVideoElement
71 if (response.status === 404) return videoNotFound(videoElement)
73 return videoFetchError(videoElement)
76 const videoInfo: VideoDetails = await response.json()
82 let params = new URL(window.location.toString()).searchParams
83 autoplay = params.has('autoplay') && (params.get('autoplay') === '1' || params.get('autoplay') === 'true')
85 const startTimeParamString = params.get('start')
86 const startTimeParamNumber = parseInt(startTimeParamString, 10)
87 if (isNaN(startTimeParamNumber) === false) startTime = startTimeParamNumber
89 console.error('Cannot get params from URL.', err)
92 const videojsOptions = getVideojsOptions({
94 inactivityTimeout: 1500,
95 videoViewUrl: getVideoUrl(videoId) + '/views',
96 playerElement: videoElement,
97 videoFiles: videoInfo.files,
98 videoDuration: videoInfo.duration,
101 poster: window.location.origin + videoInfo.previewPath,
105 videojs(videoContainerId, videojsOptions, function () {
109 title: videoInfo.name,
110 description: player.localize('Uses P2P, others may know you are watching this video.')
113 addContextMenu(player, window.location.origin + videoInfo.embedPath)
116 .catch(err => console.error(err))