-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProSpotify.js
More file actions
1395 lines (1277 loc) · 54.9 KB
/
Copy pathProSpotify.js
File metadata and controls
1395 lines (1277 loc) · 54.9 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function ProSpotify() {
if (!(Spicetify.Player.data && Spicetify.Platform)) {
setTimeout(ProSpotify, 100);
return;
}
console.log("ProSpotify is running");
function getAlbumInfo(uri) {
return Spicetify.CosmosAsync.get(
`https://api.spotify.com/v1/albums/${uri}`
);
}
function valueSet() {
const blurValue = Number.parseInt(localStorage.getItem("blurAmount"));
const contValue = Number.parseInt(localStorage.getItem("contAmount"));
const satuValue = Number.parseInt(localStorage.getItem("satuAmount"));
const brightValue = Number.parseInt(localStorage.getItem("brightAmount"));
if (!Number.isNaN(blurValue)) {
document.documentElement.style.setProperty("--blur", `${blurValue}px`);
} else {
document.documentElement.style.setProperty("--blur", "15px");
}
if (!Number.isNaN(contValue)) {
document.documentElement.style.setProperty("--cont", `${contValue}%`);
} else {
document.documentElement.style.setProperty("--cont", "50%");
}
if (!Number.isNaN(satuValue)) {
document.documentElement.style.setProperty("--satu", `${satuValue}%`);
} else {
document.documentElement.style.setProperty("--satu", "70%");
}
if (!Number.isNaN(brightValue)) {
document.documentElement.style.setProperty("--bright", `${brightValue}%`);
} else {
document.documentElement.style.setProperty("--bright", "120%");
}
}
valueSet();
async function fetchFadeTime() {
const crossfadeEnabled = false;
let FadeTime = "0.4s";
if (crossfadeEnabled) {
const fadeTime = FadeTime;
const dividedTime = fadeTime / 1000;
FadeTime = `${dividedTime}s`;
}
document.documentElement.style.setProperty("--fade-time", FadeTime);
console.log(FadeTime);
}
async function onSongChange() {
fetchFadeTime();
const album_uri = Spicetify.Player.data.item.metadata.album_uri;
let bgImage = Spicetify.Player.data.item.metadata.image_url;
if (album_uri !== undefined && !album_uri.includes("spotify:show")) {
const albumInfo = await getAlbumInfo(
album_uri.replace("spotify:album:", "")
);
} else if (Spicetify.Player.data.item.uri.includes("spotify:episode")) {
bgImage = bgImage.replace("spotify:image:", "https://i.scdn.co/image/");
} else if (Spicetify.Player.data.item.provider === "ad") {
return;
} else {
setTimeout(onSongChange, 200);
}
loopOptions("/");
updateLyricsPageProperties();
if (!config.useCustomColor) {
let imageUrl;
if (!config.useCurrSongAsHome) {
imageUrl = Spicetify.Player.data.item.metadata.image_url.replace("spotify:image:", "https://i.scdn.co/image/");
} else {
const defImage = "https://i.imgur.com/Wl2D0h0.png";
imageUrl = localStorage.getItem("hazy:startupBg") || defImage;
}
changeAccentColors(imageUrl);
} else {
let color = localStorage.getItem("CustomColor") || "#FFC0EA";
document.querySelector(':root').style.setProperty('--spice-button', color);
document.querySelector(':root').style.setProperty('--spice-button-active', color);
document.querySelector(':root').style.setProperty('--spice-accent', color);
}
}
function changeAccentColors(imageUrl) {
getMostProminentColor(imageUrl, function (color) {
document.querySelector(':root').style.setProperty('--spice-button', color);
document.querySelector(':root').style.setProperty('--spice-button-active', color);
document.querySelector(':root').style.setProperty('--spice-accent', color);
});
}
function getMostProminentColor(imageUrl, callback) {
const img = new Image();
img.crossOrigin = "Anonymous";
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
let rgbList = buildRgb(imageData);
let hexColor = findColor(rgbList);
if (!hexColor) {
hexColor = findColor(rgbList, true);
}
callback(hexColor);
};
img.onerror = function () {
console.error("Image load error");
callback(null);
};
img.src = imageUrl;
}
function findColor(rgbList, skipFilters = false) {
const colorCount = {};
let maxColor = '';
let maxCount = 0;
for (let i = 0; i < rgbList.length; i++) {
if (!skipFilters && (isTooDark(rgbList[i]) || isTooCloseToWhite(rgbList[i]))) {
continue;
}
const color = `${rgbList[i].r},${rgbList[i].g},${rgbList[i].b}`;
colorCount[color] = (colorCount[color] || 0) + 1;
if (colorCount[color] > maxCount) {
maxColor = color;
maxCount = colorCount[color];
}
}
if (maxColor) {
const [r, g, b] = maxColor.split(',').map(Number);
return rgbToHex(r, g, b);
} else {
return null;
}
}
const buildRgb = (imageData) => {
const rgbValues = [];
for (let i = 0; i < imageData.length; i += 4) {
const rgb = {
r: imageData[i],
g: imageData[i + 1],
b: imageData[i + 2],
};
rgbValues.push(rgb);
}
return rgbValues;
};
function rgbToHex(r, g, b) {
return "#" + [r, g, b].map(x => x.toString(16).padStart(2, '0')).join('');
}
function isTooDark(rgb) {
const brightness = 0.299 * rgb.r + 0.587 * rgb.g + 0.114 * rgb.b;
const threshold = 100;
return brightness < threshold;
}
function isTooCloseToWhite(rgb) {
const threshold = 200;
return rgb.r > threshold && rgb.g > threshold && rgb.b > threshold;
}
Spicetify.Player.addEventListener("songchange", onSongChange);
onSongChange();
windowControls();
galaxyFade();
function scrollToTop() {
const element = document.querySelector(".main-entityHeader-container");
element.scrollIntoView({ behavior: "smooth", block: "start" });
}
document.addEventListener("click", (event) => {
const clickedElement = event.target;
if (clickedElement.closest(".main-entityHeader-topbarTitle")) {
scrollToTop();
}
});
(function sidebar() {
const item = localStorage.getItem("spicetify-exp-features");
const parsedObject = JSON.parse(item);
let reload = false;
const features = [
"enableYLXSidebar",
"enableRightSidebar",
"enableRightSidebarTransitionAnimations",
"enableRightSidebarLyrics",
"enableRightSidebarExtractedColors",
"enablePanelSizeCoordination",
];
if (!localStorage.getItem("Hazy Sidebar Activated")) {
localStorage.setItem("Hazy Sidebar Activated", true);
for (const feature of features) {
if (!parsedObject[feature]) continue;
if (!parsedObject[feature].value) {
parsedObject[feature].value = true;
reload = true;
}
}
}
localStorage.setItem(
"spicetify-exp-features",
JSON.stringify(parsedObject)
);
if (reload) {
window.location.reload();
reload = false;
}
})();
function windowControls() {
function detectOS() {
const userAgent = window.navigator.userAgent;
if (userAgent.indexOf("Win") !== -1) {
document.body.classList.add("windows");
}
}
detectOS();
}
function addTransparentControls(height, width) {
document.documentElement.style.setProperty(
"--control-height",
`${height}px`
);
document.documentElement.style.setProperty("--control-width", `${width}px`);
}
async function setMainWindowControlHeight(height) {
await Spicetify.CosmosAsync.post("sp://messages/v1/container/control", {
type: "update_titlebar",
height: height,
});
}
function updateZoomVariable() {
let prevOuterWidth = window.outerWidth;
let prevInnerWidth = window.innerWidth;
let prevRatio = window.devicePixelRatio;
function calculateAndApplyZoom() {
const newOuterWidth = window.outerWidth;
const newInnerWidth = window.innerWidth;
const newRatio = window.devicePixelRatio;
if (
prevOuterWidth <= 160 ||
prevRatio !== newRatio ||
prevOuterWidth !== newOuterWidth ||
prevInnerWidth !== newInnerWidth
) {
const zoomFactor = newOuterWidth / newInnerWidth || 1;
document.documentElement.style.setProperty("--zoom", zoomFactor);
console.debug(
`Zoom Updated: ${newOuterWidth} / ${newInnerWidth} = ${zoomFactor}`
);
prevOuterWidth = newOuterWidth;
prevInnerWidth = newInnerWidth;
prevRatio = newRatio;
}
}
calculateAndApplyZoom();
window.addEventListener("resize", calculateAndApplyZoom);
}
updateZoomVariable();
function waitForElement(elements, func, timeout = 100) {
const queries = elements.map((element) => document.querySelector(element));
if (queries.every((a) => a)) {
func(queries);
} else if (timeout > 0) {
setTimeout(waitForElement, 300, elements, func, timeout - 1);
}
}
function getAndApplyNav(element) {
const isCenteredGlobalNav = Spicetify.Platform.version >= "1.2.46.462";
document.body.classList.add(
`${
element?.[0]?.classList.contains("Root__globalNav")
? isCenteredGlobalNav
? "global-nav-centered"
: "global-nav"
: "control-nav"
}`
);
}
waitForElement([".Root__globalNav"], getAndApplyNav, 10000);
Spicetify.Platform.History.listen(updateLyricsPageProperties);
waitForElement([".Root__lyrics-cinema"], ([lyricsCinema]) => {
const lyricsCinemaObserver = new MutationObserver(
updateLyricsPageProperties
);
const lyricsCinemaObserverConfig = {
attributes: true,
attributeFilter: ["class"],
};
lyricsCinemaObserver.observe(lyricsCinema, lyricsCinemaObserverConfig);
});
waitForElement([".main-view-container"], ([mainViewContainer]) => {
const mainViewContainerResizeObserver = new ResizeObserver(
updateLyricsPageProperties
);
mainViewContainerResizeObserver.observe(mainViewContainer);
});
function updateLyricsPageProperties() {
function setLyricsPageProperties() {
function detectTextDirection() {
const lyric = document.querySelectorAll(
".lyrics-lyricsContent-lyric"
)[2];
const rtl_rx = /[\u0591-\u07FF]/;
return rtl_rx.test(lyric.innerText) ? "rtl" : "ltr";
}
function setLyricsTransformOrigin(textDirection) {
if (textDirection === "rtl") {
document.documentElement.style.setProperty(
"--lyrics-text-direction",
"right"
);
} else {
document.documentElement.style.setProperty(
"--lyrics-text-direction",
"left"
);
}
}
function calculateLyricsMaxWidth(lyricsContentWrapper) {
const lyricsContentContainer = lyricsContentWrapper.parentElement;
const marginLeft = Number.parseInt(
window.getComputedStyle(lyricsContentWrapper).marginLeft,
10
);
const totalOffset = lyricsContentWrapper.offsetLeft + marginLeft;
return Math.round(
0.95 * (lyricsContentContainer.clientWidth - totalOffset)
);
}
function lockLyricsWrapperWidth(lyricsWrapper) {
const lyricsWrapperWidth = lyricsWrapper.getBoundingClientRect().width;
lyricsWrapper.style.maxWidth = `${lyricsWrapperWidth}px`;
lyricsWrapper.style.width = `${lyricsWrapperWidth}px`;
}
waitForElement(
[".lyrics-lyrics-contentWrapper"],
([lyricsContentWrapper]) => {
lyricsContentWrapper.style.maxWidth = "";
lyricsContentWrapper.style.width = "";
const lyricsTextDirection = detectTextDirection();
setLyricsTransformOrigin(lyricsTextDirection);
const lyricsMaxWidth = calculateLyricsMaxWidth(lyricsContentWrapper);
document.documentElement.style.setProperty(
"--lyrics-active-max-width",
`${lyricsMaxWidth}px`
);
lockLyricsWrapperWidth(lyricsContentWrapper);
}
);
}
function lyricsCallback(mutationsList, lyricsObserver) {
for (const mutation of mutationsList) {
for (addedNode of mutation.addedNodes) {
if (addedNode.classList?.contains("lyrics-lyricsContent-provider")) {
setLyricsPageProperties();
}
}
}
lyricsObserver.disconnect;
}
waitForElement(
[".lyrics-lyricsContent-provider"],
([lyricsContentProvider]) => {
const lyricsContentWrapper = lyricsContentProvider.parentElement;
setLyricsPageProperties();
const lyricsObserver = new MutationObserver(lyricsCallback);
const lyricsObserverConfig = { childList: true };
lyricsObserver.observe(lyricsContentWrapper, lyricsObserverConfig);
}
);
}
function galaxyFade() {
waitForElement(
[".Root__main-view [data-overlayscrollbars-viewport]"],
([scrollNode]) => {
scrollNode.addEventListener("scroll", () => {
const scrollValue = scrollNode.scrollTop;
const artist_fade = Math.max(0, (-0.3 * scrollValue + 100) / 100);
document.documentElement.style.setProperty(
"--artist-fade",
artist_fade
);
const fadeDirection =
scrollNode.scrollTop === 0
? "bottom"
: scrollNode.scrollHeight -
scrollNode.scrollTop -
scrollNode.clientHeight ===
0
? "top"
: "full";
scrollNode.setAttribute("fade", fadeDirection);
if (scrollNode.scrollTop === 0) {
scrollNode.setAttribute("fade", "bottom");
} else if (
scrollNode.scrollHeight -
scrollNode.scrollTop -
scrollNode.clientHeight ===
0
) {
scrollNode.setAttribute("fade", "top");
} else {
scrollNode.setAttribute("fade", "full");
}
});
}
);
waitForElement(
[".Root__nav-bar [data-overlayscrollbars-viewport]"],
([scrollNode]) => {
scrollNode.setAttribute("fade", "bottom");
scrollNode.addEventListener("scroll", () => {
if (scrollNode.scrollTop === 0) {
scrollNode.setAttribute("fade", "bottom");
} else if (
scrollNode.scrollHeight -
scrollNode.scrollTop -
scrollNode.clientHeight ===
0
) {
scrollNode.setAttribute("fade", "top");
} else {
scrollNode.setAttribute("fade", "full");
}
});
}
);
}
const config = {};
parseOptions();
function loopOptions(page) {
if (page === "/") {
if (config.useCurrSongAsHome) {
document.documentElement.style.setProperty(
"--image_url",
`url("${startImage}")`
);
} else {
const bgImage = Spicetify.Player.data.item.metadata.image_url;
document.documentElement.style.setProperty(
"--image_url",
`url("${bgImage}")`
);
}
}
}
loopOptions("/");
})();
let current = "5.5";
function waitForElement(els, func, timeout = 100) {
const queries = els.map((el) => document.querySelector(el));
if (queries.every((a) => a)) {
func(queries);
} else if (timeout > 0) {
setTimeout(waitForElement, 300, els, func, --timeout);
}
}
function getAlbumInfo(id) {
return Spicetify.CosmosAsync.get(`https://api.spotify.com/v1/albums/${id}`);
}
function getEpisodeInfo(id) {
return Spicetify.CosmosAsync.get(`https://api.spotify.com/v1/episodes/${id}`);
}
function isLight(hex) {
var [r, g, b] = hexToRgb(hex).map(Number);
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
return brightness > 128;
}
function hexToRgb(hex) {
var bigint = parseInt(hex.replace("#", ""), 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return [r, g, b];
}
function rgbToHex([r, g, b]) {
const rgb = (r << 16) | (g << 8) | (b << 0);
return "#" + (0x1000000 + rgb).toString(16).slice(1);
}
function lightenDarkenColor(h, p) {
return (
"#" +
[1, 3, 5]
.map((s) => parseInt(h.substr(s, 2), 16))
.map((c) => parseInt((c * (100 + p)) / 100))
.map((c) => (c < 255 ? c : 255))
.map((c) => c.toString(16).padStart(2, "0"))
.join("")
);
}
function rgbToHsl([r, g, b]) {
(r /= 255), (g /= 255), (b /= 255);
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h,
s,
l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}
return [h, s, l];
}
function hslToRgb([h, s, l]) {
var r, g, b;
if (s == 0) {
r = g = b = l; // achromatic
} else {
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
return [r * 255, g * 255, b * 255];
}
function setLightness(hex, lightness) {
hsl = rgbToHsl(hexToRgb(hex));
hsl[2] = lightness;
return rgbToHex(hslToRgb(hsl));
}
let textColor = "#1db954";
let textColorBg = getComputedStyle(document.documentElement).getPropertyValue("--spice-main");
function setRootColor(name, colHex) {
let root = document.documentElement;
if (root === null) return;
root.style.setProperty("--spice-" + name, colHex);
root.style.setProperty("--spice-rgb-" + name, hexToRgb(colHex).join(","));
}
function toggleDark(setDark) {
if (setDark === undefined) setDark = isLight(textColorBg);
document.documentElement.style.setProperty("--is_light", setDark ? 0 : 1);
textColorBg = setDark ? "#0A0A0A" : "#FAFAFA";
setRootColor("main", textColorBg);
setRootColor("sidebar", textColorBg);
setRootColor("player", textColorBg);
setRootColor("shadow", textColorBg);
setRootColor("card", setDark ? "#040404" : "#ECECEC");
setRootColor("subtext", setDark ? "#EAEAEA" : "#3D3D3D");
setRootColor("selected-row", setDark ? "#EAEAEA" : "#3D3D3D");
setRootColor("main-elevated", setDark ? "#303030" : "#DDDDDD");
setRootColor("notification", setDark ? "#303030" : "#DDDDDD");
setRootColor("highlight-elevated", setDark ? "#303030" : "#DDDDDD");
updateColors(textColor);
}
/* Init with current system light/dark mode */
let systemDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
toggleDark(systemDark);
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
toggleDark(e.matches);
});
waitForElement([".main-actionButtons"], (queries) => {
// Add activator on top bar
const buttonContainer = queries[0];
const button = document.createElement("button");
Array.from(buttonContainer.firstChild.attributes).forEach((attr) => {
button.setAttribute(attr.name, attr.value);
});
button.id = "main-topBar-moon-button";
button.className = buttonContainer.firstChild.className;
button.onclick = () => {
toggleDark();
};
button.innerHTML = `<svg role="img" viewBox="0 0 16 16" height="16" width="16"><path fill="currentColor" d="M9.598 1.591a.75.75 0 01.785-.175 7 7 0 11-8.967 8.967.75.75 0 01.961-.96 5.5 5.5 0 007.046-7.046.75.75 0 01.175-.786zm1.616 1.945a7 7 0 01-7.678 7.678 5.5 5.5 0 107.678-7.678z"></path></svg>`;
const tooltip = Spicetify.Tippy(button, {
...Spicetify.TippyProps,
content: "Light/Dark"
});
buttonContainer.insertBefore(button, buttonContainer.firstChild);
});
function updateColors(textColHex) {
if (textColHex == undefined) return registerCoverListener();
let isLightBg = isLight(textColorBg);
if (isLightBg)
textColHex = lightenDarkenColor(textColHex, -15); // vibrant color is always too bright for white bg mode
else textColHex = setLightness(textColHex, 0.45);
let darkColHex = lightenDarkenColor(textColHex, isLightBg ? 12 : -20);
let darkerColHex = lightenDarkenColor(textColHex, isLightBg ? 30 : -40);
let softHighlightHex = setLightness(textColHex, isLightBg ? 0.9 : 0.14);
setRootColor("text", textColHex);
setRootColor("button", darkerColHex);
setRootColor("button-active", darkColHex);
setRootColor("selected-row", darkerColHex);
setRootColor("tab-active", softHighlightHex);
setRootColor("button-disabled", softHighlightHex);
let softerHighlightHex = setLightness(textColHex, isLightBg ? 0.9 : 0.1);
setRootColor("highlight", softerHighlightHex);
// compute hue rotation to change spotify green to main color
let rgb = hexToRgb(textColHex);
let m = `url('data:image/svg+xml;utf8,
<svg xmlns="http://www.w3.org/2000/svg">
<filter id="recolor" color-interpolation-filters="sRGB">
<feColorMatrix type="matrix" values="
0 0 0 0 ${rgb[0] / 255}
0 0 0 0 ${rgb[1] / 255}
0 0 0 0 ${rgb[2] / 255}
0 0 0 1 0
"/>
</filter>
</svg>
#recolor')`;
document.documentElement.style.setProperty("--colormatrix", encodeURI(m));
}
let nearArtistSpanText = "";
async function songchange() {
if (!document.querySelector(".main-trackInfo-container")) return setTimeout(songchange, 300);
try {
// warning popup
if (Spicetify.Platform.PlatformData.client_version_triple < "1.1.68") Spicetify.showNotification(`Your version of Spotify ${Spicetify.Platform.PlatformData.client_version_triple}) is un-supported`);
} catch (err) {
console.error(err);
}
let album_uri = Spicetify.Player.data.item.metadata.album_uri;
let bgImage = Spicetify.Player.data.item.metadata.image_url;
if (!bgImage) {
bgImage = "https://cdn.jsdelivr.net/gh/JulienMaille/spicetify-dynamic-theme@main/images/tracklist-row-song-fallback.svg";
textColor = "#1db954";
updateColors(textColor);
}
if (album_uri && !album_uri.includes("spotify:show")) {
const albumInfo = await getAlbumInfo(album_uri.replace("spotify:album:", ""));
let album_date = new Date(albumInfo.release_date);
let recent_date = new Date();
recent_date.setMonth(recent_date.getMonth() - 6);
album_date = album_date.toLocaleString("default", album_date > recent_date ? { year: "numeric", month: "short" } : { year: "numeric" });
nearArtistSpanText = `
<span>
<span draggable="true">
<a draggable="false" dir="auto" href="${album_uri}">${Spicetify.Player.data.item.metadata.album_title}</a>
</span>
</span>
<span> • ${album_date}</span>
`;
} else if (Spicetify.Player.data.item.type === "episode") {
// podcast
const episodeInfo = await getEpisodeInfo(Spicetify.Player.data.item.uri.replace("spotify:episode:", ""));
let podcast_date = new Date(episodeInfo.release_date);
podcast_date = podcast_date.toLocaleString("default", { year: "numeric", month: "short" });
bgImage = bgImage.replace("spotify:image:", "https://i.scdn.co/image/");
nearArtistSpanText = `
<span>
<span draggable="true">
<a draggable="false" dir="auto" href="${album_uri}">${Spicetify.Player.data.item.metadata["show.publisher"]}</a>
</span>
</span>
<span> • ${podcast_date}</span>
`;
} else if (Spicetify.Player.data.item.isLocal) {
// local file
nearArtistSpanText = Spicetify.Player.data.item.metadata.album_title;
} else if (Spicetify.Player.data.item.provider == "ad") {
// ad
nearArtistSpanText.innerHTML = "Advertisement";
return;
} else {
// When clicking a song from the homepage, songChange is fired with half empty metadata
// todo: retry only once?
setTimeout(songchange, 200);
}
if (!document.querySelector("#main-trackInfo-year")) {
waitForElement([".main-trackInfo-container:not(#upcomingSongDiv)"], (queries) => {
nearArtistSpan = document.createElement("div");
nearArtistSpan.id = "main-trackInfo-year";
nearArtistSpan.classList.add("main-trackInfo-release", "standalone-ellipsis-one-line", "main-type-finale");
nearArtistSpan.innerHTML = nearArtistSpanText;
queries[0].append(nearArtistSpan);
});
} else {
nearArtistSpan.innerHTML = nearArtistSpanText;
}
document.documentElement.style.setProperty("--image_url", `url("${bgImage}")`);
pickCoverColor();
}
function getVibrant(image) {
try {
var swatches = new Vibrant(image, 12).swatches();
cols = isLight(textColorBg) ? ["Vibrant", "DarkVibrant", "Muted", "LightVibrant"] : ["Vibrant", "LightVibrant", "Muted", "DarkVibrant"];
for (var col in cols)
if (swatches[cols[col]]) {
textColor = swatches[cols[col]].getHex();
break;
}
} catch (err) {
console.error(err);
}
}
function pickCoverColor() {
const img = document.querySelector(".main-image-image.cover-art-image");
if (!img) return setTimeout(pickCoverColor, 250); // Check if image exists
// Force src for local files, otherwise we will pick color from previous cover
if (Spicetify.Player.data.item.isLocal) img.src = Spicetify.Player.data.item.metadata.image_url;
if (!img.complete) return setTimeout(pickCoverColor, 250); // Check if image is loaded
textColor = "#1db954";
if (Spicetify.Platform.PlatformData.client_version_triple >= "1.2.48" && img.src.startsWith("https://i.scdn.co/image")) {
var imgCORS = new Image();
imgCORS.crossOrigin = "anonymous"; // Enable CORS
imgCORS.src = Spicetify.Player.data.item.metadata.image_url.replace("spotify:image:", "https://i.scdn.co/image/");
imgCORS.onload = function () {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
ctx.drawImage(imgCORS, 0, 0);
getVibrant(imgCORS);
imgCORS = null;
updateColors(textColor);
};
return;
} else {
if (!img.src.startsWith("spotify:")) return;
}
if (img.complete) getVibrant(img);
updateColors(textColor);
}
Spicetify.Player.addEventListener("songchange", songchange);
songchange();
document.documentElement.style.setProperty("--warning_message", " ");
(function e$$0(x, z, l) {
function h(p, b) {
if (!z[p]) {
if (!x[p]) {
var a = "function" == typeof require && require;
if (!b && a) return a(p, !0);
if (g) return g(p, !0);
a = Error("Cannot find module '" + p + "'");
throw ((a.code = "MODULE_NOT_FOUND"), a);
}
a = z[p] = { exports: {} };
x[p][0].call(
a.exports,
function (a) {
var b = x[p][1][a];
return h(b ? b : a);
},
a,
a.exports,
e$$0,
x,
z,
l
);
}
return z[p].exports;
}
for (var g = "function" == typeof require && require, w = 0; w < l.length; w++) h(l[w]);
return h;
})(
{
1: [
function (A, x, z) {
if (!l)
var l = {
map: function (h, g) {
var l = {};
return g
? h.map(function (h, b) {
l.index = b;
return g.call(l, h);
})
: h.slice();
},
naturalOrder: function (h, g) {
return h < g ? -1 : h > g ? 1 : 0;
},
sum: function (h, g) {
var l = {};
return h.reduce(
g
? function (h, b, a) {
l.index = a;
return h + g.call(l, b);
}
: function (h, b) {
return h + b;
},
0
);
},
max: function (h, g) {
return Math.max.apply(null, g ? l.map(h, g) : h);
}
};
A = (function () {
function h(f, c, a) {
return (f << (2 * d)) + (c << d) + a;
}
function g(f) {
function c() {
a.sort(f);
b = !0;
}
var a = [],
b = !1;
return {
push: function (c) {
a.push(c);
b = !1;
},
peek: function (f) {
b || c();
void 0 === f && (f = a.length - 1);
return a[f];
},
pop: function () {
b || c();
return a.pop();
},
size: function () {
return a.length;
},
map: function (c) {
return a.map(c);
},
debug: function () {
b || c();
return a;
}
};
}
function w(f, c, a, b, m, e, q) {
this.r1 = f;
this.r2 = c;
this.g1 = a;
this.g2 = b;
this.b1 = m;
this.b2 = e;
this.histo = q;
}
function p() {
this.vboxes = new g(function (f, c) {
return l.naturalOrder(f.vbox.count() * f.vbox.volume(), c.vbox.count() * c.vbox.volume());
});
}
function b(f) {
var c = Array(1 << (3 * d)),
a,
b,
m,
r;
f.forEach(function (f) {
b = f[0] >> e;
m = f[1] >> e;
r = f[2] >> e;
a = h(b, m, r);
c[a] = (c[a] || 0) + 1;
});
return c;
}
function a(f, c) {
var a = 1e6,
b = 0,
m = 1e6,
d = 0,
q = 1e6,
n = 0,
h,
k,
l;
f.forEach(function (c) {
h = c[0] >> e;
k = c[1] >> e;
l = c[2] >> e;
h < a ? (a = h) : h > b && (b = h);
k < m ? (m = k) : k > d && (d = k);
l < q ? (q = l) : l > n && (n = l);
});
return new w(a, b, m, d, q, n, c);
}