-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
218 lines (205 loc) · 7.94 KB
/
popup.js
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const CLIENT_ID = '5d9bc4f5c43c41548b24eb1161ebb589';
const CLIENT_SECRET = 'b1c310e06b124c44b56141a9d5a24d39';
let ACCESS_TOKEN = '';
$(document).ready(async function () {
ACCESS_TOKEN = await getAccessToken();
currentlyPlaying();
setInterval(currentlyPlaying, 3000);
getLatest();
});
function getLatest() {
$.getJSON('https://tananana.ro/live/meta.php', async function (data) {
$('div#main').empty();
if (isEmpty(data)) {
$('div#main').addClass('errorBody');
const errorMessage = createErrorMessage();
$('div#main').append(errorMessage);
return;
}
const [currentTrack, currentArtist] = await Promise.all([searchTrack(data.current), searchArtist(data.current)]);
const current = createTrackElement(1, data.current, currentTrack, currentArtist, true);
$('div#main').append(current);
let latestTracks = [];
let latestArtists = [];
data.latest.forEach(e => {
latestTracks.push(searchTrack(e));
latestArtists.push(searchArtist(e));
});
let tracksPromise = Promise.all(latestTracks);
let artistsPromise = Promise.all(latestArtists);
const [tracks, artists] = await Promise.all([tracksPromise, artistsPromise]);
for (let i = 0; i < data.latest.length; ++i) {
const elem = createTrackElement(i + 2, data.latest[i], tracks[i], artists[i], false);
$('div#main').append(elem);
}
});
}
function createTrackElement(index, track, spotifyTrack, spotifyArtist, isCurrent) {
const card = document.createElement('div');
card.className = 'card';
const digit = document.createElement('div');
digit.className = 'digit';
const digitText = document.createTextNode(index);
digit.appendChild(digitText);
card.appendChild(digit);
const body = document.createElement('div');
body.className = 'body';
const title = document.createElement('div');
title.className = 'title';
const spotifyAnchor = document.createElement('a');
const trackURL = getSpotifyURL(spotifyTrack);
if (trackURL) {
spotifyAnchor.setAttribute('href', trackURL);
spotifyAnchor.setAttribute('target', '_blank');
} else {
spotifyAnchor.style.textDecoration = "none";
}
const titleText = document.createTextNode(track.split(' - ')[1]);
spotifyAnchor.appendChild(titleText);
title.appendChild(spotifyAnchor);
if (isCurrent) {
digit.id = 'first';
const tanananaAnchor = document.createElement('a');
tanananaAnchor.setAttribute('href', "http://tananana.ro");
tanananaAnchor.setAttribute('title', "go to TANANANA website");
tanananaAnchor.setAttribute('target', '_blank');
const currentlyPlayingSpan = document.createElement('span');
currentlyPlayingSpan.id = 'currently-playing';
const volumeIcon = document.createElement('i');
volumeIcon.classList.add('fa');
volumeIcon.classList.add('fa-volume-up');
volumeIcon.setAttribute('aria-hidden', 'true');
currentlyPlayingSpan.appendChild(volumeIcon);
tanananaAnchor.append(currentlyPlayingSpan);
title.appendChild(tanananaAnchor);
}
body.appendChild(title);
const artist = document.createElement('div');
artist.className = 'artist';
const spotifyArtistAnchor = document.createElement('a');
const artistURL = getArtistURL(spotifyTrack, spotifyArtist);
if (artistURL) {
spotifyArtistAnchor.setAttribute('href', artistURL);
spotifyArtistAnchor.setAttribute('target', '_blank');
} else {
spotifyArtistAnchor.style.textDecoration = "none";
}
const artistText = document.createTextNode(track.split(' - ')[0]);
spotifyArtistAnchor.appendChild(artistText);
artist.appendChild(spotifyArtistAnchor);
body.appendChild(artist);
card.appendChild(body);
return card;
}
function getSpotifyURL(track) {
if (checkDefinedProperty(track, "tracks")) {
return track.tracks?.items[0]?.external_urls.spotify ? track.tracks.items[0]?.external_urls.spotify : '';
}
}
function getArtistURL(track, artist) {
if (checkDefinedProperty(track, "tracks") && checkDefinedProperty(artist, "artists")) {
return track.tracks.items[0]?.artists[0]?.external_urls.spotify ? track.tracks.items[0]?.artists[0]?.external_urls.spotify :
artist.artists.items[0]?.external_urls.spotify ? artist.artists.items[0]?.external_urls.spotify : '';
}
}
function sanitizeTitle(title) {
if (title === null || title === undefined || title === '') return;
// can be improved for 're vs are etc
return title
.replace(/F(ea)?T.*/gmi, "")
.replace(/\(.*\)/gmi, "")
.split(' - ')
.join(' ');
}
function searchTrack(title) {
const encodedTitle = encodeURIComponent(sanitizeTitle((title)));
if (encodedTitle === "") return;
return $.ajax({
url: "https://api.spotify.com/v1/search?q=" + encodedTitle + "&type=track",
type: "GET",
contentType: "application/x-www-form-urlencoded",
headers: {
"Authorization": "Bearer " + ACCESS_TOKEN.access_token
},
data: {
"grant_type": "client_credentials",
}
});
}
function searchArtist(title) {
const artist = title.split(' - ')[0];
if (artist === "") return;
return $.ajax({
url: "https://api.spotify.com/v1/search?q=" + artist + "&type=artist",
type: "GET",
contentType: "application/x-www-form-urlencoded",
headers: {
"Authorization": "Bearer " + ACCESS_TOKEN.access_token
},
data: {
"grant_type": "client_credentials",
}
});
}
function currentlyPlaying() {
const a = $('span#currently-playing');
const volumeIconDown = document.createElement('i');
volumeIconDown.classList.add('fa');
volumeIconDown.classList.add('fa-volume-down');
volumeIconDown.setAttribute('aria-hidden', "true");
a.html(volumeIconDown);
setTimeout(function () {
const volumeIconUp = document.createElement('i');
volumeIconUp.classList.add('fa');
volumeIconUp.classList.add('fa-volume-up');
volumeIconUp.setAttribute('aria-hidden', "true");
a.html(volumeIconUp);
}, 1500);
}
function getAccessToken() {
return $.ajax({
url: "https://accounts.spotify.com/api/token",
type: "POST",
contentType: "application/x-www-form-urlencoded",
headers: {
"Authorization": "Basic " + btoa(CLIENT_ID + ":" + CLIENT_SECRET)
},
data: {
"grant_type": "client_credentials",
}
});
}
function checkDefinedProperty(obj, property) {
return obj !== null && obj !== undefined && obj[property] !== null;
}
function isEmpty(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object
}
function createErrorMessage() {
const error = document.createElement('div');
// header
const errorHeader = document.createElement('div');
const errorHeaderText = document.createTextNode('Oops! No data available.');
errorHeader.appendChild(errorHeaderText);
//header end
// body
const errorText = document.createElement('div');
const errorTextContent = document.createTextNode('Go check ');
// anchor
const tanananaAnchor = document.createElement('a');
const tanananaSpan = document.createElement('span');
tanananaSpan.id = "errorSpan";
const tanananaText = document.createTextNode('TANANANA.ro');
tanananaSpan.appendChild(tanananaText);
tanananaAnchor.setAttribute('href', "https://tananana.ro");
tanananaAnchor.setAttribute('title', "TANANANA website");
tanananaAnchor.setAttribute('target', '_blank');
tanananaAnchor.appendChild(tanananaSpan);
// anchor end
errorText.append(errorTextContent);
errorText.appendChild(tanananaAnchor);
// body end
error.appendChild(errorHeader);
error.appendChild(errorText);
return error;
}