-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreditsClickCopy.js
More file actions
855 lines (751 loc) · 32.4 KB
/
Copy pathcreditsClickCopy.js
File metadata and controls
855 lines (751 loc) · 32.4 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
((async () => {
const CREDIT_SELECTOR = 'div[class*="credit" i] a, div[class*="credit" i] span';
const CREDITS_MODAL_ROOT_SELECTOR = '.main-trackCreditsModal-container';
const CREDITS_CLOSE_BUTTON_SELECTOR = 'button.main-trackCreditsModal-closeBtn';
const VERSION_URL = 'https://raw.githubusercontent.com/Balint2201/creditsClickCopy/refs/heads/main/version.json';
const GLOBAL_SWITCH_URL = 'https://raw.githubusercontent.com/Balint2201/creditsClickCopy/refs/heads/main/globalswitch.json';
const STORAGE_KEY_ENABLED = "creditsClickCopy:enabled";
const TRACK_CREDITS_EXPERIMENT_DESCRIPTIONS = [
"Enables grouped credits display in TrackCreditsModal (credits grouped by role category)",
"Enables the new TrackCreditsModal implementation",
];
const TRACK_CREDITS_EXPERIMENT_RELOAD_MARKER = "creditsClickCopy:reloadedAfterDisablingTrackCreditsModal";
const TRACK_CREDITS_EXPERIMENT_ENABLE_RELOAD_MARKER = "creditsClickCopy:reloadedAfterEnablingTrackCreditsModal";
const DEFAULT_POPUP_LENGTH_MS = 4500;
const DEBUG_KEY_PREFIX = "creditsClickCopy:debug:";
// Version
const CURRENT_VERSION = '1.5.7';
let enabled = true;
let enabledGlobally = true;
let popupLengthMs = DEFAULT_POPUP_LENGTH_MS;
let running = false;
let observer;
function setDebugValue(key, value) {
try {
localStorage.setItem(`${DEBUG_KEY_PREFIX}${key}`, String(value));
} catch {}
}
function getDebugValue(key) {
try {
return localStorage.getItem(`${DEBUG_KEY_PREFIX}${key}`);
} catch {
return null;
}
}
function getTrackCreditsExperimentLabel() {
return TRACK_CREDITS_EXPERIMENT_DESCRIPTIONS[0];
}
function getTrackCreditsExperimentTarget(props, description) {
const exactMatch = (props || []).find(p => String(p?.description || "").trim() === description);
if (exactMatch) return exactMatch;
return (props || []).find(p => /TrackCreditsModal/i.test(String(p?.description || "")));
}
function shouldReloadAfterTrackCreditsExperimentChange(previousEffective, desiredValue) {
return previousEffective === !desiredValue;
}
function shouldReloadAfterAnyTrackCreditsExperimentChange(statusList) {
return Array.isArray(statusList) && statusList.some(status => status?.requiresReload);
}
function ensureStyle() {
if (document.getElementById("credits-click-copy-style")) return;
if (!document.head) return;
const style = document.createElement("style");
style.id = "credits-click-copy-style";
style.textContent = `
${CREDITS_MODAL_ROOT_SELECTOR} div[class*="credit" i] a,
${CREDITS_MODAL_ROOT_SELECTOR} div[class*="credit" i] span {
cursor: copy;
}
.copied {
opacity: 0.6;
transition: opacity 150ms ease;
}
.ccc-toast {
position: fixed;
right: 16px;
bottom: 72px;
background: rgba(0,0,0,0.85);
color: #fff;
border-radius: 8px;
padding: 10px 12px;
font-size: 13px;
box-shadow: 0 8px 24px rgba(0,0,0,0.3);
z-index: 99999;
opacity: 0;
transform: translateY(8px);
transition: opacity 150ms ease, transform 150ms ease;
pointer-events: none;
}
.ccc-toast.show {
opacity: 1;
transform: translateY(0);
}
.ccc-about-debug {
margin-top: 10px;
}
.ccc-about-debug summary {
font-size: 14px;
font-weight: 700;
cursor: pointer;
user-select: none;
}
.ccc-about-debug[open] summary {
margin-bottom: 8px;
}
.ccc-about-debug .row {
display: flex;
gap: 10px;
margin: 4px 0;
flex-wrap: wrap;
}
.ccc-about-debug .k {
font-weight: 600;
}
.ccc-about-debug .v {
opacity: 0.9;
word-break: break-word;
}
.ccc-about-debug button {
cursor: pointer;
}
`;
document.head.appendChild(style);
}
function getStoredEnabled() {
try {
const raw = window.Spicetify?.LocalStorage?.get?.(STORAGE_KEY_ENABLED) ?? localStorage.getItem(STORAGE_KEY_ENABLED);
if (raw === null || raw === undefined || raw === "") return true;
if (typeof raw === "boolean") return raw;
const normalized = String(raw).toLowerCase();
return normalized === "true" || normalized === "1" || normalized === "yes";
} catch {
return true;
}
}
function setStoredEnabled(value) {
const serialized = value ? "true" : "false";
try {
window.Spicetify?.LocalStorage?.set?.(STORAGE_KEY_ENABLED, serialized);
} catch {}
try {
localStorage.setItem(STORAGE_KEY_ENABLED, serialized);
} catch {}
}
function isElementVisible(el) {
if (!el || el.nodeType !== 1) return false;
try {
if (el.getClientRects().length === 0) return false;
} catch {}
try {
const style = window.getComputedStyle(el);
if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") return false;
} catch {}
return true;
}
function getOpenCreditsModalRoot() {
// Strongest signal: the Credits modal close button.
try {
const buttons = Array.from(document.querySelectorAll(CREDITS_CLOSE_BUTTON_SELECTOR));
for (let i = buttons.length - 1; i >= 0; i--) {
const btn = buttons[i];
if (!isElementVisible(btn)) continue;
const root = btn.closest(CREDITS_MODAL_ROOT_SELECTOR);
if (!root) continue;
if (!isElementVisible(root)) continue;
if (!root.querySelector?.(CREDIT_SELECTOR)) continue;
return root;
}
} catch {}
// Fallback: find the last visible credits modal root in DOM order.
try {
const roots = Array.from(document.querySelectorAll(CREDITS_MODAL_ROOT_SELECTOR));
for (let i = roots.length - 1; i >= 0; i--) {
const root = roots[i];
if (!isElementVisible(root)) continue;
if (!root.querySelector?.(CREDIT_SELECTOR)) continue;
// Optional hardening: credits modal should expose the close button.
if (!root.querySelector?.(CREDITS_CLOSE_BUTTON_SELECTOR)) continue;
return root;
}
} catch {}
return null;
}
function isInOpenCreditsModal(el) {
const root = getOpenCreditsModalRoot();
if (!root) return false;
try {
return root.contains(el);
} catch {
return false;
}
}
function getCopyTargetFromEventTarget(target) {
if (!target?.closest) return null;
const candidate = target.closest(CREDIT_SELECTOR);
if (!candidate) return null;
if (!isInOpenCreditsModal(candidate)) return null;
return candidate;
}
function copyText(text) {
navigator.clipboard.writeText(text).catch(() => {
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.style.position = "fixed";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
});
}
function onDocumentClick(e) {
if (!enabled) return;
if (e.defaultPrevented) return;
if (e.button !== 0) return;
const el = getCopyTargetFromEventTarget(e.target);
if (!el) return;
e.preventDefault();
e.stopPropagation();
const text = el.textContent?.trim();
if (!text) return;
copyText(text);
el.classList.add("copied");
setTimeout(() => el.classList.remove("copied"), 150);
}
function start() {
if (running) return;
if (!document.body) {
setTimeout(start, 100);
return;
}
ensureStyle();
setDebugValue("lastStartedAt", new Date().toISOString());
running = true;
document.addEventListener("click", onDocumentClick, true);
checkVersion();
}
function stop() {
if (!running) return;
running = false;
setDebugValue("lastStoppedAt", new Date().toISOString());
document.removeEventListener("click", onDocumentClick, true);
observer?.disconnect();
observer = undefined;
}
function showToast(text, durationMs = popupLengthMs) {
ensureStyle();
if (!document.body) {
setTimeout(() => showToast(text, durationMs), 150);
return;
}
let el = document.getElementById("ccc-toast");
if (!el) {
el = document.createElement("div");
el.id = "ccc-toast";
el.className = "ccc-toast";
el.textContent = text;
document.body.appendChild(el);
requestAnimationFrame(() => el.classList.add("show"));
} else {
el.textContent = text;
el.classList.add("show");
}
const finalDuration = Number.isFinite(durationMs) ? Math.max(500, durationMs) : DEFAULT_POPUP_LENGTH_MS;
clearTimeout(el._hideTimer);
el._hideTimer = setTimeout(() => {
el.classList.remove("show");
setTimeout(() => el.remove(), 200);
}, finalDuration);
}
function checkVersion() {
fetch(VERSION_URL, { cache: "no-store" })
.then(r => r.ok ? r.json() : null)
.then(json => {
if (!json || !json.version) return;
setDebugValue("lastVersionCheckAt", new Date().toISOString());
setDebugValue("lastLatestVersion", String(json.version));
if (String(json.version) !== String(CURRENT_VERSION)) {
showToast(`creditsClickCopy: Update available. Current: v${CURRENT_VERSION} · Latest: v${json.version}`);
}
})
.catch(() => {});
}
function setupMenuToggle() {
if (!window.Spicetify?.Menu?.Item) {
setTimeout(setupMenuToggle, 300);
return;
}
const menuItem = new Spicetify.Menu.Item(
"creditsClickCopy",
enabled,
async (self) => {
const next = !self.isEnabled;
self.setState(next);
enabled = next;
setStoredEnabled(enabled);
if (enabled) {
if (shouldAttemptTrackCreditsExperimentOverride()) {
const status = await setTrackCreditsModalExperimentDisabled().catch(() => null);
if (status?.failed && status?.effective !== false) {
showToast(`creditsClickCopy: Couldn't disable the experiment "${getTrackCreditsExperimentLabel()}". The extension may not work.`);
}
}
start();
} else {
stop();
if (shouldAttemptTrackCreditsExperimentOverride()) {
await setTrackCreditsModalExperimentEnabled().catch(() => {});
}
}
try {
window.Spicetify?.showNotification?.(`creditsClickCopy ${enabled ? "enabled" : "disabled"}`);
} catch {}
}
);
menuItem.register();
}
function coerceBoolean(value) {
if (typeof value === "boolean") return value;
if (typeof value === "string") {
const normalized = value.trim().toLowerCase();
if (normalized === "true") return true;
if (normalized === "false") return false;
}
if (typeof value === "number") {
if (value === 1) return true;
if (value === 0) return false;
}
return undefined;
}
function getRemoteConfigEffectiveBoolean(prop) {
if (!prop) return undefined;
const candidates = [
prop.effectiveValue,
prop.resolvedValue,
prop.currentValue,
prop.value
];
for (const candidate of candidates) {
const coerced = coerceBoolean(candidate);
if (typeof coerced === "boolean") return coerced;
}
return undefined;
}
function waitFor(predicate, { intervalMs = 250, timeoutMs = 5000 } = {}) {
return new Promise(resolve => {
const start = Date.now();
const tick = () => {
try {
if (predicate()) return resolve(true);
} catch {}
if (Date.now() - start >= timeoutMs) return resolve(false);
setTimeout(tick, intervalMs);
};
tick();
});
}
const MIN_SPOTIFY_VERSION_FOR_EXPERIMENT_OVERRIDE = "1.2.83";
function parseMajorMinorPatch(versionLike) {
if (!versionLike) return null;
const m = String(versionLike).match(/(\d+)\.(\d+)\.(\d+)/);
if (!m) return null;
const major = Number(m[1]);
const minor = Number(m[2]);
const patch = Number(m[3]);
if (![major, minor, patch].every(n => Number.isFinite(n))) return null;
return [major, minor, patch];
}
function compareMajorMinorPatch(a, b) {
for (let i = 0; i < 3; i++) {
const diff = (a?.[i] ?? 0) - (b?.[i] ?? 0);
if (diff !== 0) return diff;
}
return 0;
}
function getSpotifyAppVersionString() {
// Best-effort: Spotify desktop typically includes a `Spotify/x.y.z.build` token in the UA.
try {
const ua = navigator.userAgent || "";
const m = ua.match(/\bSpotify\/(\d+\.\d+\.\d+(?:\.\d+)*)\b/i);
if (m?.[1]) return m[1];
} catch {}
return null;
}
function shouldAttemptTrackCreditsExperimentOverride() {
const current = parseMajorMinorPatch(getSpotifyAppVersionString());
const min = parseMajorMinorPatch(MIN_SPOTIFY_VERSION_FOR_EXPERIMENT_OVERRIDE);
if (!min) return true;
// If we can't detect the Spotify app version, keep existing behavior.
if (!current) return true;
return compareMajorMinorPatch(current, min) >= 0;
}
function clampPopupLengthMs(value) {
const n = Number(value);
if (!Number.isFinite(n)) return undefined;
return Math.min(60000, Math.max(500, n));
}
async function getGlobalSwitch() {
try {
const r = await fetch(GLOBAL_SWITCH_URL, { cache: "no-store" });
if (!r.ok) return null;
const json = await r.json();
if (!json || typeof json !== "object") return null;
const enabledFlag = json.enabled_globally;
if (typeof enabledFlag !== "boolean") return null;
const message = typeof json.message === "string" ? json.message : "";
const len = clampPopupLengthMs(json.popuplenght);
return {
enabled_globally: enabledFlag,
message,
popuplenght: len
};
} catch {
return null;
}
}
function looksLikeAboutSpotifyDialogText(text) {
if (!text) return false;
const hasSpicetify = /spicetify/i.test(text);
const hasSpotifyAppLine = /(spotify for windows|spotify for macos|spotify for linux)/i.test(text);
const hasSpotifyVersionLike = /\b\d+\.\d+\.\d+\./.test(text) || /\b\d+\.\d+\.\d+\b/.test(text);
return hasSpicetify && (hasSpotifyAppLine || hasSpotifyVersionLike);
}
function findAboutContainerElement() {
try {
const candidates = [];
const pushUnique = el => {
if (!el) return;
if (candidates.includes(el)) return;
candidates.push(el);
};
for (const modal of Array.from(document.querySelectorAll("generic-modal"))) {
pushUnique(modal.querySelector?.('[role="dialog"][aria-modal="true"]') || modal);
}
for (const dialog of Array.from(document.querySelectorAll('[role="dialog"][aria-modal="true"], [role="dialog"]'))) {
pushUnique(dialog);
}
for (let i = candidates.length - 1; i >= 0; i--) {
const candidate = candidates[i];
if (!isElementVisible(candidate)) continue;
const main = candidate.querySelector?.("main");
const container = isElementVisible(main) ? main : candidate;
const text = container?.textContent || "";
const dialogTitle = (
candidate.getAttribute?.("aria-label") ||
candidate.querySelector?.('h1, h2, h3')?.textContent ||
""
).trim();
if (
/about\s+spotify|spotify\s+névjegye/i.test(dialogTitle) ||
looksLikeAboutSpotifyDialogText(text) ||
/spicetify\s*v?\d+\.\d+\.\d+/i.test(text)
) {
return container;
}
}
} catch {}
try {
const ABOUT_MODAL_CLASS_SUBSTRING = "desktopmodals-aboutSpotifyModal";
const matches = Array.from(document.querySelectorAll(`[class*="${ABOUT_MODAL_CLASS_SUBSTRING}"]`));
for (let i = matches.length - 1; i >= 0; i--) {
let el = matches[i];
if (!isElementVisible(el)) continue;
while (el?.parentElement && String(el.parentElement.className || "").includes(ABOUT_MODAL_CLASS_SUBSTRING)) {
el = el.parentElement;
}
if (!isElementVisible(el)) continue;
return el;
}
} catch {}
const globalAnchor = findVisibleSpicetifyInfoElement(document);
if (globalAnchor) {
return (
globalAnchor.closest('main, [role="dialog"], generic-modal') ||
globalAnchor.parentElement ||
null
);
}
return null;
}
function findSpicetifySummaryElement(aboutRoot) {
if (!aboutRoot?.querySelectorAll) return null;
const nodes = aboutRoot.querySelectorAll("summary");
let best = null;
let bestLen = Infinity;
for (const el of nodes) {
const text = el.textContent;
if (!text) continue;
if (!/spicetify/i.test(text)) continue;
if (!(/\bv\d+\./i.test(text) || /\bspicetify\s*v\d+\./i.test(text) || /\d+\.\d+\.\d+/.test(text))) continue;
const len = text.length;
if (len < bestLen) {
best = el;
bestLen = len;
}
}
return best;
}
function findVisibleSpicetifyInfoElement(root = document) {
if (!root?.querySelectorAll) return null;
const nodes = root.querySelectorAll("summary, details, li, p, div, span, h1, h2, h3");
let best = null;
let bestLen = Infinity;
for (const el of nodes) {
if (!isElementVisible(el)) continue;
const text = String(el.textContent || "").trim();
if (!/spicetify\s*v?\d+\.\d+\.\d+/i.test(text)) continue;
const len = text.length;
if (len < bestLen) {
best = el;
bestLen = len;
}
}
return best;
}
function findSpicetifyAnchorElement(aboutRoot) {
const spicetifySummary = findSpicetifySummaryElement(aboutRoot);
if (spicetifySummary) return spicetifySummary.closest("details") || spicetifySummary;
const visibleInfo = findVisibleSpicetifyInfoElement(aboutRoot);
if (visibleInfo) return visibleInfo.closest("details") || visibleInfo;
if (!aboutRoot?.querySelectorAll) return null;
const nodes = aboutRoot.querySelectorAll("details, li, p, div, span, h1, h2, h3");
let best = null;
let bestLen = Infinity;
for (const el of nodes) {
const text = String(el.textContent || "").trim();
if (!/spicetify\s*v?\d+\.\d+\.\d+/i.test(text)) continue;
const len = text.length;
if (len < bestLen) {
best = el;
bestLen = len;
}
}
return best;
}
function renderAboutDebugBlock() {
if (!document.body) return;
const aboutContainer = findAboutContainerElement();
if (!aboutContainer) return;
ensureStyle();
let block = document.getElementById("ccc-about-debug");
if (!block) {
block = document.createElement("details");
block.id = "ccc-about-debug";
block.className = "ccc-about-debug";
const summary = document.createElement("summary");
summary.textContent = "creditsClickCopy (DEBUG)";
block.appendChild(summary);
const insertionAnchor = findSpicetifyAnchorElement(aboutContainer);
if (insertionAnchor?.insertAdjacentElement) insertionAnchor.insertAdjacentElement("afterend", block);
else aboutContainer.appendChild(block);
} else {
const insertionAnchor = findSpicetifyAnchorElement(aboutContainer);
const desiredParent = insertionAnchor?.parentElement || aboutContainer;
const shouldMoveToDesiredParent = desiredParent && block.parentElement !== desiredParent;
const shouldPlaceAfterAnchor = insertionAnchor && block.previousElementSibling !== insertionAnchor;
if ((shouldMoveToDesiredParent || shouldPlaceAfterAnchor) && insertionAnchor?.insertAdjacentElement) {
try { insertionAnchor.insertAdjacentElement("afterend", block); } catch {}
} else if (!insertionAnchor && desiredParent && block.parentElement !== desiredParent) {
try { desiredParent.appendChild(block); } catch {}
}
}
const rows = [
["Version", CURRENT_VERSION],
["Local enabled", String(getStoredEnabled())],
["Global enabled", String(enabledGlobally)],
["Global switch fetched", getDebugValue("lastGlobalSwitchFetchAt") || "(unknown)"],
["Global switch message", getDebugValue("globalSwitchMessage") || ""],
["Popup length (ms)", String(popupLengthMs)],
["Last loaded", getDebugValue("lastLoadedAt") || "(unknown)"],
["Last started", getDebugValue("lastStartedAt") || "(never)"],
["Last stopped", getDebugValue("lastStoppedAt") || "(never)"],
["Experiment last action", getDebugValue("lastExperimentAt") || "(unknown)"],
["Experiment desired", getDebugValue("lastExperimentDesired") || "(unknown)"],
["Experiment result", getDebugValue("lastExperimentResult") || "(unknown)"],
["Version check", getDebugValue("lastVersionCheckAt") || "(unknown)"],
["Latest version", getDebugValue("lastLatestVersion") || "(unknown)"],
];
const summary = block.querySelector("summary");
block.innerHTML = "";
if (summary) block.appendChild(summary);
else {
const s = document.createElement("summary");
s.textContent = "creditsClickCopy (DEBUG)";
block.appendChild(s);
}
const container = document.createElement("div");
block.appendChild(container);
for (const [k, v] of rows) {
const row = document.createElement("div");
row.className = "row";
const keyEl = document.createElement("div");
keyEl.className = "k";
keyEl.textContent = `${k}:`;
const valueEl = document.createElement("div");
valueEl.className = "v";
valueEl.textContent = v;
row.appendChild(keyEl);
row.appendChild(valueEl);
container.appendChild(row);
}
}
async function setupAboutDebugInjection() {
const attempt = () => {
try { renderAboutDebugBlock(); } catch {}
};
let scheduled = false;
const scheduleAttempt = () => {
if (scheduled) return;
scheduled = true;
setTimeout(() => {
scheduled = false;
attempt();
}, 250);
};
attempt();
try {
const observer = new MutationObserver(() => scheduleAttempt());
observer.observe(document.body, { childList: true, subtree: true });
} catch {}
const hasHistory = await waitFor(
() => !!window.Spicetify?.Platform?.History?.listen,
{ intervalMs: 250, timeoutMs: 6000 }
);
if (hasHistory) {
try {
window.Spicetify.Platform.History.listen(() => {
setTimeout(attempt, 300);
setTimeout(attempt, 1200);
});
} catch {}
}
setInterval(attempt, 2500);
}
async function setTrackCreditsModalExperimentOverrideForDescription(description, desiredValue) {
const hasApi = await waitFor(
() => !!window.Spicetify?.Platform?.RemoteConfigDebugAPI?.getProperties,
{ intervalMs: 250, timeoutMs: 6000 }
);
const api = window.Spicetify?.Platform?.RemoteConfigDebugAPI;
if (!hasApi || !api?.getProperties || !api?.setOverride) {
return { effective: undefined, overridden: false, failed: true };
}
let props;
try {
props = await api.getProperties();
} catch {
return { effective: undefined, overridden: false, failed: true };
}
const target = getTrackCreditsExperimentTarget(props, description);
if (!target?.name) return { effective: undefined, overridden: false, failed: true };
const currentEffective = getRemoteConfigEffectiveBoolean(target);
if (currentEffective === desiredValue) {
return { effective: desiredValue, overridden: false, failed: false };
}
try {
await api.setOverride(
{ source: "web", type: "boolean", name: target.name },
desiredValue
);
} catch {
return { effective: currentEffective, overridden: false, failed: true };
}
return {
effective: currentEffective,
overridden: true,
failed: false,
requiresReload: shouldReloadAfterTrackCreditsExperimentChange(currentEffective, desiredValue),
};
}
function setTrackCreditsModalExperimentDisabled() {
return Promise.all(
TRACK_CREDITS_EXPERIMENT_DESCRIPTIONS.map(description => setTrackCreditsModalExperimentOverrideForDescription(description, false))
);
}
function setTrackCreditsModalExperimentEnabled() {
return Promise.all(
TRACK_CREDITS_EXPERIMENT_DESCRIPTIONS.map(description => setTrackCreditsModalExperimentOverrideForDescription(description, true))
);
}
const globalSwitch = await getGlobalSwitch();
if (globalSwitch?.popuplenght !== undefined) popupLengthMs = globalSwitch.popuplenght;
if (globalSwitch && globalSwitch.enabled_globally === false) {
enabledGlobally = false;
showToast(`creditsClickCopy: Extension disabled globally, reason: ${globalSwitch.message}`);
return;
}
enabled = getStoredEnabled();
enabledGlobally = true;
setDebugValue("lastLoadedAt", new Date().toISOString());
if (globalSwitch) {
setDebugValue("lastGlobalSwitchFetchAt", new Date().toISOString());
setDebugValue("globalSwitchEnabled", String(globalSwitch.enabled_globally));
setDebugValue("globalSwitchMessage", globalSwitch.message);
if (globalSwitch.popuplenght !== undefined) setDebugValue("globalSwitchPopupLength", String(globalSwitch.popuplenght));
}
setupAboutDebugInjection().catch(() => {});
if (enabled) {
if (shouldAttemptTrackCreditsExperimentOverride()) {
const expStatusList = await setTrackCreditsModalExperimentDisabled();
const expStatus = {
failed: expStatusList.some(status => status?.failed),
effective: expStatusList.every(status => status?.effective === false) ? false : expStatusList.find(status => status?.effective !== undefined)?.effective,
overridden: expStatusList.some(status => status?.overridden),
};
setDebugValue("lastExperimentAt", new Date().toISOString());
setDebugValue("lastExperimentDesired", "false");
setDebugValue("lastExperimentResult", JSON.stringify(expStatusList));
if (expStatus?.failed && expStatus?.effective !== false) {
showToast(`creditsClickCopy: Couldn't disable the experiment "${getTrackCreditsExperimentLabel()}". The extension may not work.`);
}
if (shouldReloadAfterAnyTrackCreditsExperimentChange(expStatusList)) {
try {
if (!sessionStorage.getItem(TRACK_CREDITS_EXPERIMENT_RELOAD_MARKER)) {
sessionStorage.setItem(TRACK_CREDITS_EXPERIMENT_RELOAD_MARKER, "1");
location.reload();
}
} catch {}
}
} else {
setDebugValue("lastExperimentAt", new Date().toISOString());
setDebugValue("lastExperimentDesired", "skipped");
setDebugValue(
"lastExperimentResult",
JSON.stringify({
skipped: true,
reason: `Spotify < ${MIN_SPOTIFY_VERSION_FOR_EXPERIMENT_OVERRIDE}`,
spotifyVersion: getSpotifyAppVersionString()
})
);
}
start();
} else {
if (shouldAttemptTrackCreditsExperimentOverride()) {
const expStatusList = await setTrackCreditsModalExperimentEnabled().catch(() => null);
setDebugValue("lastExperimentAt", new Date().toISOString());
setDebugValue("lastExperimentDesired", "true");
setDebugValue("lastExperimentResult", JSON.stringify(expStatusList));
if (shouldReloadAfterAnyTrackCreditsExperimentChange(expStatusList)) {
try {
if (!sessionStorage.getItem(TRACK_CREDITS_EXPERIMENT_ENABLE_RELOAD_MARKER)) {
sessionStorage.setItem(TRACK_CREDITS_EXPERIMENT_ENABLE_RELOAD_MARKER, "1");
location.reload();
}
} catch {}
}
} else {
setDebugValue("lastExperimentAt", new Date().toISOString());
setDebugValue("lastExperimentDesired", "skipped");
setDebugValue(
"lastExperimentResult",
JSON.stringify({
skipped: true,
reason: `Spotify < ${MIN_SPOTIFY_VERSION_FOR_EXPERIMENT_OVERRIDE}`,
spotifyVersion: getSpotifyAppVersionString()
})
);
}
stop();
}
setupMenuToggle();
})());