Repository navigation
Expand file tree
/
Copy pathspotloop.js
More file actions
616 lines (547 loc) · 22.9 KB
/
Copy pathspotloop.js
File metadata and controls
616 lines (547 loc) · 22.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
/*
* SpotLoop
* A-B repeat and saved song sections for Spotify Desktop via Spicetify.
*
* Copyright (c) 2026 Jackson Maynard
* Released under the MIT License.
*/
(function spotLoopExtension() {
"use strict";
const STORAGE_KEY = "spotloop:state:v1";
const VERSION = "0.1.5";
const MIN_LOOP_MS = 1000;
const SEEK_GUARD_MS = 350;
const LOOP_POLL_MS = 100;
const PROGRESS_SYNC_THRESHOLD_MS = 25;
function clamp(value, minimum, maximum) {
return Math.min(Math.max(Number(value) || 0, minimum), maximum);
}
function formatTime(milliseconds) {
const totalSeconds = Math.max(0, Math.floor((Number(milliseconds) || 0) / 1000));
const minutes = Math.floor(totalSeconds / 60);
const seconds = String(totalSeconds % 60).padStart(2, "0");
return `${minutes}:${seconds}`;
}
function validBounds(start, end, duration = Number.POSITIVE_INFINITY) {
return (
Number.isFinite(start) &&
Number.isFinite(end) &&
start >= 0 &&
end <= duration &&
end - start >= MIN_LOOP_MS
);
}
function makeLastWindow(position, windowMs = 15000) {
const end = Math.max(0, Number(position) || 0);
return { start: Math.max(0, end - windowMs), end };
}
function playbackContextChanged(
expectedTrackUri,
currentTrackUri,
expectedDeviceId = null,
currentDeviceId = null,
) {
if (!currentTrackUri || currentTrackUri !== expectedTrackUri) return true;
return Boolean(
expectedDeviceId && currentDeviceId && expectedDeviceId !== currentDeviceId,
);
}
function emptyState() {
return { version: 1, sectionsByTrack: {} };
}
function safeParseState(value) {
if (!value) return emptyState();
try {
const parsed = typeof value === "string" ? JSON.parse(value) : value;
if (!parsed || typeof parsed !== "object") return emptyState();
return {
version: 1,
sectionsByTrack:
parsed.sectionsByTrack && typeof parsed.sectionsByTrack === "object"
? parsed.sectionsByTrack
: {},
};
} catch {
return emptyState();
}
}
if (typeof module !== "undefined" && module.exports) {
module.exports = {
clamp,
formatTime,
validBounds,
makeLastWindow,
playbackContextChanged,
safeParseState,
};
}
if (typeof Spicetify === "undefined") return;
function waitForSpicetify() {
if (
!Spicetify.Player ||
!Spicetify.Playbar ||
!Spicetify.PopupModal ||
!Spicetify.Keyboard ||
!Spicetify.LocalStorage ||
!Spicetify.CosmosAsync
) {
setTimeout(waitForSpicetify, 250);
return;
}
initialize();
}
function initialize() {
let markerA = null;
let markerB = null;
let loopEnabled = false;
let loopTrackUri = null;
let loopDeviceId = null;
let lastSeekAt = 0;
let estimatedProgress = null;
let lastObservedProgress = null;
let lastProgressCheckAt = Date.now();
let loopBoundaryTimer = null;
let state = loadState();
let playbarButton = null;
injectStyles();
function loadState() {
return safeParseState(Spicetify.LocalStorage.get(STORAGE_KEY));
}
function saveState() {
Spicetify.LocalStorage.set(STORAGE_KEY, JSON.stringify(state));
}
function trackInfo() {
const item = Spicetify.Player.data?.item;
const metadata = item?.metadata || {};
return {
uri: item?.uri || null,
title: item?.name || metadata.title || metadata.name || "Current track",
artist:
metadata.artist_name ||
metadata.artist ||
metadata.artists ||
"Unknown artist",
duration: Math.max(0, Number(Spicetify.Player.getDuration()) || 0),
};
}
function notify(message, error = false) {
Spicetify.showNotification(message, error, 2500);
}
function hasUsableTrack() {
const track = trackInfo();
if (!track.uri || track.duration <= 0) {
notify("Play a seekable track before setting a loop.", true);
return false;
}
return true;
}
function stopLoop(silent = false) {
loopEnabled = false;
loopTrackUri = null;
loopDeviceId = null;
if (loopBoundaryTimer !== null) clearTimeout(loopBoundaryTimer);
loopBoundaryTimer = null;
estimatedProgress = null;
lastObservedProgress = null;
syncPlaybarButton();
if (!silent) notify("SpotLoop paused");
}
function scheduleLoopBoundary(fromPosition = markerA) {
if (loopBoundaryTimer !== null) clearTimeout(loopBoundaryTimer);
loopBoundaryTimer = null;
if (!loopEnabled || !Number.isFinite(markerA) || !Number.isFinite(markerB)) return;
const current = clamp(fromPosition, markerA, markerB);
const remaining = Math.max(50, markerB - current);
loopBoundaryTimer = setTimeout(performLoopSeek, remaining);
}
async function getActiveDeviceId() {
if (!Spicetify.CosmosAsync?.get) return null;
try {
const playback = await Spicetify.CosmosAsync.get(
"https://api.spotify.com/v1/me/player",
);
return playback?.device?.id || null;
} catch {
return null;
}
}
async function performLoopSeek() {
if (!loopEnabled) return;
const trackBeforeCheck = trackInfo();
if (playbackContextChanged(loopTrackUri, trackBeforeCheck.uri)) {
stopLoop(true);
return;
}
const activeDeviceId = await getActiveDeviceId();
if (!loopEnabled) return;
const currentTrack = trackInfo();
if (
playbackContextChanged(
loopTrackUri,
currentTrack.uri,
loopDeviceId,
activeDeviceId,
)
) {
stopLoop(true);
notify("Loop stopped because playback moved to another track or device.");
refreshOpenModal();
return;
}
if (!loopDeviceId && activeDeviceId) loopDeviceId = activeDeviceId;
lastSeekAt = Date.now();
Spicetify.Player.seek(markerA);
resetProgressClock(markerA);
scheduleLoopBoundary(markerA);
}
function resetProgressClock(position) {
estimatedProgress = Math.max(0, Number(position) || 0);
lastObservedProgress = estimatedProgress;
lastProgressCheckAt = Date.now();
}
function getLiveProgress() {
const now = Date.now();
const elapsed = Math.max(0, now - lastProgressCheckAt);
const observed = Number(Spicetify.Player.getProgress());
const observedMoved =
Number.isFinite(observed) &&
lastObservedProgress !== null &&
Math.abs(observed - lastObservedProgress) >= PROGRESS_SYNC_THRESHOLD_MS;
if (estimatedProgress === null) {
estimatedProgress = Number.isFinite(observed) ? observed : 0;
} else if (observedMoved && now - lastSeekAt >= SEEK_GUARD_MS) {
estimatedProgress = observed;
} else if (
Spicetify.Player.data?.is_paused !== true &&
Spicetify.Player.data?.isPaused !== true
) {
const speed = Math.max(0.1, Number(Spicetify.Player.data?.playback_speed) || 1);
estimatedProgress += elapsed * speed;
}
if (Number.isFinite(observed)) lastObservedProgress = observed;
lastProgressCheckAt = now;
return estimatedProgress;
}
function activateLoop({ seekToStart = true } = {}) {
if (!hasUsableTrack()) return false;
const track = trackInfo();
if (!validBounds(markerA, markerB, track.duration)) {
notify("Set A and B at least one second apart.", true);
return false;
}
loopEnabled = true;
loopTrackUri = track.uri;
loopDeviceId = null;
getActiveDeviceId().then((deviceId) => {
if (loopEnabled && loopTrackUri === trackInfo().uri && deviceId) {
loopDeviceId = deviceId;
}
});
if (seekToStart) {
lastSeekAt = Date.now();
Spicetify.Player.seek(markerA);
resetProgressClock(markerA);
} else {
resetProgressClock(Spicetify.Player.getProgress());
}
if (!Spicetify.Player.isPlaying()) Spicetify.Player.play();
scheduleLoopBoundary(markerA);
syncPlaybarButton();
notify(`Looping ${formatTime(markerA)} to ${formatTime(markerB)}`);
return true;
}
function toggleLoop() {
if (loopEnabled) stopLoop();
else activateLoop();
refreshOpenModal();
}
function setMarker(which, value = Spicetify.Player.getProgress()) {
if (!hasUsableTrack()) return;
const duration = trackInfo().duration;
const position = clamp(value, 0, duration);
if (which === "A") {
markerA = position;
if (markerB === null || markerB - markerA < MIN_LOOP_MS) {
markerB = Math.min(duration, markerA + 15000);
}
} else {
markerB = position;
if (markerA === null || markerB - markerA < MIN_LOOP_MS) {
markerA = Math.max(0, markerB - 15000);
}
}
if (!validBounds(markerA, markerB, duration)) loopEnabled = false;
if (loopEnabled) scheduleLoopBoundary(getLiveProgress());
syncPlaybarButton();
notify(`${which} set at ${formatTime(position)}`);
refreshOpenModal();
}
function loopLast(seconds = 15) {
if (!hasUsableTrack()) return;
const bounds = makeLastWindow(Spicetify.Player.getProgress(), seconds * 1000);
if (!validBounds(bounds.start, bounds.end, trackInfo().duration)) {
notify(`Play at least ${seconds} seconds first.`, true);
return;
}
markerA = bounds.start;
markerB = bounds.end;
activateLoop();
refreshOpenModal();
}
function clearMarkers() {
markerA = null;
markerB = null;
stopLoop(true);
notify("Loop markers cleared");
refreshOpenModal();
}
function saveSection(name) {
if (!hasUsableTrack()) return false;
const track = trackInfo();
if (!validBounds(markerA, markerB, track.duration)) {
notify("Set a valid loop before saving it.", true);
return false;
}
const section = {
id: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
name: String(name || "Favorite part").trim().slice(0, 60) || "Favorite part",
start: Math.round(markerA),
end: Math.round(markerB),
trackTitle: track.title,
artist: track.artist,
};
const sections = state.sectionsByTrack[track.uri] || [];
state.sectionsByTrack[track.uri] = [...sections, section];
saveState();
notify(`Saved “${section.name}”`);
return true;
}
function deleteSection(id) {
const uri = trackInfo().uri;
if (!uri) return;
state.sectionsByTrack[uri] = (state.sectionsByTrack[uri] || []).filter(
(section) => section.id !== id,
);
if (state.sectionsByTrack[uri].length === 0) delete state.sectionsByTrack[uri];
saveState();
refreshOpenModal();
}
function playSection(section) {
markerA = section.start;
markerB = section.end;
activateLoop();
refreshOpenModal();
}
function onProgress() {
if (!loopEnabled) return;
const track = trackInfo();
if (!track.uri || track.uri !== loopTrackUri) {
stopLoop(true);
return;
}
const progress = getLiveProgress();
const now = Date.now();
if (progress >= markerB && now - lastSeekAt >= SEEK_GUARD_MS) {
performLoopSeek();
}
}
function onSongChange() {
markerA = null;
markerB = null;
stopLoop(true);
refreshOpenModal();
}
function syncPlaybarButton() {
if (!playbarButton) return;
playbarButton.active = loopEnabled;
const a = markerA === null ? "A" : formatTime(markerA);
const b = markerB === null ? "B" : formatTime(markerB);
playbarButton.label = loopEnabled
? `SpotLoop active: ${a} to ${b}`
: `SpotLoop: ${a} to ${b}`;
}
function injectStyles() {
if (document.getElementById("spotloop-styles")) return;
const style = document.createElement("style");
style.id = "spotloop-styles";
style.textContent = `
.spotloop { color: var(--spice-text); min-width: 520px; max-width: 680px; }
.spotloop * { box-sizing: border-box; }
.spotloop__track { margin-bottom: 18px; }
.spotloop__eyebrow { color: var(--spice-subtext); font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.spotloop__title { font-size: 22px; font-weight: 700; margin-top: 4px; }
.spotloop__artist { color: var(--spice-subtext); font-size: 13px; margin-top: 2px; }
.spotloop__timeline { background: rgba(var(--spice-rgb-shadow), .18); border-radius: 12px; padding: 16px; }
.spotloop__times { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; margin-bottom: 10px; }
.spotloop__time { display: flex; flex-direction: column; gap: 2px; }
.spotloop__time:last-child { text-align: right; }
.spotloop__time-label { color: var(--spice-subtext); font-size: 10px; font-weight: 700; letter-spacing: .08em; }
.spotloop__time-value { font-size: 20px; font-weight: 700; }
.spotloop__range { accent-color: var(--spice-button-active); display: block; margin: 8px 0; width: 100%; }
.spotloop__actions { display: grid; gap: 8px; grid-template-columns: repeat(5, 1fr); margin-top: 14px; }
.spotloop__button { background: var(--spice-button); border: 0; border-radius: 999px; color: var(--spice-button-text); cursor: pointer; font-weight: 700; min-height: 36px; padding: 8px 12px; }
.spotloop__button:hover { filter: brightness(1.12); transform: scale(1.02); }
.spotloop__button--secondary { background: rgba(var(--spice-rgb-selected-row), .35); color: var(--spice-text); }
.spotloop__button--active { background: var(--spice-button-active); color: var(--spice-button-text); }
.spotloop__save { display: flex; gap: 8px; margin-top: 14px; }
.spotloop__input { background: var(--spice-card); border: 1px solid rgba(var(--spice-rgb-subtext), .35); border-radius: 8px; color: var(--spice-text); flex: 1; min-width: 0; padding: 10px 12px; }
.spotloop__section-title { font-size: 13px; font-weight: 700; margin: 22px 0 10px; }
.spotloop__saved { display: flex; flex-direction: column; gap: 8px; }
.spotloop__saved-row { align-items: center; background: rgba(var(--spice-rgb-selected-row), .22); border-radius: 10px; display: grid; gap: 10px; grid-template-columns: 1fr auto auto; padding: 10px 12px; }
.spotloop__saved-name { font-size: 13px; font-weight: 700; }
.spotloop__saved-time, .spotloop__empty, .spotloop__shortcuts { color: var(--spice-subtext); font-size: 12px; }
.spotloop__icon-button { background: transparent; border: 0; color: var(--spice-subtext); cursor: pointer; font-size: 16px; padding: 6px; }
.spotloop__shortcuts { border-top: 1px solid rgba(var(--spice-rgb-subtext), .2); line-height: 1.7; margin-top: 18px; padding-top: 12px; }
.spotloop kbd { background: rgba(var(--spice-rgb-selected-row), .4); border-radius: 4px; color: var(--spice-text); font-family: inherit; padding: 2px 5px; }
@media (max-width: 700px) {
.spotloop { min-width: 0; }
.spotloop__actions { grid-template-columns: 1fr 1fr; }
}
`;
document.head.appendChild(style);
}
function renderModalContent() {
const track = trackInfo();
const duration = Math.max(track.duration, 1);
const currentA = markerA === null ? 0 : markerA;
const currentB = markerB === null ? duration : markerB;
const sections = track.uri ? state.sectionsByTrack[track.uri] || [] : [];
const container = document.createElement("div");
container.className = "spotloop";
container.dataset.spotloopModal = "true";
container.innerHTML = `
<div class="spotloop__track">
<div class="spotloop__eyebrow">Now playing</div>
<div class="spotloop__title"></div>
<div class="spotloop__artist"></div>
</div>
<div class="spotloop__timeline">
<div class="spotloop__times">
<div class="spotloop__time"><span class="spotloop__time-label">START A</span><span class="spotloop__time-value" data-time="a">${formatTime(currentA)}</span></div>
<div class="spotloop__time"><span class="spotloop__time-label">END B</span><span class="spotloop__time-value" data-time="b">${formatTime(currentB)}</span></div>
</div>
<input class="spotloop__range" data-range="a" type="range" min="0" max="${duration}" step="250" value="${currentA}">
<input class="spotloop__range" data-range="b" type="range" min="0" max="${duration}" step="250" value="${currentB}">
<div class="spotloop__actions">
<button class="spotloop__button spotloop__button--secondary" data-action="set-a">Set A</button>
<button class="spotloop__button spotloop__button--secondary" data-action="set-b">Set B</button>
<button class="spotloop__button ${loopEnabled ? "spotloop__button--active" : ""}" data-action="toggle">${loopEnabled ? "Pause loop" : "Start loop"}</button>
<button class="spotloop__button spotloop__button--secondary" data-action="last-15">Last 15s</button>
<button class="spotloop__button spotloop__button--secondary" data-action="clear">Clear</button>
</div>
</div>
<div class="spotloop__save">
<input class="spotloop__input" data-section-name maxlength="60" placeholder="Name this section, e.g. Verse 2">
<button class="spotloop__button" data-action="save">Save section</button>
</div>
<div class="spotloop__section-title">Saved for this track</div>
<div class="spotloop__saved"></div>
<div class="spotloop__shortcuts">
<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>[</kbd> Set A •
<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>]</kbd> Set B •
<kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>L</kbd> Toggle loop
</div>
`;
container.querySelector(".spotloop__title").textContent = track.title;
container.querySelector(".spotloop__artist").textContent = track.artist;
const savedContainer = container.querySelector(".spotloop__saved");
if (sections.length === 0) {
const empty = document.createElement("div");
empty.className = "spotloop__empty";
empty.textContent = "No saved sections for this track yet.";
savedContainer.appendChild(empty);
} else {
sections.forEach((section) => {
const row = document.createElement("div");
row.className = "spotloop__saved-row";
const copy = document.createElement("div");
const name = document.createElement("div");
name.className = "spotloop__saved-name";
name.textContent = section.name;
const time = document.createElement("div");
time.className = "spotloop__saved-time";
time.textContent = `${formatTime(section.start)} to ${formatTime(section.end)}`;
copy.append(name, time);
const play = document.createElement("button");
play.className = "spotloop__button";
play.textContent = "Loop";
play.addEventListener("click", () => playSection(section));
const remove = document.createElement("button");
remove.className = "spotloop__icon-button";
remove.title = "Delete saved section";
remove.setAttribute("aria-label", `Delete ${section.name}`);
remove.textContent = "×";
remove.addEventListener("click", () => deleteSection(section.id));
row.append(copy, play, remove);
savedContainer.appendChild(row);
});
}
const rangeA = container.querySelector('[data-range="a"]');
const rangeB = container.querySelector('[data-range="b"]');
const timeA = container.querySelector('[data-time="a"]');
const timeB = container.querySelector('[data-time="b"]');
rangeA.addEventListener("input", () => {
markerA = Math.min(Number(rangeA.value), Number(rangeB.value) - MIN_LOOP_MS);
markerA = Math.max(0, markerA);
rangeA.value = String(markerA);
timeA.textContent = formatTime(markerA);
if (loopEnabled && !validBounds(markerA, markerB, track.duration)) stopLoop(true);
else if (loopEnabled) scheduleLoopBoundary(getLiveProgress());
syncPlaybarButton();
});
rangeB.addEventListener("input", () => {
markerB = Math.max(Number(rangeB.value), Number(rangeA.value) + MIN_LOOP_MS);
markerB = Math.min(track.duration, markerB);
rangeB.value = String(markerB);
timeB.textContent = formatTime(markerB);
if (loopEnabled && !validBounds(markerA, markerB, track.duration)) stopLoop(true);
else if (loopEnabled) scheduleLoopBoundary(getLiveProgress());
syncPlaybarButton();
});
container.querySelector('[data-action="set-a"]').addEventListener("click", () => setMarker("A"));
container.querySelector('[data-action="set-b"]').addEventListener("click", () => setMarker("B"));
container.querySelector('[data-action="toggle"]').addEventListener("click", toggleLoop);
container.querySelector('[data-action="last-15"]').addEventListener("click", () => loopLast(15));
container.querySelector('[data-action="clear"]').addEventListener("click", clearMarkers);
container.querySelector('[data-action="save"]').addEventListener("click", () => {
const input = container.querySelector("[data-section-name]");
if (saveSection(input.value)) refreshOpenModal();
});
container.querySelector("[data-section-name]").addEventListener("keydown", (event) => {
if (event.key === "Enter" && saveSection(event.currentTarget.value)) refreshOpenModal();
});
return container;
}
function openModal() {
Spicetify.PopupModal.display({
title: `SpotLoop v${VERSION}`,
content: renderModalContent(),
isLarge: true,
});
}
function refreshOpenModal() {
if (!document.querySelector('[data-spotloop-modal="true"]')) return;
openModal();
}
playbarButton = new Spicetify.Playbar.Button(
"SpotLoop",
"repeat",
openModal,
false,
false,
);
playbarButton.element.addEventListener("contextmenu", (event) => {
event.preventDefault();
toggleLoop();
});
syncPlaybarButton();
Spicetify.Keyboard.registerShortcut("ctrl+shift+[", () => setMarker("A"));
Spicetify.Keyboard.registerShortcut("ctrl+shift+]", () => setMarker("B"));
Spicetify.Keyboard.registerShortcut("ctrl+shift+l", toggleLoop);
Spicetify.Player.addEventListener("onprogress", onProgress);
Spicetify.Player.addEventListener("songchange", onSongChange);
setInterval(onProgress, LOOP_POLL_MS);
console.log(`[SpotLoop] Loaded v${VERSION}`);
}
waitForSpicetify();
})();