1 function getStoredVolume () {
2 const value = getLocalStorage('volume')
3 if (value !== null && value !== undefined) {
4 const valueNumber = parseFloat(value)
5 if (isNaN(valueNumber)) return undefined
13 function getStoredMute () {
14 const value = getLocalStorage('mute')
15 if (value !== null && value !== undefined) return value === 'true'
20 function getStoredTheater () {
21 const value = getLocalStorage('theater-enabled')
22 if (value !== null && value !== undefined) return value === 'true'
27 function saveVolumeInStore (value: number) {
28 return setLocalStorage('volume', value.toString())
31 function saveMuteInStore (value: boolean) {
32 return setLocalStorage('mute', value.toString())
35 function saveTheaterInStore (enabled: boolean) {
36 return setLocalStorage('theater-enabled', enabled.toString())
39 function saveAverageBandwidth (value: number) {
40 /** used to choose the most fitting resolution */
41 return setLocalStorage('average-bandwidth', value.toString())
44 function getAverageBandwidthInStore () {
45 const value = getLocalStorage('average-bandwidth')
46 if (value !== null && value !== undefined) {
47 const valueNumber = parseInt(value, 10)
48 if (isNaN(valueNumber)) return undefined
56 function saveLastSubtitle (language: string) {
57 return setLocalStorage('last-subtitle', language)
60 function getStoredLastSubtitle () {
61 return getLocalStorage('last-subtitle')
64 function saveVideoWatchHistory (videoUUID: string, duration: number) {
65 return setLocalStorage(`video-watch-history`, JSON.stringify({
66 ...getStoredVideoWatchHistory(),
70 date: `${(new Date()).toISOString()}`
75 function getStoredVideoWatchHistory (videoUUID?: string) {
79 const value = getLocalStorage('video-watch-history')
82 data = JSON.parse(value)
84 console.error('Cannot parse video watch history from local storage: ', error)
89 if (videoUUID) return data[videoUUID]
94 function cleanupVideoWatch () {
95 const data = getStoredVideoWatchHistory()
98 const newData = Object.keys(data).reduce((acc, videoUUID) => {
99 const date = Date.parse(data[videoUUID].date)
101 const diff = Math.ceil(((new Date()).getTime() - date) / (1000 * 3600 * 24))
103 if (diff > 30) return acc
107 [videoUUID]: data[videoUUID]
111 setLocalStorage('video-watch-history', JSON.stringify(newData))
114 // ---------------------------------------------------------------------------
123 saveAverageBandwidth,
124 getAverageBandwidthInStore,
126 getStoredLastSubtitle,
127 saveVideoWatchHistory,
128 getStoredVideoWatchHistory,
132 // ---------------------------------------------------------------------------
134 const KEY_PREFIX = 'peertube-videojs-'
136 function getLocalStorage (key: string) {
138 return localStorage.getItem(KEY_PREFIX + key)
144 function setLocalStorage (key: string, value: string) {
146 localStorage.setItem(KEY_PREFIX + key, value)
147 } catch { /* empty */