-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
97 lines (85 loc) · 3.06 KB
/
script.js
File metadata and controls
97 lines (85 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const albumArt = document.getElementById('album-art');
const songTitle = document.querySelector('.now-playing h2');
const artistName = document.querySelector('.now-playing h3');
const prevButton = document.getElementById('prev-button');
const skipButton = document.getElementById('skip-button');
const playButton = document.getElementById('play-button');
let acs_code;
document.getElementById('submit-code').addEventListener('click', function() {
acs_code = document.getElementById('access-code').value;
console.log(acs_code); // For debugging purposes, you can see the value in the console
// Fetch access token and saved songs once the access code is submitted
getAccessToken();
getSavedSongs();
});
let accessToken = ''; // Initialize access token as an empty string
let currentTrackIndex = 0;
let savedTracks = []; // Stores saved songs data
// Function to fetch access token (this function assumes acs_code is already set)
function getAccessToken() {
// Use the access code directly as the access token for this example
accessToken = acs_code;
console.log(`Access token set: ${accessToken}`);
}
// Function to fetch saved songs
function getSavedSongs() {
if (!accessToken) {
console.error('Access token is not set');
return;
}
fetch('https://api.spotify.com/v1/me/tracks?limit=50', {
headers: {
Authorization: `Bearer ${accessToken}`
}
})
.then(response => response.json())
.then(data => {
savedTracks = data.items.map(item => ({
title: item.track.name,
artist: item.track.artists[0].name,
image: item.track.album.images[0].url // Get first image from album
}));
updateDisplay();
})
.catch(error => console.error(error));
}
// Function to update displayed song information
function updateDisplay() {
if (savedTracks.length === 0) {
songTitle.textContent = "No Saved Songs";
artistName.textContent = "";
albumArt.src = "";
return;
}
const currentTrack = savedTracks[currentTrackIndex];
songTitle.textContent = currentTrack.title;
artistName.textContent = currentTrack.artist;
albumArt.src = currentTrack.image;
}
// Function to handle previous button click (currently non-functional for playback)
prevButton.addEventListener('click', () => {
currentTrackIndex--;
if (currentTrackIndex < 0) {
currentTrackIndex = savedTracks.length - 1;
}
updateDisplay();
});
// Function to handle skip button click (currently non-functional for playback)
skipButton.addEventListener('click', () => {
currentTrackIndex++;
if (currentTrackIndex >= savedTracks.length) {
currentTrackIndex = 0;
}
updateDisplay();
});
// Function to handle play button click to open song on YouTube
playButton.addEventListener('click', () => {
if (savedTracks.length === 0) {
console.error('No tracks available to play');
return;
}
const currentTrack = savedTracks[currentTrackIndex];
const searchQuery = `${currentTrack.title} ${currentTrack.artist}`;
const youtubeUrl = `https://www.youtube.com/results?search_query=${encodeURIComponent(searchQuery)}`;
window.open(youtubeUrl, '_blank');
});