aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/assets/player/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/assets/player/utils.ts')
-rw-r--r--client/src/assets/player/utils.ts44
1 files changed, 40 insertions, 4 deletions
diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts
index c27e630e5..c02e19929 100644
--- a/client/src/assets/player/utils.ts
+++ b/client/src/assets/player/utils.ts
@@ -24,15 +24,50 @@ function isMobile () {
24} 24}
25 25
26function buildVideoLink (time?: number) { 26function buildVideoLink (time?: number) {
27 let href = window.location.href.replace('/embed/', '/watch/') 27 const baseEmbedPath = window.location.pathname.replace('/embed/', '/watch/')
28 const baseEmbedURL = window.location.origin + baseEmbedPath
29
28 if (time) { 30 if (time) {
29 const timeInt = Math.floor(time) 31 const timeInt = Math.floor(time)
30 32
31 if (window.location.search) href += '&start=' + timeInt 33 const params = new URLSearchParams(window.location.search)
32 else href += '?start=' + timeInt 34 params.set('start', secondsToTime(timeInt))
35
36 return baseEmbedURL + '?' + params.toString()
33 } 37 }
34 38
35 return href 39 return baseEmbedURL
40}
41
42function timeToInt (time: number | string) {
43 if (typeof time === 'number') return time
44
45 const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/
46 const matches = time.match(reg)
47
48 if (!matches) return 0
49
50 const hours = parseInt(matches[2] || '0', 10)
51 const minutes = parseInt(matches[4] || '0', 10)
52 const seconds = parseInt(matches[6] || '0', 10)
53
54 return hours * 3600 + minutes * 60 + seconds
55}
56
57function secondsToTime (seconds: number) {
58 let time = ''
59
60 let hours = Math.floor(seconds / 3600)
61 if (hours >= 1) time = hours + 'h'
62
63 seconds %= 3600
64 let minutes = Math.floor(seconds / 60)
65 if (minutes >= 1) time += minutes + 'm'
66
67 seconds %= 60
68 if (seconds >= 1) time += seconds + 's'
69
70 return time
36} 71}
37 72
38function buildVideoEmbed (embedUrl: string) { 73function buildVideoEmbed (embedUrl: string) {
@@ -81,6 +116,7 @@ function videoFileMinByResolution (files: VideoFile[]) {
81 116
82export { 117export {
83 toTitleCase, 118 toTitleCase,
119 timeToInt,
84 buildVideoLink, 120 buildVideoLink,
85 buildVideoEmbed, 121 buildVideoEmbed,
86 videoFileMaxByResolution, 122 videoFileMaxByResolution,