]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/assets/player/utils.ts
(embed) sandbox the iframe
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / utils.ts
CommitLineData
e945b184 1import { is18nLocale, isDefaultLocale } from '../../../../shared/models/i18n/i18n'
6cca7360 2import { VideoFile } from '../../../../shared/models/videos'
e945b184 3
c6352f2c
C
4function toTitleCase (str: string) {
5 return str.charAt(0).toUpperCase() + str.slice(1)
6}
7
8// https://github.com/danrevah/ngx-pipes/blob/master/src/pipes/math/bytes.ts
9// Don't import all Angular stuff, just copy the code with shame
10const dictionaryBytes: Array<{max: number, type: string}> = [
11 { max: 1024, type: 'B' },
12 { max: 1048576, type: 'KB' },
13 { max: 1073741824, type: 'MB' },
14 { max: 1.0995116e12, type: 'GB' }
15]
16function bytes (value) {
17 const format = dictionaryBytes.find(d => value < d.max) || dictionaryBytes[dictionaryBytes.length - 1]
18 const calc = Math.floor(value / (format.max / 1024)).toString()
19
20 return [ calc, format.type ]
21}
22
23function getStoredVolume () {
24 const value = getLocalStorage('volume')
25 if (value !== null && value !== undefined) {
26 const valueNumber = parseFloat(value)
27 if (isNaN(valueNumber)) return undefined
28
29 return valueNumber
30 }
31
32 return undefined
33}
34
35function getStoredMute () {
36 const value = getLocalStorage('mute')
37 if (value !== null && value !== undefined) return value === 'true'
38
39 return undefined
40}
41
a8462c8e
C
42function getAverageBandwidth () {
43 const value = getLocalStorage('average-bandwidth')
44 if (value !== null && value !== undefined) {
45 const valueNumber = parseInt(value, 10)
46 if (isNaN(valueNumber)) return undefined
47
48 return valueNumber
49 }
50
51 return undefined
52}
53
054a103b
C
54function getStoredTheater () {
55 const value = getLocalStorage('theater-enabled')
56 if (value !== null && value !== undefined) return value === 'true'
57
58 return undefined
59}
60
c6352f2c
C
61function saveVolumeInStore (value: number) {
62 return setLocalStorage('volume', value.toString())
63}
64
65function saveMuteInStore (value: boolean) {
66 return setLocalStorage('mute', value.toString())
67}
68
054a103b
C
69function saveTheaterInStore (enabled: boolean) {
70 return setLocalStorage('theater-enabled', enabled.toString())
71}
72
a8462c8e
C
73function saveAverageBandwidth (value: number) {
74 return setLocalStorage('average-bandwidth', value.toString())
75}
76
d1bd87e0
C
77function isMobile () {
78 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent)
79}
80
960a11e8
C
81function buildVideoLink (time?: number) {
82 let href = window.location.href.replace('/embed/', '/watch/')
83 if (time) {
84 const timeInt = Math.floor(time)
85
86 if (window.location.search) href += '&start=' + timeInt
87 else href += '?start=' + timeInt
88 }
89
90 return href
91}
92
93function buildVideoEmbed (embedUrl: string) {
94 return '<iframe width="560" height="315" ' +
77540346 95 'sandbox="allow-same-origin allow-scripts" ' +
960a11e8
C
96 'src="' + embedUrl + '" ' +
97 'frameborder="0" allowfullscreen>' +
98 '</iframe>'
99}
100
101function copyToClipboard (text: string) {
102 const el = document.createElement('textarea')
103 el.value = text
104 el.setAttribute('readonly', '')
105 el.style.position = 'absolute'
106 el.style.left = '-9999px'
107 document.body.appendChild(el)
108 el.select()
109 document.execCommand('copy')
110 document.body.removeChild(el)
111}
112
6cca7360
C
113function videoFileMaxByResolution (files: VideoFile[]) {
114 let max = files[0]
115
116 for (let i = 1; i < files.length; i++) {
117 const file = files[i]
118 if (max.resolution.id < file.resolution.id) max = file
119 }
120
121 return max
122}
123
124function videoFileMinByResolution (files: VideoFile[]) {
125 let min = files[0]
126
127 for (let i = 1; i < files.length; i++) {
128 const file = files[i]
129 if (min.resolution.id > file.resolution.id) min = file
130 }
131
132 return min
133}
134
c6352f2c
C
135export {
136 toTitleCase,
960a11e8 137 buildVideoLink,
c6352f2c
C
138 getStoredVolume,
139 saveVolumeInStore,
a8462c8e
C
140 saveAverageBandwidth,
141 getAverageBandwidth,
c6352f2c 142 saveMuteInStore,
960a11e8 143 buildVideoEmbed,
c6352f2c 144 getStoredMute,
6cca7360
C
145 videoFileMaxByResolution,
146 videoFileMinByResolution,
960a11e8 147 copyToClipboard,
054a103b
C
148 getStoredTheater,
149 saveTheaterInStore,
d1bd87e0 150 isMobile,
c6352f2c
C
151 bytes
152}
153
154// ---------------------------------------------------------------------------
155
156const KEY_PREFIX = 'peertube-videojs-'
157
158function getLocalStorage (key: string) {
159 try {
160 return localStorage.getItem(KEY_PREFIX + key)
161 } catch {
162 return undefined
163 }
164}
165
166function setLocalStorage (key: string, value: string) {
167 try {
168 localStorage.setItem(KEY_PREFIX + key, value)
169 } catch { /* empty */ }
170}