|
|
@@ -3,30 +3,45 @@
|
|
|
|
|
|
const PlaylistUtils = {
|
|
|
/**
|
|
|
- * Extract video ID from YouTube URL
|
|
|
+ * Extract video ID from YouTube URL. Handles both standard watch URLs
|
|
|
+ * (`/watch?v=<id>`) and Shorts URLs (`/shorts/<id>`, which carry the ID
|
|
|
+ * in the path rather than a `v` query param).
|
|
|
* @param {string} url - YouTube URL
|
|
|
* @returns {string|null} - Video ID or null if not found
|
|
|
*/
|
|
|
extractVideoId(url) {
|
|
|
try {
|
|
|
const urlObj = new URL(url);
|
|
|
- return urlObj.searchParams.get("v");
|
|
|
+ const vParam = urlObj.searchParams.get("v");
|
|
|
+ if (vParam) {
|
|
|
+ return vParam;
|
|
|
+ }
|
|
|
+ const shortsMatch = urlObj.pathname.match(/^\/shorts\/([^/?]+)/);
|
|
|
+ return shortsMatch ? shortsMatch[1] : null;
|
|
|
} catch (e) {
|
|
|
return null;
|
|
|
}
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
- * Normalize a YouTube watch URL down to just the `v` (video ID) param,
|
|
|
- * stripping queue/playlist params like `list`, `index`, and `pp` so that
|
|
|
+ * Normalize a YouTube video URL down to just its video ID, stripping
|
|
|
+ * queue/playlist params like `list`, `index`, and `pp` so that
|
|
|
* navigating to the stored URL later doesn't drop the user back into
|
|
|
- * whatever queue or playlist they originally watched it from.
|
|
|
+ * whatever queue or playlist they originally watched it from. Shorts
|
|
|
+ * URLs keep the `/shorts/<id>` form rather than being rewritten to
|
|
|
+ * `/watch?v=<id>`.
|
|
|
* @param {string} url - YouTube URL
|
|
|
- * @returns {string} - Clean watch URL, or the original url if no video ID is found
|
|
|
+ * @returns {string} - Clean URL, or the original url if no video ID is found
|
|
|
*/
|
|
|
cleanVideoUrl(url) {
|
|
|
const videoId = this.extractVideoId(url);
|
|
|
- return videoId ? `https://www.youtube.com/watch?v=${videoId}` : url;
|
|
|
+ if (!videoId) {
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+ const isShorts = new URL(url).pathname.startsWith("/shorts/");
|
|
|
+ return isShorts
|
|
|
+ ? `https://www.youtube.com/shorts/${videoId}`
|
|
|
+ : `https://www.youtube.com/watch?v=${videoId}`;
|
|
|
},
|
|
|
|
|
|
/**
|