Skip to content

Commit

Permalink
feat: allow timecode to be used as timestamp in Share Modal
Browse files Browse the repository at this point in the history
  • Loading branch information
thecashewtrader authored and Bnyro committed Jan 9, 2025
1 parent 73ce4bf commit aaeddb3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/components/ShareModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit aaeddb3

Please sign in to comment.