From d350f7604fec90adb02ed9af3fe1ce9e734b4f01 Mon Sep 17 00:00:00 2001 From: thecashewtrader Date: Thu, 3 Nov 2022 21:33:30 +0530 Subject: [PATCH] Allow timecode to be used as timestamp in Share Modal --- src/components/ShareModal.vue | 26 ++++++++++++++++++++++++-- src/locales/en.json | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/components/ShareModal.vue b/src/components/ShareModal.vue index 174299130b..024992e508 100644 --- a/src/components/ShareModal.vue +++ b/src/components/ShareModal.vue @@ -46,7 +46,7 @@ export default { }; }, mounted() { - this.timeStamp = parseInt(this.currentTime); + this.timeStamp = this.parseSecondsToTimeStamp(this.currentTime || 0); this.withTimeCode = this.getPreferenceBoolean("shareWithTimeCode", true); this.pipedLink = this.getPreferenceBoolean("shareAsPipedLink", true); }, @@ -69,6 +69,27 @@ export default { this.setPreference("shareWithTimeCode", this.withTimeCode); this.setPreference("shareAsPipedLink", this.pipedLink); }, + parseTimeStampToSeconds(timestamp) { + const timeArray = timestamp.split(":").reverse(); + let seconds = 0; + const durations = [1, 60, 60 * 60, 60 * 60 * 24]; + for (let i = 0; i < timeArray.length; i++) { + seconds += timeArray[i] * durations[i]; + } + return seconds; + }, + parseSecondsToTimeStamp(seconds) { + const timeArray = []; + const durations = [1, 60, 60 * 60, 60 * 60 * 24].reverse(); + for (let i in durations) { + const currentValue = Math.floor(seconds / durations[i]); + if (currentValue > 0) { + timeArray.push(currentValue.toString().padStart(2, "0")); + seconds -= currentValue * durations[i]; + } + } + return timeArray.join(":"); + }, }, computed: { generatedLink() { @@ -76,7 +97,8 @@ export default { ? window.location.origin + "/watch?v=" + this.videoId : "https://youtu.be/" + this.videoId; var url = new URL(baseUrl); - if (this.withTimeCode && this.timeStamp > 0) url.searchParams.append("t", this.timeStamp); + if (this.withTimeCode && this.timeStamp) + url.searchParams.append("t", this.parseTimeStampToSeconds(this.timeStamp)); return url.href; }, }, diff --git a/src/locales/en.json b/src/locales/en.json index 9cef968d97..a0152a20d5 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -108,7 +108,7 @@ "piped_link": "Piped link", "follow_link": "Follow link", "copy_link": "Copy link", - "time_code": "Time code (in seconds)", + "time_code": "Time code (in seconds or HH:MM:SS)", "show_chapters": "Chapters", "store_search_history": "Store Search history", "hide_watched": "Hide watched videos in the feed",