From aaeddb3903d476ee60e6f20d2c758f898f5675e3 Mon Sep 17 00:00:00 2001 From: thecashewtrader Date: Thu, 3 Nov 2022 21:33:30 +0530 Subject: [PATCH] feat: allow timecode to be used as timestamp in Share Modal --- src/components/ShareModal.vue | 33 +++++++++++++++++++++++++++++---- src/locales/en.json | 2 +- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/components/ShareModal.vue b/src/components/ShareModal.vue index b165c934..e8b8f412 100644 --- a/src/components/ShareModal.vue +++ b/src/components/ShareModal.vue @@ -70,24 +70,29 @@ export default { timeStamp: null, hasPlaylist: false, showQrCode: false, + durations: [1, 60, 60 * 60, 60 * 60 * 24], }; }, computed: { generatedLink() { - var baseUrl = this.pipedLink + const baseUrl = this.pipedLink ? 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); + const url = new URL(baseUrl); + + if (this.withTimeCode && this.timeStamp) + url.searchParams.append("t", this.parseTimeStampToSeconds(this.timeStamp)); + if (this.hasPlaylist && this.withPlaylist) { url.searchParams.append("list", this.playlistId); url.searchParams.append("index", this.playlistIndex); } + return url.href; }, }, 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); this.withPlaylist = this.getPreferenceBoolean("shareWithPlaylist", true); @@ -108,6 +113,26 @@ export default { alert(this.$t("info.cannot_copy")); } }, + parseTimeStampToSeconds(timestamp) { + const timeArray = timestamp.split(":").reverse(); + let seconds = 0; + for (let i = 0; i < timeArray.length; i++) { + seconds += timeArray[i] * this.durations[i]; + } + return seconds; + }, + parseSecondsToTimeStamp(seconds) { + const timeArray = []; + const durationsReversed = this.durations.toReversed(); + for (let i in durationsReversed) { + const currentValue = Math.floor(seconds / durationsReversed[i]); + if (currentValue > 0) { + timeArray.push(currentValue.toString().padStart(2, "0")); + seconds -= currentValue * durationsReversed[i]; + } + } + return timeArray.join(":"); + }, onChange() { this.setPreference("shareWithTimeCode", this.withTimeCode, true); this.setPreference("shareAsPipedLink", this.pipedLink, true); diff --git a/src/locales/en.json b/src/locales/en.json index 9c9d505a..3fa0fff6 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -128,7 +128,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",