direct url takes you to view page

This commit is contained in:
cool.gitter.choco
2025-02-05 10:57:11 -06:00
parent 01bdf22088
commit d59361c335

View File

@@ -26,15 +26,16 @@ async function performSearch() {
return; return;
} }
// If the query is a Spotify URL for a supported resource, redirect to our route.
if (isSpotifyUrl(query)) { if (isSpotifyUrl(query)) {
try { try {
const type = getResourceTypeFromUrl(query); const { type, id } = getSpotifyResourceDetails(query);
const supportedTypes = ['track', 'album', 'playlist', 'artist']; const supportedTypes = ['track', 'album', 'playlist', 'artist'];
if (!supportedTypes.includes(type)) throw new Error('Unsupported URL type'); if (!supportedTypes.includes(type))
throw new Error('Unsupported URL type');
const item = { name: `Direct URL (${type})`, external_urls: { spotify: query } }; // Redirect to {base_url}/{type}/{id}
startDownload(query, type, item, type === 'artist' ? 'album,single,compilation' : undefined); window.location.href = `${window.location.origin}/${type}/${id}`;
document.getElementById('searchInput').value = '';
return; return;
} catch (error) { } catch (error) {
showError(`Invalid Spotify URL: ${error.message}`); showError(`Invalid Spotify URL: ${error.message}`);
@@ -127,9 +128,21 @@ function isSpotifyUrl(url) {
return url.startsWith('https://open.spotify.com/'); return url.startsWith('https://open.spotify.com/');
} }
function getResourceTypeFromUrl(url) { /**
const pathParts = new URL(url).pathname.split('/'); * Extracts the resource type and ID from a Spotify URL.
return pathParts[1]; * Expected URL format: https://open.spotify.com/{type}/{id}
*/
function getSpotifyResourceDetails(url) {
const urlObj = new URL(url);
const pathParts = urlObj.pathname.split('/');
// Expecting ['', type, id, ...]
if (pathParts.length < 3 || !pathParts[1] || !pathParts[2]) {
throw new Error('Invalid Spotify URL');
}
return {
type: pathParts[1],
id: pathParts[2]
};
} }
function msToMinutesSeconds(ms) { function msToMinutesSeconds(ms) {