feat: allow timecode to be used as timestamp in Share Modal

This commit is contained in:
thecashewtrader 2022-11-03 21:33:30 +05:30 committed by Bnyro
parent 73ce4bfb2e
commit aaeddb3903
2 changed files with 30 additions and 5 deletions

View File

@ -70,24 +70,29 @@ export default {
timeStamp: null, timeStamp: null,
hasPlaylist: false, hasPlaylist: false,
showQrCode: false, showQrCode: false,
durations: [1, 60, 60 * 60, 60 * 60 * 24],
}; };
}, },
computed: { computed: {
generatedLink() { generatedLink() {
var baseUrl = this.pipedLink const baseUrl = this.pipedLink
? window.location.origin + "/watch?v=" + this.videoId ? window.location.origin + "/watch?v=" + this.videoId
: "https://youtu.be/" + this.videoId; : "https://youtu.be/" + this.videoId;
var url = new URL(baseUrl); const 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));
if (this.hasPlaylist && this.withPlaylist) { if (this.hasPlaylist && this.withPlaylist) {
url.searchParams.append("list", this.playlistId); url.searchParams.append("list", this.playlistId);
url.searchParams.append("index", this.playlistIndex); url.searchParams.append("index", this.playlistIndex);
} }
return url.href; return url.href;
}, },
}, },
mounted() { mounted() {
this.timeStamp = parseInt(this.currentTime); this.timeStamp = this.parseSecondsToTimeStamp(this.currentTime ?? 0);
this.withTimeCode = this.getPreferenceBoolean("shareWithTimeCode", true); this.withTimeCode = this.getPreferenceBoolean("shareWithTimeCode", true);
this.pipedLink = this.getPreferenceBoolean("shareAsPipedLink", true); this.pipedLink = this.getPreferenceBoolean("shareAsPipedLink", true);
this.withPlaylist = this.getPreferenceBoolean("shareWithPlaylist", true); this.withPlaylist = this.getPreferenceBoolean("shareWithPlaylist", true);
@ -108,6 +113,26 @@ export default {
alert(this.$t("info.cannot_copy")); 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() { onChange() {
this.setPreference("shareWithTimeCode", this.withTimeCode, true); this.setPreference("shareWithTimeCode", this.withTimeCode, true);
this.setPreference("shareAsPipedLink", this.pipedLink, true); this.setPreference("shareAsPipedLink", this.pipedLink, true);

View File

@ -128,7 +128,7 @@
"piped_link": "Piped link", "piped_link": "Piped link",
"follow_link": "Follow link", "follow_link": "Follow link",
"copy_link": "Copy link", "copy_link": "Copy link",
"time_code": "Time code (in seconds)", "time_code": "Time code (in seconds or HH:MM:SS)",
"show_chapters": "Chapters", "show_chapters": "Chapters",
"store_search_history": "Store Search History", "store_search_history": "Store Search History",
"hide_watched": "Hide watched videos in the feed", "hide_watched": "Hide watched videos in the feed",