]>
Commit | Line | Data |
---|---|---|
202e7223 | 1 | import './embed.scss' |
583eb04b | 2 | import videojs from 'video.js' |
a4ff3100 C |
3 | import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index' |
4 | import { Tokens } from '@root-helpers/users' | |
bd45d503 | 5 | import { peertubeTranslate } from '../../../../shared/core-utils/i18n' |
3f9c4955 | 6 | import { |
3f9c4955 C |
7 | ResultList, |
8 | ServerConfig, | |
583eb04b C |
9 | UserRefreshToken, |
10 | VideoCaption, | |
4504f09f | 11 | VideoDetails, |
5abc96fc C |
12 | VideoPlaylist, |
13 | VideoPlaylistElement, | |
583eb04b C |
14 | VideoStreamingPlaylistType |
15 | } from '../../../../shared/models' | |
16 | import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode } from '../../assets/player/peertube-player-manager' | |
abb3097e | 17 | import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings' |
583eb04b C |
18 | import { TranslationsManager } from '../../assets/player/translations-manager' |
19 | import { PeerTubeEmbedApi } from './embed-api' | |
abb3097e C |
20 | |
21 | type Translations = { [ id: string ]: string } | |
202e7223 | 22 | |
5efab546 | 23 | export class PeerTubeEmbed { |
5abc96fc | 24 | playerElement: HTMLVideoElement |
7e37e111 | 25 | player: videojs.Player |
902aa3a0 | 26 | api: PeerTubeEmbedApi = null |
5abc96fc | 27 | |
3b019808 C |
28 | autoplay: boolean |
29 | controls: boolean | |
30 | muted: boolean | |
31 | loop: boolean | |
32 | subtitle: string | |
902aa3a0 | 33 | enableApi = false |
1f6824c9 | 34 | startTime: number | string = 0 |
f0a39880 | 35 | stopTime: number | string |
5efab546 C |
36 | |
37 | title: boolean | |
38 | warningTitle: boolean | |
08d9ba0f | 39 | peertubeLink: boolean |
5efab546 C |
40 | bigPlayBackgroundColor: string |
41 | foregroundColor: string | |
42 | ||
3b6f205c | 43 | mode: PlayerMode |
902aa3a0 C |
44 | scope = 'peertube' |
45 | ||
a4ff3100 | 46 | userTokens: Tokens |
71ab65d0 | 47 | headers = new Headers() |
4504f09f RK |
48 | LOCAL_STORAGE_OAUTH_CLIENT_KEYS = { |
49 | CLIENT_ID: 'client_id', | |
50 | CLIENT_SECRET: 'client_secret' | |
51 | } | |
71ab65d0 | 52 | |
5abc96fc C |
53 | private translationsPromise: Promise<{ [id: string]: string }> |
54 | private configPromise: Promise<ServerConfig> | |
55 | private PeertubePlayerManagerModulePromise: Promise<any> | |
56 | ||
57 | private playlist: VideoPlaylist | |
58 | private playlistElements: VideoPlaylistElement[] | |
59 | private currentPlaylistElement: VideoPlaylistElement | |
60 | ||
61 | private wrapperElement: HTMLElement | |
62 | ||
902aa3a0 | 63 | static async main () { |
5abc96fc | 64 | const videoContainerId = 'video-wrapper' |
99941732 WL |
65 | const embed = new PeerTubeEmbed(videoContainerId) |
66 | await embed.init() | |
67 | } | |
902aa3a0 | 68 | |
5abc96fc C |
69 | constructor (private videoWrapperId: string) { |
70 | this.wrapperElement = document.getElementById(this.videoWrapperId) | |
902aa3a0 C |
71 | } |
72 | ||
99941732 WL |
73 | getVideoUrl (id: string) { |
74 | return window.location.origin + '/api/v1/videos/' + id | |
75 | } | |
d4f3fea6 | 76 | |
4504f09f RK |
77 | refreshFetch (url: string, options?: Object) { |
78 | return fetch(url, options) | |
79 | .then((res: Response) => { | |
80 | if (res.status !== 401) return res | |
81 | ||
82 | // 401 unauthorized is not catch-ed, but then-ed | |
83 | const error = res | |
84 | ||
85 | const refreshingTokenPromise = new Promise((resolve, reject) => { | |
86 | const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID) | |
87 | const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET) | |
88 | const headers = new Headers() | |
89 | headers.set('Content-Type', 'application/x-www-form-urlencoded') | |
90 | const data = { | |
a4ff3100 | 91 | refresh_token: this.userTokens.refreshToken, |
4504f09f RK |
92 | client_id: clientId, |
93 | client_secret: clientSecret, | |
94 | response_type: 'code', | |
95 | grant_type: 'refresh_token' | |
96 | } | |
97 | ||
98 | fetch('/api/v1/users/token', { | |
99 | headers, | |
100 | method: 'POST', | |
101 | body: objectToUrlEncoded(data) | |
102 | }) | |
103 | .then(res => res.json()) | |
104 | .then((obj: UserRefreshToken) => { | |
a4ff3100 C |
105 | this.userTokens.accessToken = obj.access_token |
106 | this.userTokens.refreshToken = obj.refresh_token | |
107 | this.userTokens.save() | |
108 | ||
109 | this.setHeadersFromTokens() | |
110 | ||
4504f09f RK |
111 | resolve() |
112 | }) | |
113 | .catch((refreshTokenError: any) => { | |
114 | reject(refreshTokenError) | |
115 | }) | |
116 | }) | |
117 | ||
118 | return refreshingTokenPromise | |
119 | .catch(() => { | |
120 | // If refreshing fails, continue with original error | |
121 | throw error | |
122 | }) | |
123 | .then(() => fetch(url, { | |
124 | ...options, | |
125 | headers: this.headers | |
126 | })) | |
127 | }) | |
128 | } | |
129 | ||
5abc96fc C |
130 | getPlaylistUrl (id: string) { |
131 | return window.location.origin + '/api/v1/video-playlists/' + id | |
132 | } | |
133 | ||
99941732 | 134 | loadVideoInfo (videoId: string): Promise<Response> { |
4504f09f | 135 | return this.refreshFetch(this.getVideoUrl(videoId), { headers: this.headers }) |
99941732 | 136 | } |
d4f3fea6 | 137 | |
16f7022b | 138 | loadVideoCaptions (videoId: string): Promise<Response> { |
4504f09f | 139 | return fetch(this.getVideoUrl(videoId) + '/captions') |
16f7022b C |
140 | } |
141 | ||
5abc96fc C |
142 | loadPlaylistInfo (playlistId: string): Promise<Response> { |
143 | return fetch(this.getPlaylistUrl(playlistId)) | |
144 | } | |
145 | ||
146 | loadPlaylistElements (playlistId: string): Promise<Response> { | |
147 | return fetch(this.getPlaylistUrl(playlistId) + '/videos') | |
148 | } | |
149 | ||
150 | loadConfig (): Promise<ServerConfig> { | |
31b6ddf8 | 151 | return fetch('/api/v1/config') |
5abc96fc | 152 | .then(res => res.json()) |
31b6ddf8 C |
153 | } |
154 | ||
99941732 WL |
155 | removeElement (element: HTMLElement) { |
156 | element.parentElement.removeChild(element) | |
157 | } | |
d4f3fea6 | 158 | |
abb3097e | 159 | displayError (text: string, translations?: Translations) { |
99941732 | 160 | // Remove video element |
5abc96fc C |
161 | if (this.playerElement) { |
162 | this.removeElement(this.playerElement) | |
163 | this.playerElement = undefined | |
164 | } | |
99941732 | 165 | |
ad3fa0c5 C |
166 | const translatedText = peertubeTranslate(text, translations) |
167 | const translatedSorry = peertubeTranslate('Sorry', translations) | |
168 | ||
169 | document.title = translatedSorry + ' - ' + translatedText | |
99941732 WL |
170 | |
171 | const errorBlock = document.getElementById('error-block') | |
172 | errorBlock.style.display = 'flex' | |
173 | ||
ad3fa0c5 C |
174 | const errorTitle = document.getElementById('error-title') |
175 | errorTitle.innerHTML = peertubeTranslate('Sorry', translations) | |
176 | ||
99941732 | 177 | const errorText = document.getElementById('error-content') |
ad3fa0c5 | 178 | errorText.innerHTML = translatedText |
99941732 WL |
179 | } |
180 | ||
abb3097e | 181 | videoNotFound (translations?: Translations) { |
99941732 | 182 | const text = 'This video does not exist.' |
ad3fa0c5 | 183 | this.displayError(text, translations) |
99941732 WL |
184 | } |
185 | ||
abb3097e | 186 | videoFetchError (translations?: Translations) { |
99941732 | 187 | const text = 'We cannot fetch the video. Please try again later.' |
ad3fa0c5 | 188 | this.displayError(text, translations) |
99941732 WL |
189 | } |
190 | ||
5abc96fc C |
191 | playlistNotFound (translations?: Translations) { |
192 | const text = 'This playlist does not exist.' | |
193 | this.displayError(text, translations) | |
194 | } | |
195 | ||
196 | playlistFetchError (translations?: Translations) { | |
197 | const text = 'We cannot fetch the playlist. Please try again later.' | |
198 | this.displayError(text, translations) | |
199 | } | |
200 | ||
3b019808 | 201 | getParamToggle (params: URLSearchParams, name: string, defaultValue?: boolean) { |
99941732 WL |
202 | return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue |
203 | } | |
d4f3fea6 | 204 | |
3b019808 | 205 | getParamString (params: URLSearchParams, name: string, defaultValue?: string) { |
99941732 WL |
206 | return params.has(name) ? params.get(name) : defaultValue |
207 | } | |
da99ccf2 | 208 | |
902aa3a0 | 209 | async init () { |
99941732 | 210 | try { |
a4ff3100 | 211 | this.userTokens = Tokens.load() |
99941732 WL |
212 | await this.initCore() |
213 | } catch (e) { | |
214 | console.error(e) | |
215 | } | |
216 | } | |
217 | ||
902aa3a0 C |
218 | private initializeApi () { |
219 | if (!this.enableApi) return | |
220 | ||
221 | this.api = new PeerTubeEmbedApi(this) | |
222 | this.api.initialize() | |
223 | } | |
224 | ||
0f2f274c | 225 | private loadParams (video: VideoDetails) { |
da99ccf2 | 226 | try { |
c4710631 | 227 | const params = new URL(window.location.toString()).searchParams |
99941732 | 228 | |
31b6ddf8 C |
229 | this.autoplay = this.getParamToggle(params, 'autoplay', false) |
230 | this.controls = this.getParamToggle(params, 'controls', true) | |
64645512 | 231 | this.muted = this.getParamToggle(params, 'muted', undefined) |
31b6ddf8 | 232 | this.loop = this.getParamToggle(params, 'loop', false) |
5efab546 | 233 | this.title = this.getParamToggle(params, 'title', true) |
99941732 | 234 | this.enableApi = this.getParamToggle(params, 'api', this.enableApi) |
5efab546 | 235 | this.warningTitle = this.getParamToggle(params, 'warningTitle', true) |
08d9ba0f | 236 | this.peertubeLink = this.getParamToggle(params, 'peertubeLink', true) |
f37bad63 | 237 | |
3b019808 C |
238 | this.scope = this.getParamString(params, 'scope', this.scope) |
239 | this.subtitle = this.getParamString(params, 'subtitle') | |
240 | this.startTime = this.getParamString(params, 'start') | |
f0a39880 | 241 | this.stopTime = this.getParamString(params, 'stop') |
3b6f205c | 242 | |
5efab546 C |
243 | this.bigPlayBackgroundColor = this.getParamString(params, 'bigPlayBackgroundColor') |
244 | this.foregroundColor = this.getParamString(params, 'foregroundColor') | |
245 | ||
0f2f274c C |
246 | const modeParam = this.getParamString(params, 'mode') |
247 | ||
248 | if (modeParam) { | |
249 | if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader' | |
250 | else this.mode = 'webtorrent' | |
251 | } else { | |
252 | if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader' | |
253 | else this.mode = 'webtorrent' | |
254 | } | |
da99ccf2 C |
255 | } catch (err) { |
256 | console.error('Cannot get params from URL.', err) | |
257 | } | |
99941732 WL |
258 | } |
259 | ||
5abc96fc C |
260 | private async loadPlaylist (playlistId: string) { |
261 | const playlistPromise = this.loadPlaylistInfo(playlistId) | |
262 | const playlistElementsPromise = this.loadPlaylistElements(playlistId) | |
99941732 | 263 | |
5abc96fc C |
264 | const playlistResponse = await playlistPromise |
265 | ||
266 | if (!playlistResponse.ok) { | |
267 | const serverTranslations = await this.translationsPromise | |
71ab65d0 | 268 | |
5abc96fc C |
269 | if (playlistResponse.status === 404) { |
270 | this.playlistNotFound(serverTranslations) | |
271 | return undefined | |
272 | } | |
273 | ||
274 | this.playlistFetchError(serverTranslations) | |
275 | return undefined | |
276 | } | |
277 | ||
278 | return { playlistResponse, videosResponse: await playlistElementsPromise } | |
279 | } | |
280 | ||
281 | private async loadVideo (videoId: string) { | |
3f9c4955 | 282 | const videoPromise = this.loadVideoInfo(videoId) |
3f9c4955 | 283 | |
3f9c4955 | 284 | const videoResponse = await videoPromise |
99941732 | 285 | |
16f7022b | 286 | if (!videoResponse.ok) { |
5abc96fc C |
287 | const serverTranslations = await this.translationsPromise |
288 | ||
289 | if (videoResponse.status === 404) { | |
290 | this.videoNotFound(serverTranslations) | |
291 | return undefined | |
292 | } | |
293 | ||
294 | this.videoFetchError(serverTranslations) | |
295 | return undefined | |
296 | } | |
297 | ||
298 | const captionsPromise = this.loadVideoCaptions(videoId) | |
299 | ||
300 | return { captionsPromise, videoResponse } | |
301 | } | |
3f9c4955 | 302 | |
5abc96fc C |
303 | private async buildPlaylistManager () { |
304 | const translations = await this.translationsPromise | |
305 | ||
306 | this.player.upnext({ | |
307 | timeout: 10000, // 10s | |
308 | headText: peertubeTranslate('Up Next', translations), | |
309 | cancelText: peertubeTranslate('Cancel', translations), | |
310 | suspendedText: peertubeTranslate('Autoplay is suspended', translations), | |
311 | getTitle: () => this.nextVideoTitle(), | |
312 | next: () => this.autoplayNext(), | |
313 | condition: () => !!this.getNextPlaylistElement(), | |
314 | suspended: () => false | |
315 | }) | |
316 | } | |
99941732 | 317 | |
5abc96fc C |
318 | private async autoplayNext () { |
319 | const next = this.getNextPlaylistElement() | |
320 | if (!next) { | |
321 | console.log('Next element not found in playlist.') | |
322 | return | |
99941732 WL |
323 | } |
324 | ||
5abc96fc | 325 | this.currentPlaylistElement = next |
3f9c4955 | 326 | |
4572c3d0 C |
327 | return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid) |
328 | } | |
329 | ||
330 | private async loadVideoAndBuildPlayer (uuid: string) { | |
331 | const res = await this.loadVideo(uuid) | |
5abc96fc C |
332 | if (res === undefined) return |
333 | ||
334 | return this.buildVideoPlayer(res.videoResponse, res.captionsPromise) | |
335 | } | |
3f9c4955 | 336 | |
5abc96fc C |
337 | private nextVideoTitle () { |
338 | const next = this.getNextPlaylistElement() | |
339 | if (!next) return '' | |
340 | ||
341 | return next.video.name | |
342 | } | |
343 | ||
344 | private getNextPlaylistElement (position?: number): VideoPlaylistElement { | |
345 | if (!position) position = this.currentPlaylistElement.position + 1 | |
346 | ||
347 | if (position > this.playlist.videosLength) { | |
348 | return undefined | |
349 | } | |
350 | ||
351 | const next = this.playlistElements.find(e => e.position === position) | |
352 | ||
353 | if (!next || !next.video) { | |
354 | return this.getNextPlaylistElement(position + 1) | |
355 | } | |
356 | ||
357 | return next | |
358 | } | |
359 | ||
360 | private async buildVideoPlayer (videoResponse: Response, captionsPromise: Promise<Response>) { | |
361 | let alreadyHadPlayer = false | |
362 | ||
363 | if (this.player) { | |
364 | this.player.dispose() | |
365 | alreadyHadPlayer = true | |
366 | } | |
367 | ||
368 | this.playerElement = document.createElement('video') | |
369 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
370 | this.playerElement.setAttribute('playsinline', 'true') | |
371 | this.wrapperElement.appendChild(this.playerElement) | |
372 | ||
373 | const videoInfoPromise = videoResponse.json() | |
374 | .then((videoInfo: VideoDetails) => { | |
375 | if (!alreadyHadPlayer) this.loadPlaceholder(videoInfo) | |
376 | ||
377 | return videoInfo | |
378 | }) | |
379 | ||
380 | const [ videoInfo, serverTranslations, captionsResponse, config, PeertubePlayerManagerModule ] = await Promise.all([ | |
381 | videoInfoPromise, | |
382 | this.translationsPromise, | |
383 | captionsPromise, | |
384 | this.configPromise, | |
385 | this.PeertubePlayerManagerModulePromise | |
386 | ]) | |
3f9c4955 C |
387 | |
388 | const PeertubePlayerManager = PeertubePlayerManagerModule.PeertubePlayerManager | |
5efab546 | 389 | const videoCaptions = await this.buildCaptions(serverTranslations, captionsResponse) |
99941732 | 390 | |
0f2f274c | 391 | this.loadParams(videoInfo) |
da99ccf2 | 392 | |
4572c3d0 C |
393 | const playlistPlugin = this.currentPlaylistElement |
394 | ? { | |
395 | elements: this.playlistElements, | |
396 | playlist: this.playlist, | |
397 | ||
398 | getCurrentPosition: () => this.currentPlaylistElement.position, | |
399 | ||
400 | onItemClicked: (videoPlaylistElement: VideoPlaylistElement) => { | |
401 | this.currentPlaylistElement = videoPlaylistElement | |
402 | ||
403 | this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid) | |
404 | .catch(err => console.error(err)) | |
405 | } | |
406 | } | |
407 | : undefined | |
408 | ||
2adfc7ea C |
409 | const options: PeertubePlayerManagerOptions = { |
410 | common: { | |
5abc96fc C |
411 | // Autoplay in playlist mode |
412 | autoplay: alreadyHadPlayer ? true : this.autoplay, | |
2adfc7ea C |
413 | controls: this.controls, |
414 | muted: this.muted, | |
415 | loop: this.loop, | |
416 | captions: videoCaptions.length !== 0, | |
417 | startTime: this.startTime, | |
f0a39880 | 418 | stopTime: this.stopTime, |
2adfc7ea C |
419 | subtitle: this.subtitle, |
420 | ||
5abc96fc | 421 | nextVideo: () => this.autoplayNext(), |
4572c3d0 | 422 | playlist: playlistPlugin, |
5abc96fc | 423 | |
2adfc7ea | 424 | videoCaptions, |
35f0a5e6 | 425 | inactivityTimeout: 2500, |
5abc96fc | 426 | videoViewUrl: this.getVideoUrl(videoInfo.uuid) + '/views', |
6ec0b75b | 427 | |
5abc96fc C |
428 | playerElement: this.playerElement, |
429 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, | |
6ec0b75b | 430 | |
2adfc7ea C |
431 | videoDuration: videoInfo.duration, |
432 | enableHotkeys: true, | |
08d9ba0f | 433 | peertubeLink: this.peertubeLink, |
2adfc7ea | 434 | poster: window.location.origin + videoInfo.previewPath, |
3d9a63d3 | 435 | theaterButton: false, |
2adfc7ea C |
436 | |
437 | serverUrl: window.location.origin, | |
438 | language: navigator.language, | |
439 | embedUrl: window.location.origin + videoInfo.embedPath | |
6ec0b75b C |
440 | }, |
441 | ||
442 | webtorrent: { | |
443 | videoFiles: videoInfo.files | |
2adfc7ea | 444 | } |
3b6f205c | 445 | } |
2adfc7ea | 446 | |
3b6f205c | 447 | if (this.mode === 'p2p-media-loader') { |
09209296 C |
448 | const hlsPlaylist = videoInfo.streamingPlaylists.find(p => p.type === VideoStreamingPlaylistType.HLS) |
449 | ||
3b6f205c C |
450 | Object.assign(options, { |
451 | p2pMediaLoader: { | |
09209296 C |
452 | playlistUrl: hlsPlaylist.playlistUrl, |
453 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
454 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
455 | trackerAnnounce: videoInfo.trackerUrls, | |
5a71acd2 | 456 | videoFiles: hlsPlaylist.files |
09209296 | 457 | } as P2PMediaLoaderOptions |
3b6f205c | 458 | }) |
2adfc7ea | 459 | } |
202e7223 | 460 | |
7e37e111 | 461 | this.player = await PeertubePlayerManager.initialize(this.mode, options, (player: videojs.Player) => this.player = player) |
2adfc7ea | 462 | this.player.on('customError', (event: any, data: any) => this.handleError(data.err, serverTranslations)) |
99941732 | 463 | |
2adfc7ea | 464 | window[ 'videojsPlayer' ] = this.player |
902aa3a0 | 465 | |
5efab546 C |
466 | this.buildCSS() |
467 | ||
5abc96fc | 468 | await this.buildDock(videoInfo, config) |
5efab546 C |
469 | |
470 | this.initializeApi() | |
3f9c4955 C |
471 | |
472 | this.removePlaceholder() | |
5abc96fc C |
473 | |
474 | if (this.isPlaylistEmbed()) { | |
475 | await this.buildPlaylistManager() | |
4572c3d0 | 476 | this.player.playlist().updateSelected() |
5abc96fc C |
477 | } |
478 | } | |
479 | ||
480 | private async initCore () { | |
481 | if (this.userTokens) this.setHeadersFromTokens() | |
482 | ||
483 | this.configPromise = this.loadConfig() | |
484 | this.translationsPromise = TranslationsManager.getServerTranslations(window.location.origin, navigator.language) | |
485 | this.PeertubePlayerManagerModulePromise = import('../../assets/player/peertube-player-manager') | |
486 | ||
487 | let videoId: string | |
488 | ||
489 | if (this.isPlaylistEmbed()) { | |
490 | const playlistId = this.getResourceId() | |
491 | const res = await this.loadPlaylist(playlistId) | |
492 | if (!res) return undefined | |
493 | ||
494 | this.playlist = await res.playlistResponse.json() | |
495 | ||
496 | const playlistElementResult = await res.videosResponse.json() | |
497 | this.playlistElements = playlistElementResult.data | |
498 | ||
499 | this.currentPlaylistElement = this.playlistElements[0] | |
500 | videoId = this.currentPlaylistElement.video.uuid | |
501 | } else { | |
502 | videoId = this.getResourceId() | |
503 | } | |
504 | ||
4572c3d0 | 505 | return this.loadVideoAndBuildPlayer(videoId) |
5efab546 C |
506 | } |
507 | ||
508 | private handleError (err: Error, translations?: { [ id: string ]: string }) { | |
509 | if (err.message.indexOf('from xs param') !== -1) { | |
510 | this.player.dispose() | |
5abc96fc | 511 | this.playerElement = null |
5efab546 C |
512 | this.displayError('This video is not available because the remote instance is not responding.', translations) |
513 | return | |
514 | } | |
515 | } | |
516 | ||
5abc96fc | 517 | private async buildDock (videoInfo: VideoDetails, config: ServerConfig) { |
abb3097e | 518 | if (!this.controls) return |
5efab546 | 519 | |
818c449b C |
520 | // On webtorrent fallback, player may have been disposed |
521 | if (!this.player.player_) return | |
5efab546 | 522 | |
abb3097e | 523 | const title = this.title ? videoInfo.name : undefined |
31b6ddf8 | 524 | |
abb3097e C |
525 | const description = config.tracker.enabled && this.warningTitle |
526 | ? '<span class="text">' + peertubeTranslate('Watching this video may reveal your IP address to others.') + '</span>' | |
527 | : undefined | |
528 | ||
529 | this.player.dock({ | |
530 | title, | |
531 | description | |
532 | }) | |
5efab546 | 533 | } |
16f7022b | 534 | |
5efab546 C |
535 | private buildCSS () { |
536 | const body = document.getElementById('custom-css') | |
537 | ||
538 | if (this.bigPlayBackgroundColor) { | |
539 | body.style.setProperty('--embedBigPlayBackgroundColor', this.bigPlayBackgroundColor) | |
540 | } | |
541 | ||
542 | if (this.foregroundColor) { | |
543 | body.style.setProperty('--embedForegroundColor', this.foregroundColor) | |
544 | } | |
99941732 | 545 | } |
6d88de72 | 546 | |
5efab546 C |
547 | private async buildCaptions (serverTranslations: any, captionsResponse: Response): Promise<VideoJSCaption[]> { |
548 | if (captionsResponse.ok) { | |
549 | const { data } = (await captionsResponse.json()) as ResultList<VideoCaption> | |
550 | ||
551 | return data.map(c => ({ | |
552 | label: peertubeTranslate(c.language.label, serverTranslations), | |
553 | language: c.language.id, | |
554 | src: window.location.origin + c.captionPath | |
555 | })) | |
6d88de72 | 556 | } |
5efab546 C |
557 | |
558 | return [] | |
6d88de72 | 559 | } |
3f9c4955 C |
560 | |
561 | private loadPlaceholder (video: VideoDetails) { | |
562 | const placeholder = this.getPlaceholderElement() | |
563 | ||
564 | const url = window.location.origin + video.previewPath | |
565 | placeholder.style.backgroundImage = `url("${url}")` | |
5abc96fc | 566 | placeholder.style.display = 'block' |
3f9c4955 C |
567 | } |
568 | ||
569 | private removePlaceholder () { | |
570 | const placeholder = this.getPlaceholderElement() | |
5abc96fc | 571 | placeholder.style.display = 'none' |
3f9c4955 C |
572 | } |
573 | ||
574 | private getPlaceholderElement () { | |
575 | return document.getElementById('placeholder-preview') | |
576 | } | |
a4ff3100 C |
577 | |
578 | private setHeadersFromTokens () { | |
579 | this.headers.set('Authorization', `${this.userTokens.tokenType} ${this.userTokens.accessToken}`) | |
580 | } | |
5abc96fc C |
581 | |
582 | private getResourceId () { | |
583 | const urlParts = window.location.pathname.split('/') | |
584 | return urlParts[ urlParts.length - 1 ] | |
585 | } | |
586 | ||
587 | private isPlaylistEmbed () { | |
588 | return window.location.pathname.split('/')[1] === 'video-playlists' | |
589 | } | |
99941732 WL |
590 | } |
591 | ||
592 | PeerTubeEmbed.main() | |
902aa3a0 | 593 | .catch(err => console.error('Cannot init embed.', err)) |