Allow pasting Playlist URLs in search to navigate straight to playlist page

This commit is contained in:
Preston 2021-01-15 11:34:44 -05:00
parent d5304cfcdd
commit 890d5b4a09
2 changed files with 46 additions and 18 deletions

View File

@ -77,7 +77,7 @@ export default Vue.extend({
this.debounceSearchResults = debounce(this.getSearchSuggestions, 200)
},
methods: {
goToSearch: function (query) {
goToSearch: async function (query) {
const appWidth = $(window).width()
if (appWidth <= 680) {
@ -89,10 +89,18 @@ export default Vue.extend({
searchInput.blur()
}
this.$store.dispatch('getVideoIdFromUrl', query).then((result) => {
if (result) {
const videoId = await this.$store.dispatch('getVideoIdFromUrl', query)
const playlistId = await this.$store.dispatch('getPlaylistIdFromUrl', query)
console.log(playlistId)
if (videoId) {
this.$router.push({
path: `/watch/${result}`
path: `/watch/${videoId}`
})
} else if (playlistId) {
this.$router.push({
path: `/playlist/${playlistId}`
})
} else {
router.push({
@ -105,7 +113,6 @@ export default Vue.extend({
}
})
}
})
this.showFilters = false
},

View File

@ -229,6 +229,27 @@ const actions = {
return extractors.reduce((a, c) => a || c(), null) || false
},
getPlaylistIdFromUrl (_, url) {
/** @type {URL} */
let urlObject
try {
urlObject = new URL(url)
} catch (e) {
return false
}
const extractors = [
// anything with /playlist?list=
function() {
if (urlObject.pathname === '/playlist' && urlObject.searchParams.has('list')) {
return urlObject.searchParams.get('list')
}
}
]
return extractors.reduce((a, c) => a || c(), null) || false
},
padNumberWithLeadingZeros(_, payload) {
let numberString = payload.number.toString()
while (numberString.length < payload.length) {