-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewPlaylistsWithSong.js
302 lines (267 loc) · 14.9 KB
/
ViewPlaylistsWithSong.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// NAME: ViewPlaylistsWithSong
// AUTHOR: elijaholmos
// DESCRIPTION: Adds context menu button to view playlists in your library that contain the selected song
// VERSION: 1.1.0
(async function ViewPlaylistsWithSong() {
if (!Spicetify.LocalStorage) {
setTimeout(ViewPlaylistsWithSong, 1000);
return;
}
// ------------ "constants" ------------
const SONG_PAGE_DISABLED = !window.initialState.isSongPageEnabled; //does the user have the song page enabled or disabled per their Spicetify settings
let READY_TO_USE = false; //is the extension ready to use yet (has setup complete)
const VERSION = '1.0.1';
console.log(`Running ViewPlaylistsWithSong v${VERSION}`); //log current version for debugging purposes
// ------------ API methods, based off code from @khanhas ------------
const fetchPlaylist = (uri) => new Promise((resolve, reject) => {
Spicetify.BridgeAPI.cosmosJSON(
{
method: "GET",
uri: `sp://core-playlist/v1/playlist/${uri}`,
body: {
policy: {
link: true
}
}
},
(error, res) => {
if (error) {
reject(error);
return;
}
resolve({
name: res.playlist.name,
description: res.playlist.description,
uri: res.playlist.link,
image: res.playlist.picture,
tracks: res.items //simplify this to only return what is necessary?
})
}
);
});
const fetchFolder = () => new Promise((resolve, reject) => {
Spicetify.BridgeAPI.cosmosJSON(
{
method: "GET",
uri: `sp://core-playlist/v1/rootlist`,
body: {
policy: {
folder: {
rows: true,
link: true
}
}
}
},
(error, res) => {
if (error) {
reject(error);
return;
}
resolve(res);
}
);
});
// ------------ Custom methods ------------
/**
* get and store all the self-created, public playlists in the current user's library
* @param {boolean} [setup] whether or not to display "progressbar" with current retrieval status
* @returns {Array} array of custom playlist objects, see fetchPlaylist()
*/
const getUserPlaylists = async function (setup = false) {
const playlists = [];
const library = await fetchFolder();
let counter = 1; //for "progress bar" notification
for (const item of library.rows) {
//if the current item in their library is a folder, we need to read all the playlists and any sub-folders inside it
item.type == "folder" ?
playlists.push(...(await recursivelyExtractPlaylistsFromFolder(item))) :
item.ownedBySelf && item.totalLength > 0 && playlists.push(await fetchPlaylist(item.link));
setup && Spicetify.showNotification(`ViewPlaylistsWithSong: Setup ${Math.round(((counter++) / library.rows.length) * 100)}% Complete`);
} //TODO: add error catching?
READY_TO_USE = true;
setup && Spicetify.showNotification(`ViewPlaylistsWithSong: Setup 100% Complete`); //just in case counter gets messed up, display 100% notif to user
return playlists;
};
//gets user's playlists, updates global variable, then calls itself after the specified delay
const getUserPlaylistsLoop = async function () {
USER_PLAYLISTS = await getUserPlaylists();
setTimeout(getUserPlaylistsLoop, 5000);
return;
};
//this needs to be recursive in case there are subfolders
const recursivelyExtractPlaylistsFromFolder = async function (folder) {
const playlists = [];
for(const item of folder.rows) {
item.type == "folder" ?
playlists.push(...(await recursivelyExtractPlaylistsFromFolder(item))) :
item.ownedBySelf && item.totalLength > 0 && playlists.push(await fetchPlaylist(item.link));
}
return playlists;
};
/**
* Search through the stored playlist library for the specified song
* @param {string} uri URI of the song to search for
* @returns {Array} Array of playlists that contain the specified song
*/
const getPlaylistsWithSong = function (uri) {
const playlists = [];
for(const playlist of USER_PLAYLISTS)
playlist.tracks.some(track => track.link === uri) && playlists.push(playlist);
return playlists;
};
// ------------ HTML Methods ------------
/**
* pass in a playlist object and receive an HTML string ready to be appended to the SongPage grid
* @param {Object} playlist a custom playlist object, see fetchPlaylist()
* @returns {String} HTML string ready to be appended to the DOM
*/
const generatePlaylistCard = function (playlist = {}) {
return `<div class="col-md-3">
<div class="card card-type-playlist"
data-interaction-context="card"
data-log-data="{ "name": "${playlist.name}", "target_uri": "${playlist.uri}" }"
data-uri="${playlist.uri}" data-contextmenu="" data-context=""
data-card-pressed-container="" data-ta-id="card">
<div class="card-attention-highlight-box">
</div>
<div class="card-image-wrapper" data-contextmenu-spy="" data-ta-id="card-image-wrapper">
<div class="card-image-hit-area" data-card-trigger-pressed-state="">
<a href="${playlist.uri}" data-uri="${playlist.uri}"
draggable="true" data-drag-text="${playlist.name}" class="card-image-link "
data-interaction-target="image" aria-hidden="true" tabindex="-1">
<div class="card-hit-area-counter-scale"></div>
<div class="card-placeholder-wrapper">
<svg class="card-placeholder" viewBox="0 0 10 10" preserveAspectRatio="xMidYMid meet">
<text class="playlist-placeholder" x="5" y="9.8"></text>
<text class="playlist-folder-placeholder" x="5" y="9.8"></text>
<text class="album-placeholder" x="5" y="9.8"></text>
<text class="track-placeholder" x="5" y="9.8"></text>
<text class="artist-placeholder" x="5" y="9.8"></text>
<text class="user-placeholder" x="5" y="9.8"></text>
<text class="genre-placeholder" x="5" y="9.8"></text>
<text class="collection-placeholder" x="5" y="9.8"></text>
<text class="local-files-placeholder" x="5" y="9.8"></text>
<text class="episode-placeholder" x="5" y="9.8"></text>
</svg>
</div>
<div class="card-image-content-wrapper">
<div class="card-image"
style="background-image: url('${playlist.image}')"
data-ta-id="card-image"
data-image-url="${playlist.image}">
</div>
</div>
<div class="card-overlay"></div>
</a>
<button type="button" data-button="add" class="button button-add card-button-add"
data-ta-id="card-button-add" data-tooltip="Save to Your Library"
data-tooltip-add="Save to Your Library" data-tooltip-remove="Remove from Your Library"
data-interaction-target="toggle-follow-state-button" data-interaction-intent="follow"></button>
<button type="button" class="button button-play button-icon-with-stroke card-button-play"
data-ta-id="card-button-play" data-interaction-target="play-pause-button"
data-interaction-intent="play" data-button="play-context"></button>
<button type="button" data-button="contextmenu"
class="button button-more button-no-border card-button-more"
data-ta-id="card-button-context-menu" data-tooltip="More"
data-interaction-target="context-menu-button" data-interaction-intent="show-options"></button>
</div>
</div>
<div class="card-info-wrapper card-info-with-subtitle-links card-info-with-metadata">
<div class="card-info-title">
<a href="${playlist.uri}" data-uri="${playlist.uri}"
data-ta-id="card-title-link" data-tooltip="${playlist.name}"
data-interaction-target="title" dir="auto">
${playlist.name}
</a>
</div>
<div class="card-info-subtitle-links" data-interaction-context="subtitle-links">
<a dir="auto" title="" href="" data-uri="" data-ta-id="card-subtitle-link"
data-interaction-target="0"></a>
</div>
<div class="card-info-subtitle-description" data-ta-id="card-description">
<span dir="auto">${playlist.description}</span>
</div>
</div>
</div>
</div>`;
};
//recursively hide the carousel element that appears by default on the SongPage
//counter is to prevent infinite loops
const hideCarousel = function (counter = 0) {
return new Promise((resolve, reject) => {
if(counter > 10) reject('hideCarousel timed out');
const carousel = document.querySelector('.SongPage .Carousel');
!!carousel ? carousel.style.display = 'none' : setTimeout(function() { return hideCarousel(++counter); }, 500);
resolve();
});
};
//render the given array of playlists on the SongPage
const renderPlaylists = function ({playlists = [], song = ''} = {}) {
document.querySelector('.SongPage').setAttribute('current-song', song); //update the current-song to prevent UI errors
//isolate and modify the wrapper element
const wrapper = document.querySelector('.SongPage .glue-page-wrapper') || document.querySelector('.SongPage .profile-section');
wrapper.className = 'profile-section container';
wrapper.setAttribute('data-navbar-view-id', 'public-playlists');
//create the h2 header
const list_header = document.createElement('div');
list_header.className = "GlueSectionDivider";
list_header.innerHTML = `<div class="GlueSectionDivider__container">
<div class="GlueSectionDivider__title-container">
<h2 class="GlueSectionDivider__title">Appears in <a>${playlists.length}</a> of your playlists</h2>
</div>
</div>`;
wrapper.append(list_header);
//create the grid of playlists
const row_wrapper = document.createElement('div');
row_wrapper.className = 'row standard-grid no-section-divider';
for (const playlist of playlists)
row_wrapper.innerHTML += generatePlaylistCard(playlist);
hideCarousel();
wrapper.append(row_wrapper);
};
/**
* Change the enabled state of the SongPage to the value specified. If no value is provided, the current isSongPageEnabled value will be toggled.
* @param {Boolean} [value] true to enable SongPage, false to disable it
*/
const toggleSongPage = function (value = !window.initialState.isSongPageEnabled) {
window.initialState = { ...window.initialState, isSongPageEnabled: value };
};
// ------------ Other code ------------
//function to be performed when ContextMenu button is clicked
const btnClick = async function (uris) {
if(uris.length != 1) throw new Error();
const uri = uris[0]; //isolate the only uri passed to the function
//prepare the data to be sent in the postMessage() function
const data = { // I reversed engineered this by watching JS events in the console. I don't entirely know what all of the properties mean
type: "navigation_request_state",
method: "open",
state: JSON.stringify({uri:uri}),
extra: "{}"
};
//enable the song page, otherwise the postMessage() requests will not work with track URIs
SONG_PAGE_DISABLED && toggleSongPage(true);
//send the message to update the GUI
window.postMessage(data, window);
//prevent appending the same HTML multiple times for one song if user repeatedly clicks button
if (document.querySelector('.SongPage')?.getAttribute('current-song') === uri) return;
//retrieve the user's playlists that contain the selected song
const matched_playlists = getPlaylistsWithSong(uri);
//code to execute after postMessage() has finished updating the UI
setTimeout(function() {
renderPlaylists({song:uri, playlists:matched_playlists}); //render the playlists in the UI
SONG_PAGE_DISABLED && toggleSongPage(false); //we need to disable the SongPage since we overrode its state earlier
}, 250);
return;
};
//add context menu item
new Spicetify.ContextMenu.Item("View Playlists with Song", btnClick, (uris) => {
if(uris.length != 1) return false; //only one song at a time
return READY_TO_USE && Spicetify.URI.fromString(uris[0]).type == Spicetify.URI.Type.TRACK; //uri must be track type
}, "search").register();
//this goes at the bottom because await
var USER_PLAYLISTS = await getUserPlaylists(true);
//in case the user adds/removes a playlist or changes the song composition,
//this will ensure the USER_PLAYLISTS variable is (nearly) always up to date
getUserPlaylistsLoop();
})();