+ autoplay: boolean
+ controls: boolean
+ muted: boolean
+ loop: boolean
+ subtitle: string
+ enableApi = false
+ startTime: number | string = 0
+ stopTime: number | string
+
+ title: boolean
+ warningTitle: boolean
+ peertubeLink: boolean
+ bigPlayBackgroundColor: string
+ foregroundColor: string
+
+ mode: PlayerMode
+ scope = 'peertube'
+
+ userTokens: Tokens
+ headers = new Headers()
+ LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {
+ CLIENT_ID: 'client_id',
+ CLIENT_SECRET: 'client_secret'
+ }
+
+ private translationsPromise: Promise<{ [id: string]: string }>
+ private configPromise: Promise<ServerConfig>
+ private PeertubePlayerManagerModulePromise: Promise<any>
+
+ private playlist: VideoPlaylist
+ private playlistElements: VideoPlaylistElement[]
+ private currentPlaylistElement: VideoPlaylistElement
+
+ private wrapperElement: HTMLElement
+
+ static async main () {
+ const videoContainerId = 'video-wrapper'
+ const embed = new PeerTubeEmbed(videoContainerId)
+ await embed.init()
+ }
+
+ constructor (private videoWrapperId: string) {
+ this.wrapperElement = document.getElementById(this.videoWrapperId)
+ }
+
+ getVideoUrl (id: string) {
+ return window.location.origin + '/api/v1/videos/' + id
+ }
+
+ refreshFetch (url: string, options?: Object) {
+ return fetch(url, options)
+ .then((res: Response) => {
+ if (res.status !== 401) return res
+
+ // 401 unauthorized is not catch-ed, but then-ed
+ const error = res
+
+ const refreshingTokenPromise = new Promise((resolve, reject) => {
+ const clientId: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)
+ const clientSecret: string = peertubeLocalStorage.getItem(this.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_SECRET)
+ const headers = new Headers()
+ headers.set('Content-Type', 'application/x-www-form-urlencoded')
+ const data = {
+ refresh_token: this.userTokens.refreshToken,
+ client_id: clientId,
+ client_secret: clientSecret,
+ response_type: 'code',
+ grant_type: 'refresh_token'
+ }
+
+ fetch('/api/v1/users/token', {
+ headers,
+ method: 'POST',
+ body: objectToUrlEncoded(data)
+ })
+ .then(res => res.json())
+ .then((obj: UserRefreshToken) => {
+ this.userTokens.accessToken = obj.access_token
+ this.userTokens.refreshToken = obj.refresh_token
+ this.userTokens.save()
+
+ this.setHeadersFromTokens()
+
+ resolve()
+ })
+ .catch((refreshTokenError: any) => {
+ reject(refreshTokenError)
+ })
+ })
+
+ return refreshingTokenPromise
+ .catch(() => {
+ // If refreshing fails, continue with original error
+ throw error
+ })
+ .then(() => fetch(url, {
+ ...options,
+ headers: this.headers
+ }))
+ })
+ }
+
+ getPlaylistUrl (id: string) {
+ return window.location.origin + '/api/v1/video-playlists/' + id
+ }
+
+ loadVideoInfo (videoId: string): Promise<Response> {
+ return this.refreshFetch(this.getVideoUrl(videoId), { headers: this.headers })
+ }
+
+ loadVideoCaptions (videoId: string): Promise<Response> {
+ return fetch(this.getVideoUrl(videoId) + '/captions')
+ }
+
+ loadPlaylistInfo (playlistId: string): Promise<Response> {
+ return fetch(this.getPlaylistUrl(playlistId))
+ }
+
+ loadPlaylistElements (playlistId: string, start = 0): Promise<Response> {
+ const url = new URL(this.getPlaylistUrl(playlistId) + '/videos')
+ url.search = new URLSearchParams({ start: '' + start, count: '100' }).toString()
+
+ return fetch(url.toString())
+ }
+
+ loadConfig (): Promise<ServerConfig> {
+ return fetch('/api/v1/config')
+ .then(res => res.json())
+ }
+
+ removeElement (element: HTMLElement) {
+ element.parentElement.removeChild(element)
+ }
+
+ displayError (text: string, translations?: Translations) {
+ // Remove video element
+ if (this.playerElement) {
+ this.removeElement(this.playerElement)
+ this.playerElement = undefined
+ }
+
+ const translatedText = peertubeTranslate(text, translations)
+ const translatedSorry = peertubeTranslate('Sorry', translations)
+
+ document.title = translatedSorry + ' - ' + translatedText
+
+ const errorBlock = document.getElementById('error-block')
+ errorBlock.style.display = 'flex'
+
+ const errorTitle = document.getElementById('error-title')
+ errorTitle.innerHTML = peertubeTranslate('Sorry', translations)
+
+ const errorText = document.getElementById('error-content')
+ errorText.innerHTML = translatedText
+ }
+
+ videoNotFound (translations?: Translations) {
+ const text = 'This video does not exist.'
+ this.displayError(text, translations)
+ }
+
+ videoFetchError (translations?: Translations) {
+ const text = 'We cannot fetch the video. Please try again later.'
+ this.displayError(text, translations)
+ }
+
+ playlistNotFound (translations?: Translations) {
+ const text = 'This playlist does not exist.'
+ this.displayError(text, translations)
+ }
+
+ playlistFetchError (translations?: Translations) {
+ const text = 'We cannot fetch the playlist. Please try again later.'
+ this.displayError(text, translations)
+ }
+
+ getParamToggle (params: URLSearchParams, name: string, defaultValue?: boolean) {
+ return params.has(name) ? (params.get(name) === '1' || params.get(name) === 'true') : defaultValue
+ }
+
+ getParamString (params: URLSearchParams, name: string, defaultValue?: string) {
+ return params.has(name) ? params.get(name) : defaultValue
+ }
+
+ async init () {
+ try {
+ this.userTokens = Tokens.load()
+ await this.initCore()
+ } catch (e) {
+ console.error(e)
+ }
+ }
+
+ private initializeApi () {
+ if (!this.enableApi) return
+
+ this.api = new PeerTubeEmbedApi(this)
+ this.api.initialize()
+ }
+
+ private loadParams (video: VideoDetails) {
+ try {
+ const params = new URL(window.location.toString()).searchParams
+
+ this.autoplay = this.getParamToggle(params, 'autoplay', false)
+ this.controls = this.getParamToggle(params, 'controls', true)
+ this.muted = this.getParamToggle(params, 'muted', undefined)
+ this.loop = this.getParamToggle(params, 'loop', false)
+ this.title = this.getParamToggle(params, 'title', true)
+ this.enableApi = this.getParamToggle(params, 'api', this.enableApi)
+ this.warningTitle = this.getParamToggle(params, 'warningTitle', true)
+ this.peertubeLink = this.getParamToggle(params, 'peertubeLink', true)
+
+ this.scope = this.getParamString(params, 'scope', this.scope)
+ this.subtitle = this.getParamString(params, 'subtitle')
+ this.startTime = this.getParamString(params, 'start')
+ this.stopTime = this.getParamString(params, 'stop')
+
+ this.bigPlayBackgroundColor = this.getParamString(params, 'bigPlayBackgroundColor')
+ this.foregroundColor = this.getParamString(params, 'foregroundColor')
+
+ const modeParam = this.getParamString(params, 'mode')
+
+ if (modeParam) {
+ if (modeParam === 'p2p-media-loader') this.mode = 'p2p-media-loader'
+ else this.mode = 'webtorrent'
+ } else {
+ if (Array.isArray(video.streamingPlaylists) && video.streamingPlaylists.length !== 0) this.mode = 'p2p-media-loader'
+ else this.mode = 'webtorrent'
+ }
+ } catch (err) {
+ console.error('Cannot get params from URL.', err)
+ }
+ }
+
+ private async loadAllPlaylistVideos (playlistId: string, baseResult: ResultList<VideoPlaylistElement>) {
+ let elements = baseResult.data
+ let total = baseResult.total
+ let i = 0
+
+ while (total > elements.length && i < 10) {
+ const result = await this.loadPlaylistElements(playlistId, elements.length)
+
+ const json = await result.json() as ResultList<VideoPlaylistElement>
+ total = json.total
+
+ elements = elements.concat(json.data)
+ i++
+ }
+
+ if (i === 10) {
+ console.error('Cannot fetch all playlists elements, there are too many!')
+ }
+
+ return elements
+ }
+
+ private async loadPlaylist (playlistId: string) {
+ const playlistPromise = this.loadPlaylistInfo(playlistId)
+ const playlistElementsPromise = this.loadPlaylistElements(playlistId)
+
+ const playlistResponse = await playlistPromise
+
+ if (!playlistResponse.ok) {
+ const serverTranslations = await this.translationsPromise
+
+ if (playlistResponse.status === 404) {
+ this.playlistNotFound(serverTranslations)
+ return undefined
+ }
+
+ this.playlistFetchError(serverTranslations)
+ return undefined
+ }
+
+ return { playlistResponse, videosResponse: await playlistElementsPromise }
+ }
+
+ private async loadVideo (videoId: string) {
+ const videoPromise = this.loadVideoInfo(videoId)
+
+ const videoResponse = await videoPromise
+
+ if (!videoResponse.ok) {
+ const serverTranslations = await this.translationsPromise
+
+ if (videoResponse.status === 404) {
+ this.videoNotFound(serverTranslations)
+ return undefined
+ }
+
+ this.videoFetchError(serverTranslations)
+ return undefined
+ }
+
+ const captionsPromise = this.loadVideoCaptions(videoId)
+
+ return { captionsPromise, videoResponse }
+ }
+
+ private async buildPlaylistManager () {
+ const translations = await this.translationsPromise
+
+ this.player.upnext({
+ timeout: 10000, // 10s
+ headText: peertubeTranslate('Up Next', translations),
+ cancelText: peertubeTranslate('Cancel', translations),
+ suspendedText: peertubeTranslate('Autoplay is suspended', translations),
+ getTitle: () => this.nextVideoTitle(),
+ next: () => this.playNextVideo(),
+ condition: () => !!this.getNextPlaylistElement(),
+ suspended: () => false
+ })
+ }
+
+ private async playNextVideo () {
+ const next = this.getNextPlaylistElement()
+ if (!next) {
+ console.log('Next element not found in playlist.')
+ return
+ }
+
+ this.currentPlaylistElement = next
+
+ return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid)
+ }
+
+ private async playPreviousVideo () {
+ const previous = this.getPreviousPlaylistElement()
+ if (!previous) {
+ console.log('Previous element not found in playlist.')
+ return
+ }
+
+ this.currentPlaylistElement = previous
+
+ return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid)
+ }