-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.tsx
325 lines (304 loc) · 9.35 KB
/
app.tsx
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// NAME: Copy Text
// AUTHOR: pnthach95, Tetrax-10
// DESCRIPTION: Adds Copy text to context menu for Spotify v1.1.59 and Spicetify v2.0.0 and above
import {SettingsSection} from 'spcr-settings';
const SETTINGS = {
ID: 'settings-copy-to-clipboard',
NAME: 'Copy to clipboard settings',
SEPARATOR: {
KEY: 'ctc-separator',
DESCRIPTION: 'Separator between Song name and Artist names',
DEFAULT: '; ',
},
};
const localizations: Record<string, Localization> = {
ru: {
error: 'Ошибка',
text: 'Скопировать текст',
songAndArtist: 'Cкопировать трек и артиста',
copied: 'Скопировано',
settings: {
name: 'Copy to clipboard settings',
separator: 'Separator between Song name and Artist names',
},
},
en: {
error: 'Error',
text: 'Copy Text',
songAndArtist: 'Copy Song & Artist names',
copied: 'Copied',
settings: {
name: SETTINGS.NAME,
separator: SETTINGS.SEPARATOR.DESCRIPTION,
},
},
vi: {
copied: 'Đã sao chép',
error: 'Lỗi',
settings: {
name: 'Cài đặt Copy to clipboard',
separator: 'Phân cách giữa tên bài hát và tên nghệ sĩ',
},
songAndArtist: 'Sao chép tên bài hát & nghệ sĩ',
text: 'Sao chép tên',
},
};
async function getLocalization() {
await new Promise(resolve => setTimeout(resolve, 1000));
const locale = Spicetify.Locale ? Spicetify.Locale.getLocale() : 'en';
// console.log('Spicetify.Locale._locale', Spicetify.Locale._locale);
// console.log('Spicetify.Locale.getLocale', Spicetify.Locale.getLocale());
return Object.keys(localizations).includes(locale)
? localizations[locale as keyof typeof localizations]
: localizations['en'];
}
async function fetchAlbum(id?: string) {
try {
const albumInfo: AlbumInfo = await Spicetify.CosmosAsync.get(
`https://api.spotify.com/v1/albums/${id}`,
);
return albumInfo.name;
} catch (e) {
console.log(e);
throw new Error((e as Error).message);
}
}
async function fetchArtist(uri: string) {
const {queryArtistOverview} = Spicetify.GraphQL.Definitions;
try {
const locale = Spicetify.Locale ? Spicetify.Locale.getLocale() : 'en';
const {data} = await Spicetify.GraphQL.Request(queryArtistOverview, {
uri,
includePrerelease: false,
locale,
offset: 0,
limit: 10,
});
return data.artistUnion.profile.name as string;
} catch (e) {
console.log(e);
throw new Error((e as Error).message);
}
}
async function fetchArtists(uri: string) {
const {queryTrackArtists} = Spicetify.GraphQL.Definitions;
try {
const {data} = (await Spicetify.GraphQL.Request(queryTrackArtists, {
uri,
offset: 0,
limit: 10,
})) as {data: QueryTrackArtistsData};
return data.trackUnion.artists.items.map(i => i.profile.name).join(', ');
} catch (e) {
console.log(e);
throw new Error((e as Error).message);
}
}
async function fetchTrackName(uri: string) {
const {getTrackName} = Spicetify.GraphQL.Definitions;
try {
const {data} = (await Spicetify.GraphQL.Request(getTrackName, {
uri,
offset: 0,
limit: 10,
})) as {data: GetTrackNameData};
return data.trackUnion.name;
} catch (e) {
console.log(e);
throw new Error((e as Error).message);
}
}
function initCopyText(localization: Localization) {
const getText: Spicetify.ContextMenu.OnClickCallback = async uris => {
const {Type} = Spicetify.URI;
const uri = Spicetify.URI.fromString(uris[0]);
// @ts-ignore _base62Id may be existed on old versions
const id: string = uri._base62Id ? uri._base62Id : uri.id;
try {
switch (uri.type) {
case Type.TRACK:
sendToClipboard(await fetchTrackName(uri.toURI()));
break;
case Type.LOCAL:
const tmp: string[] = [];
if (uri.track) {
tmp.push(uri.track);
}
if (uri.artist) {
tmp.push(uri.artist);
}
if (uri.album) {
tmp.push(uri.album);
}
sendToClipboard(tmp.join('; '));
break;
case Type.LOCAL_ARTIST:
sendToClipboard(`${uri.artist ? uri.artist : ''}`);
break;
case Type.LOCAL_ALBUM:
sendToClipboard(`${uri.album ? uri.album : ''}`);
break;
case Type.ALBUM:
sendToClipboard(await fetchAlbum(uri.id));
break;
case Type.ARTIST:
sendToClipboard(await fetchArtist(uri.toURI()));
break;
case Type.PLAYLIST:
case Type.PLAYLIST_V2:
sendToClipboard(
(
await Spicetify.CosmosAsync.get(
`sp://core-playlist/v1/playlist/spotify:playlist:${id}`,
)
).playlist.name,
);
break;
case Type.SHOW:
sendToClipboard(
(
await Spicetify.CosmosAsync.get(
`sp://core-show/v1/shows/${id}?responseFormat=protobufJson`,
)
).header.showMetadata.name,
);
break;
case Type.EPISODE:
sendToClipboard(
(
await Spicetify.Platform.ShowAPI.getEpisodeOrChapter(
`spotify:episode:${id}`,
)
).name,
);
break;
case Type.PROFILE:
sendToClipboard(
(
await Spicetify.CosmosAsync.get('sp://core-profile/v1/profiles', {
usernames: uri.username,
})
).profiles[0].name,
);
break;
case Type.FOLDER:
let rootlist: RootlistContent =
await Spicetify.Platform.RootlistAPI.getContents();
let folder = rootlist.items.filter(
item => item.type === 'folder' && item.uri.includes(id),
);
sendToClipboard(folder[0].name);
break;
default:
break;
}
} catch (error) {
Spicetify.showNotification(
`${localization.error}: ${(error as Error).message}`,
);
}
};
const getSongArtistText: Spicetify.ContextMenu.OnClickCallback =
async uris => {
const {Type} = Spicetify.URI;
const uri = Spicetify.URI.fromString(uris[0]);
try {
switch (uri.type) {
case Type.TRACK:
const [name, artists] = await Promise.allSettled([
fetchTrackName(uri.toURI()),
fetchArtists(uri.toURI()),
]);
if (name.status === 'fulfilled' && artists.status === 'fulfilled') {
const settings = new SettingsSection(SETTINGS.NAME, SETTINGS.ID);
sendToClipboard(
name.value +
settings.getFieldValue(SETTINGS.SEPARATOR.KEY) +
artists.value,
);
}
break;
default:
break;
}
} catch (error) {
Spicetify.showNotification(
`${localization.error}: ${(error as Error).message}`,
);
}
};
function sendToClipboard(text: string | null) {
if (text) {
Spicetify.showNotification(`${localization.copied}: ${text}`);
Spicetify.Platform.ClipboardAPI.copy(text);
}
}
const shouldAddContextMenu: Spicetify.ContextMenu.ShouldAddCallback =
uris => {
if (uris.length === 1) {
const {Type} = Spicetify.URI;
const uri = Spicetify.URI.fromString(uris[0]);
switch (uri.type) {
case Type.TRACK:
case Type.LOCAL:
case Type.LOCAL_ARTIST:
case Type.LOCAL_ALBUM:
case Type.ALBUM:
case Type.ARTIST:
case Type.PLAYLIST:
case Type.PLAYLIST_V2:
case Type.SHOW:
case Type.EPISODE:
case Type.PROFILE:
case Type.FOLDER:
return true;
default:
return false;
}
} else {
return false;
}
};
const shouldAddCSAContextMenu: Spicetify.ContextMenu.ShouldAddCallback =
uris => {
if (uris.length === 1) {
const {Type} = Spicetify.URI;
const uri = Spicetify.URI.fromString(uris[0]);
switch (uri.type) {
case Type.TRACK:
return true;
default:
return false;
}
} else {
return false;
}
};
new Spicetify.ContextMenu.Item(
localization.text,
getText,
shouldAddContextMenu,
'copy',
).register();
new Spicetify.ContextMenu.Item(
localization.songAndArtist,
getSongArtistText,
shouldAddCSAContextMenu,
'artist',
).register();
}
async function main() {
while (!Spicetify || document.readyState !== 'complete') {
await new Promise(resolve => setTimeout(resolve, 1000));
}
const localization = await getLocalization();
const settings = new SettingsSection(localization.settings.name, SETTINGS.ID);
settings.addInput(
SETTINGS.SEPARATOR.KEY,
localization.settings.separator,
SETTINGS.SEPARATOR.DEFAULT,
);
settings.pushSettings();
initCopyText(localization);
}
export default main;