Skip to content

Commit

Permalink
Support opening video timestamps in a new window (#4687)
Browse files Browse the repository at this point in the history
* Support opening video timestamps in a new window

* Scroll to top when clicking on a timestamp
  • Loading branch information
absidue authored Feb 26, 2024
1 parent f04317b commit 259f7a6
Showing 1 changed file with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ export default defineComponent({
}
},
methods: {
catchTimestampClick: function(event) {
const match = event.detail.match(/(\d+):(\d+):?(\d+)?/)
if (match[3] !== undefined) { // HH:MM:SS
const seconds = 3600 * Number(match[1]) + 60 * Number(match[2]) + Number(match[3])
this.$emit('timestamp-event', seconds)
} else { // MM:SS
const seconds = 60 * Number(match[1]) + Number(match[2])
this.$emit('timestamp-event', seconds)
}
catchTimestampClick: function (event) {
this.$emit('timestamp-event', event.detail)
},
detectTimestamps: function (input) {
return input.replaceAll(/(\d+(:\d+)+)/g, '<a href="#" onclick="this.dispatchEvent(new CustomEvent(\'timestamp-clicked\',{bubbles:true, detail:\'$1\'}))">$1</a>')
const videoId = this.$route.params.id

return input.replaceAll(/(?:(\d+):)?(\d+):(\d+)/g, (timestamp, hours, minutes, seconds) => {
let time = 60 * Number(minutes) + Number(seconds)

if (hours) {
time += 3600 * Number(hours)
}

const url = this.$router.resolve({
path: `/watch/${videoId}`,
query: {
timestamp: time
}
}).href

// Adding the URL lets the user open the video in a new window at this timestamp
return `<a href="${url}" onclick="event.preventDefault();this.dispatchEvent(new CustomEvent('timestamp-clicked',{bubbles:true,detail:${time}}));window.scrollTo(0,0)">${timestamp}</a>`
})
}
}
})

0 comments on commit 259f7a6

Please sign in to comment.