Update Youtube URL handling to recongnize playlist ID in URL (#1260)

* * Update Youtube URL handling to recongnize playlist ID in URL

Only for format of `https://www.youtube.com/watch?v=vid&list=lid`

* ! Fix vue component prop declaration

A prop where the value can be undefined cannot be declared "required"
This commit is contained in:
PikachuEXE 2021-05-31 19:23:35 +08:00 committed by GitHub
parent a2df781f76
commit a3cf210fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 8 deletions

View File

@ -304,11 +304,18 @@ export default Vue.extend({
this.getYoutubeUrlInfo(href).then((result) => { this.getYoutubeUrlInfo(href).then((result) => {
switch (result.urlType) { switch (result.urlType) {
case 'video': { case 'video': {
const { videoId, timestamp } = result const { videoId, timestamp, playlistId } = result
const query = {}
if (timestamp) {
query.timestamp = timestamp
}
if (playlistId && playlistId.length > 0) {
query.playlistId = playlistId
}
this.$router.push({ this.$router.push({
path: `/watch/${videoId}`, path: `/watch/${videoId}`,
query: timestamp ? { timestamp } : {} query: query
}) })
break break
} }

View File

@ -21,7 +21,7 @@ export default Vue.extend({
}, },
playlistId: { playlistId: {
type: String, type: String,
required: true default: ''
}, },
getTimestamp: { getTimestamp: {
type: Function, type: Function,

View File

@ -109,11 +109,18 @@ export default Vue.extend({
this.getYoutubeUrlInfo(query).then((result) => { this.getYoutubeUrlInfo(query).then((result) => {
switch (result.urlType) { switch (result.urlType) {
case 'video': { case 'video': {
const { videoId, timestamp } = result const { videoId, timestamp, playlistId } = result
const query = {}
if (timestamp) {
query.timestamp = timestamp
}
if (playlistId && playlistId.length > 0) {
query.playlistId = playlistId
}
this.$router.push({ this.$router.push({
path: `/watch/${videoId}`, path: `/watch/${videoId}`,
query: timestamp ? { timestamp } : {} query: query
}) })
break break
} }

View File

@ -84,7 +84,7 @@ export default Vue.extend({
}, },
playlistId: { playlistId: {
type: String, type: String,
required: true default: ''
}, },
theatrePossible: { theatrePossible: {
type: Boolean, type: Boolean,

View File

@ -253,7 +253,7 @@ const actions = {
getVideoParamsFromUrl (_, url) { getVideoParamsFromUrl (_, url) {
/** @type {URL} */ /** @type {URL} */
let urlObject let urlObject
const paramsObject = { videoId: null, timestamp: null } const paramsObject = { videoId: null, timestamp: null, playlistId: null }
try { try {
urlObject = new URL(url) urlObject = new URL(url)
} catch (e) { } catch (e) {
@ -270,6 +270,7 @@ const actions = {
function() { function() {
if (urlObject.pathname === '/watch' && urlObject.searchParams.has('v')) { if (urlObject.pathname === '/watch' && urlObject.searchParams.has('v')) {
extractParams(urlObject.searchParams.get('v')) extractParams(urlObject.searchParams.get('v'))
paramsObject.playlistId = urlObject.searchParams.get('list')
return paramsObject return paramsObject
} }
}, },
@ -326,11 +327,12 @@ const actions = {
// //
// If `urlType` is "invalid_url" // If `urlType` is "invalid_url"
// Nothing else // Nothing else
const { videoId, timestamp } = actions.getVideoParamsFromUrl(null, urlStr) const { videoId, timestamp, playlistId } = actions.getVideoParamsFromUrl(null, urlStr)
if (videoId) { if (videoId) {
return { return {
urlType: 'video', urlType: 'video',
videoId, videoId,
playlistId,
timestamp timestamp
} }
} }