@dudgy/podcast-widget 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -198,10 +198,11 @@ var PlaybackStorage = class {
198
198
  } catch {
199
199
  }
200
200
  }
201
- cleanupPositions(currentEpisodeIds) {
201
+ /** Remove saved positions for episodes whose IDs aren't in `keepIds`. */
202
+ cleanupPositions(keepIds) {
202
203
  try {
203
204
  const positions = this.getPositions();
204
- const currentSet = new Set(currentEpisodeIds);
205
+ const currentSet = new Set(keepIds);
205
206
  let changed = false;
206
207
  for (const id of Object.keys(positions)) {
207
208
  if (!currentSet.has(id)) {
@@ -217,6 +218,19 @@ var PlaybackStorage = class {
217
218
  }
218
219
  };
219
220
 
221
+ // src/core/types.ts
222
+ var PLAYER_STATE_VERSION = 2;
223
+ var DEFAULT_CONFIG = {
224
+ storagePrefix: "podcast",
225
+ maxStoredPositions: 100,
226
+ positionSaveInterval: 5e3,
227
+ skipSeconds: 30,
228
+ episodeCacheTTL: 60 * 60 * 1e3
229
+ };
230
+ function resolveConfig(config) {
231
+ return { ...DEFAULT_CONFIG, ...config };
232
+ }
233
+
220
234
  // src/core/player-state.ts
221
235
  var PlayerStateStorage = class {
222
236
  stateKey;
@@ -231,11 +245,22 @@ var PlayerStateStorage = class {
231
245
  } catch {
232
246
  }
233
247
  }
248
+ /**
249
+ * Load saved state, migrating older formats. Returns null if nothing usable
250
+ * is saved.
251
+ *
252
+ * Unversioned (v1) state stored only an index into the episode list, which
253
+ * can't be trusted once the list changes, so only its volume is kept.
254
+ */
234
255
  loadState() {
235
256
  try {
236
257
  const raw = localStorage.getItem(this.stateKey);
237
258
  if (!raw) return null;
238
- return JSON.parse(raw);
259
+ const data = JSON.parse(raw);
260
+ if (!data || typeof data !== "object") return null;
261
+ const volume = isVolume(data.volume) ? data.volume : 1;
262
+ const episode = data.v === PLAYER_STATE_VERSION && isEpisode(data.episode) ? data.episode : null;
263
+ return { v: PLAYER_STATE_VERSION, episode, volume };
239
264
  } catch {
240
265
  return null;
241
266
  }
@@ -257,6 +282,14 @@ var PlayerStateStorage = class {
257
282
  }
258
283
  }
259
284
  };
285
+ function isVolume(v) {
286
+ return typeof v === "number" && v >= 0 && v <= 1;
287
+ }
288
+ function isEpisode(e) {
289
+ if (!e || typeof e !== "object") return false;
290
+ const ep = e;
291
+ return typeof ep.id === "string" && typeof ep.audioUrl === "string" && typeof ep.title === "string" && typeof ep.feedName === "string";
292
+ }
260
293
 
261
294
  // src/core/episode-provider.ts
262
295
  var FetchEpisodeProvider = class {
@@ -314,18 +347,6 @@ var FetchEpisodeProvider = class {
314
347
  }
315
348
  };
316
349
 
317
- // src/core/types.ts
318
- var DEFAULT_CONFIG = {
319
- storagePrefix: "podcast",
320
- maxStoredPositions: 100,
321
- positionSaveInterval: 5e3,
322
- skipSeconds: 30,
323
- episodeCacheTTL: 60 * 60 * 1e3
324
- };
325
- function resolveConfig(config) {
326
- return { ...DEFAULT_CONFIG, ...config };
327
- }
328
-
329
350
  // src/ui/utils.ts
330
351
  function formatTime(seconds) {
331
352
  if (isNaN(seconds) || seconds < 0) return "00:00";
@@ -368,19 +389,75 @@ function escapeHtml(str) {
368
389
  function getEpisodeImage(episode) {
369
390
  return episode.image || episode.feedImage || episode.artwork || "";
370
391
  }
392
+ function setArtwork(img, url) {
393
+ if (!img) return;
394
+ img.hidden = !url;
395
+ if (url) img.src = url;
396
+ else img.removeAttribute("src");
397
+ }
398
+ function hideBrokenImages(root) {
399
+ const onError = (e) => {
400
+ const t = e.target;
401
+ if (t instanceof HTMLImageElement && t.parentElement?.classList.contains("pw-art")) t.hidden = true;
402
+ };
403
+ root.addEventListener("error", onError, true);
404
+ return () => root.removeEventListener("error", onError, true);
405
+ }
406
+ function attachSeekTooltip(bar, getDuration) {
407
+ const tip = document.createElement("div");
408
+ tip.className = "pw-seek-tooltip";
409
+ tip.setAttribute("aria-hidden", "true");
410
+ tip.hidden = true;
411
+ bar.appendChild(tip);
412
+ const onMove = (e) => {
413
+ const duration = getDuration();
414
+ if (e.pointerType === "touch" || !(duration > 0)) {
415
+ tip.hidden = true;
416
+ return;
417
+ }
418
+ const rect = bar.getBoundingClientRect();
419
+ const frac = Math.min(1, Math.max(0, (e.clientX - rect.left) / rect.width));
420
+ tip.textContent = formatTime(frac * duration);
421
+ tip.hidden = false;
422
+ const half = tip.offsetWidth / 2;
423
+ tip.style.left = `${Math.min(rect.width - half, Math.max(half, frac * rect.width))}px`;
424
+ };
425
+ const onLeave = () => {
426
+ tip.hidden = true;
427
+ };
428
+ bar.addEventListener("pointermove", onMove);
429
+ bar.addEventListener("pointerleave", onLeave);
430
+ return () => {
431
+ bar.removeEventListener("pointermove", onMove);
432
+ bar.removeEventListener("pointerleave", onLeave);
433
+ tip.remove();
434
+ };
435
+ }
371
436
 
372
437
  // src/ui/html-templates.ts
373
438
  var ICONS = {
374
439
  play: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M8 5v14l11-7z"/></svg>',
375
440
  pause: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"/></svg>',
376
- skipBack: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M11.99 5V1l-5 5 5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6h-2c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8zm-1.1 11h-.85v-3.26l-1.01.31v-.69l1.77-.63h.09V16zm4.28-1.76c0 .32-.03.6-.1.82s-.17.42-.29.57-.28.26-.45.33-.37.1-.59.10-.41-.03-.59-.1-.33-.18-.46-.33-.23-.34-.3-.57-.11-.5-.11-.82v-.74c0-.32.03-.6.1-.82s.17-.42.29-.57.28-.26.45-.33.37-.1.59-.10.41.03.59.1.33.18.46.33.23.34.3.57.11.5.11.82v.74zm-.85-.86c0-.19-.01-.35-.04-.48s-.07-.23-.12-.31-.11-.14-.19-.17-.16-.05-.25-.05-.18.02-.25.05-.14.09-.19.17-.09.18-.12.31-.04.29-.04.48v.97c0 .19.01.35.04.48s.07.24.12.32.11.14.19.17.16.05.25.05.18-.02.25-.05.14-.09.19-.17.09-.19.11-.32.04-.29.04-.48v-.97z"/></svg>',
377
- skipForward: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M18 13c0 3.31-2.69 6-6 6s-6-2.69-6-6 2.69-6 6-6v4l5-5-5-5v4c-4.42 0-8 3.58-8 8s3.58 8 8 8 8-3.58 8-8h-2zm-7.46 2.22v-.67c0-.31.02-.58.07-.8.05-.22.12-.42.22-.58.1-.16.22-.28.37-.37.14-.08.3-.12.5-.12.18 0 .35.04.49.12.14.08.27.2.37.37.1.16.17.36.22.58s.08.49.08.8v.67c0 .31-.03.58-.08.8-.05.22-.12.42-.22.58-.1.16-.23.28-.37.37-.14.08-.3.12-.49.12s-.36-.04-.5-.12c-.15-.08-.27-.2-.37-.37-.1-.16-.17-.36-.22-.58s-.07-.49-.07-.8zm.85.67c0 .25.04.45.11.59.07.14.2.21.38.21s.31-.07.38-.21c.07-.14.11-.34.11-.59v-.97c0-.25-.04-.45-.11-.59s-.2-.21-.38-.21-.31.07-.38.21c-.07.14-.11.34-.11.59v.97zm2.68-.87c0-.11.01-.21.04-.29.03-.08.06-.15.11-.2.05-.05.11-.09.18-.11s.14-.04.23-.04c.12 0 .22.02.3.07s.16.12.21.2.1.18.13.3c.03.11.05.24.05.38 0 .14-.02.27-.05.38-.03.11-.08.21-.13.3-.05.08-.12.15-.21.2s-.18.07-.3.07c-.09 0-.17-.01-.23-.04s-.13-.06-.18-.11c-.05-.05-.08-.12-.11-.2-.03-.08-.04-.18-.04-.29h-.01v-.62z"/></svg>',
441
+ // Plain circular arrows; the seconds are drawn as text on top (see renderSkipButton)
442
+ skipBack: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"/></svg>',
443
+ skipForward: '<svg viewBox="0 0 24 24" fill="currentColor"><path transform="matrix(-1 0 0 1 24 0)" d="M12 5V1L7 6l5 5V7c3.31 0 6 2.69 6 6s-2.69 6-6 6-6-2.69-6-6H4c0 4.42 3.58 8 8 8s8-3.58 8-8-3.58-8-8-8z"/></svg>',
378
444
  expand: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/></svg>',
379
445
  close: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"/></svg>',
380
446
  volume: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02z"/></svg>',
381
447
  info: '<svg viewBox="0 0 24 24" fill="currentColor" width="16" height="16"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"/></svg>',
382
- playlist: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z"/></svg>'
448
+ playlist: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7 7v2h14V7H7z"/></svg>',
449
+ note: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M12 3v10.55c-.59-.34-1.27-.55-2-.55-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4V7h4V3h-6z"/></svg>',
450
+ download: '<svg viewBox="0 0 24 24" fill="currentColor"><path d="M19 9h-4V3H9v6H5l7 7 7-7zM5 18v2h14v-2H5z"/></svg>'
383
451
  };
452
+ function renderArtwork(className, imageUrl) {
453
+ const img = imageUrl ? `<img src="${escapeHtml(imageUrl)}" alt="">` : '<img alt="" hidden>';
454
+ return `<span class="pw-art ${className}">${img}${ICONS.note}</span>`;
455
+ }
456
+ function renderSkipButton(direction, seconds, extraClass = "") {
457
+ const label = `${direction === "back" ? "Back" : "Forward"} ${seconds} seconds`;
458
+ const icon = direction === "back" ? ICONS.skipBack : ICONS.skipForward;
459
+ return `<button class="pw-btn pw-btn--skip pw-btn--skip-${direction} ${extraClass}" aria-label="${label}">${icon}<span class="pw-skip-label" aria-hidden="true">${seconds}</span></button>`;
460
+ }
384
461
  var SPEED_OPTIONS = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3];
385
462
  function renderPlaylistItem(episode, index, opts) {
386
463
  const imageUrl = getEpisodeImage(episode);
@@ -396,7 +473,7 @@ function renderPlaylistItem(episode, index, opts) {
396
473
  const feedLinkHtml = opts.feedPageUrl ? `<a href="${opts.feedPageUrl(episode.feedId)}" class="pw-view-feed" title="View show info" data-feed-link>${ICONS.info}</a>` : "";
397
474
  return `
398
475
  <div class="pw-playlist-item ${opts.isPlaying ? "pw-playing" : ""} ${hasSaved ? "pw-has-saved" : ""}" data-index="${index}">
399
- ${imageUrl ? `<img src="${escapeHtml(imageUrl)}" alt="${escapeHtml(episode.title)}" class="pw-episode-image">` : ""}
476
+ ${renderArtwork("pw-episode-image", imageUrl)}
400
477
  <div class="pw-episode-info">
401
478
  <div class="pw-episode-show">${escapeHtml(episode.feedName || "Unknown Show")}</div>
402
479
  <div class="pw-episode-title">${escapeHtml(episode.title)}</div>
@@ -405,6 +482,7 @@ function renderPlaylistItem(episode, index, opts) {
405
482
  </div>
406
483
  <div class="pw-episode-actions">
407
484
  <div class="pw-episode-duration">${escapeHtml(duration)}</div>
485
+ <a href="${escapeHtml(episode.audioUrl)}" download="${escapeHtml(episode.title)}" target="_blank" class="pw-download-btn" title="Download episode" data-download>${ICONS.download}</a>
408
486
  ${feedLinkHtml}
409
487
  </div>
410
488
  </div>
@@ -431,6 +509,9 @@ var PlayerController = class extends EventEmitter {
431
509
  config;
432
510
  provider;
433
511
  episodes = [];
512
+ /** The loaded episode. May not be in `episodes` (e.g. restored after its feed moved on). */
513
+ currentEpisode = null;
514
+ /** Index of `currentEpisode` in `episodes`, or -1 if it isn't in the list. */
434
515
  currentIndex = -1;
435
516
  saveInterval = null;
436
517
  cleanupFns = [];
@@ -450,8 +531,8 @@ var PlayerController = class extends EventEmitter {
450
531
  this.engine.volume = savedState.volume;
451
532
  }
452
533
  this.engine.on("ended", () => {
453
- if (this.currentIndex >= 0) {
454
- this.storage.removePosition(this.episodes[this.currentIndex].id);
534
+ if (this.currentEpisode) {
535
+ this.storage.removePosition(this.currentEpisode.id);
455
536
  }
456
537
  this.stopPositionSaving();
457
538
  });
@@ -465,11 +546,17 @@ var PlayerController = class extends EventEmitter {
465
546
  // ─── Episode management ────────────────────────────────────────
466
547
  async loadEpisodes() {
467
548
  this.episodes = await this.provider.getEpisodes();
468
- this.storage.cleanupPositions(this.episodes.map((ep) => ep.id));
549
+ const saved = this.stateStorage.loadState()?.episode ?? null;
550
+ const keepIds = this.episodes.map((ep) => ep.id);
551
+ if (saved) keepIds.push(saved.id);
552
+ this.storage.cleanupPositions(keepIds);
469
553
  this.emit("episodes-loaded", { episodes: this.episodes });
470
- const savedState = this.stateStorage.loadState();
471
- if (savedState && savedState.currentEpisodeIndex >= 0 && savedState.currentEpisodeIndex < this.episodes.length) {
472
- this.loadEpisode(savedState.currentEpisodeIndex, false);
554
+ if (saved) {
555
+ const index = this.episodes.findIndex((ep) => ep.id === saved.id);
556
+ if (index >= 0) this.loadEpisode(index, false);
557
+ else this.select(saved, -1, false);
558
+ } else if (this.episodes.length > 0) {
559
+ this.loadEpisode(0, false);
473
560
  }
474
561
  return this.episodes;
475
562
  }
@@ -477,21 +564,22 @@ var PlayerController = class extends EventEmitter {
477
564
  return this.episodes;
478
565
  }
479
566
  getCurrentEpisode() {
480
- return this.currentIndex >= 0 ? this.episodes[this.currentIndex] : null;
567
+ return this.currentEpisode;
481
568
  }
569
+ /** Index of the current episode in the list, or -1 if none or not listed. */
482
570
  getCurrentIndex() {
483
571
  return this.currentIndex;
484
572
  }
485
573
  loadEpisode(index, autoPlay = false) {
486
574
  if (index < 0 || index >= this.episodes.length) return;
487
- const episode = this.episodes[index];
575
+ this.select(this.episodes[index], index, autoPlay);
576
+ }
577
+ select(episode, index, autoPlay) {
578
+ this.currentEpisode = episode;
488
579
  this.currentIndex = index;
489
580
  const savedPosition = this.storage.getPosition(episode.id);
490
581
  this.engine.load(episode.audioUrl, savedPosition ?? void 0);
491
- this.stateStorage.saveState({
492
- currentEpisodeIndex: this.currentIndex,
493
- volume: this.engine.volume
494
- });
582
+ this.persistState();
495
583
  this.emit("episode-change", { episode, index });
496
584
  this.emit("episode-display", {
497
585
  imageUrl: getEpisodeImage(episode),
@@ -506,9 +594,8 @@ var PlayerController = class extends EventEmitter {
506
594
  this.loadEpisode(index, true);
507
595
  }
508
596
  async play() {
509
- if (this.episodes.length === 0) return;
510
- if (this.currentIndex === -1) {
511
- this.playEpisode(0);
597
+ if (!this.currentEpisode) {
598
+ if (this.episodes.length > 0) this.playEpisode(0);
512
599
  return;
513
600
  }
514
601
  await this.engine.play();
@@ -524,9 +611,13 @@ var PlayerController = class extends EventEmitter {
524
611
  }
525
612
  setVolume(v) {
526
613
  this.engine.volume = v;
614
+ this.persistState();
615
+ }
616
+ persistState() {
527
617
  this.stateStorage.saveState({
528
- currentEpisodeIndex: this.currentIndex,
529
- volume: v
618
+ v: PLAYER_STATE_VERSION,
619
+ episode: this.currentEpisode,
620
+ volume: this.engine.volume
530
621
  });
531
622
  }
532
623
  getPlaylistHtml(feedPageUrl) {
@@ -552,8 +643,8 @@ var PlayerController = class extends EventEmitter {
552
643
  }
553
644
  }
554
645
  saveCurrentPosition() {
555
- if (this.currentIndex < 0 || !this.engine.hasSrc) return;
556
- const episode = this.episodes[this.currentIndex];
646
+ const episode = this.currentEpisode;
647
+ if (!episode || !this.engine.hasSrc) return;
557
648
  if (this.engine.currentTime > 10) {
558
649
  this.storage.savePosition(episode.id, this.engine.currentTime, episode.duration);
559
650
  }
@@ -602,16 +693,16 @@ var MiniPlayer = class extends EventEmitter {
602
693
  const c = this.controls;
603
694
  let controlsHtml = "";
604
695
  if (c.skipBackward) {
605
- controlsHtml += `<button class="pw-btn pw-btn--mini pw-btn--skip-back" aria-label="Back ${this.controller.config.skipSeconds} seconds">${ICONS.skipBack}</button>`;
696
+ controlsHtml += renderSkipButton("back", this.controller.config.skipSeconds, "pw-btn--mini");
606
697
  }
607
698
  if (c.playPause) {
608
699
  controlsHtml += `
609
- <button class="pw-btn pw-btn--mini pw-play-btn" aria-label="Play">${ICONS.play}</button>
610
- <button class="pw-btn pw-btn--mini pw-pause-btn" aria-label="Pause" style="display:none">${ICONS.pause}</button>
700
+ <button class="pw-btn pw-btn--mini pw-btn--play pw-play-btn" aria-label="Play">${ICONS.play}</button>
701
+ <button class="pw-btn pw-btn--mini pw-btn--play pw-pause-btn" aria-label="Pause" style="display:none">${ICONS.pause}</button>
611
702
  `;
612
703
  }
613
704
  if (c.skipForward) {
614
- controlsHtml += `<button class="pw-btn pw-btn--mini pw-btn--skip-fwd" aria-label="Forward ${this.controller.config.skipSeconds} seconds">${ICONS.skipForward}</button>`;
705
+ controlsHtml += renderSkipButton("fwd", this.controller.config.skipSeconds, "pw-btn--mini");
615
706
  }
616
707
  if (c.volume) {
617
708
  controlsHtml += `
@@ -637,7 +728,7 @@ var MiniPlayer = class extends EventEmitter {
637
728
  const progressHtml = c.progress ? `<div class="pw-mini-progress pw-progress-container" role="slider" aria-label="Seek" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"><div class="pw-mini-progress-bar"></div></div>` : "";
638
729
  this.container.innerHTML = `
639
730
  <div class="pw-mini">
640
- <img class="pw-mini-artwork" src="" alt="Podcast artwork" style="display:none">
731
+ ${renderArtwork("pw-mini-artwork")}
641
732
  <div class="pw-mini-info">
642
733
  <div class="pw-mini-title">No episode playing</div>
643
734
  <div class="pw-mini-show"></div>
@@ -647,7 +738,7 @@ var MiniPlayer = class extends EventEmitter {
647
738
  </div>
648
739
  ${c.playlist ? '<div class="pw-mini-playlist" style="display:none"></div>' : ""}
649
740
  `;
650
- this.artworkEl = this.container.querySelector(".pw-mini-artwork");
741
+ this.artworkEl = this.container.querySelector(".pw-mini-artwork img");
651
742
  this.titleEl = this.container.querySelector(".pw-mini-title");
652
743
  this.showEl = this.container.querySelector(".pw-mini-show");
653
744
  this.playBtn = this.container.querySelector(".pw-play-btn");
@@ -658,6 +749,7 @@ var MiniPlayer = class extends EventEmitter {
658
749
  }
659
750
  bindEvents() {
660
751
  const { engine } = this.controller;
752
+ this.cleanupFns.push(hideBrokenImages(this.container));
661
753
  this.addClick(".pw-play-btn", () => this.controller.play());
662
754
  this.addClick(".pw-pause-btn", () => this.controller.pause());
663
755
  this.addClick(".pw-btn--skip-back", () => engine.skipBackward());
@@ -674,6 +766,7 @@ var MiniPlayer = class extends EventEmitter {
674
766
  };
675
767
  this.progressContainer.addEventListener("click", handler);
676
768
  this.cleanupFns.push(() => this.progressContainer?.removeEventListener("click", handler));
769
+ this.cleanupFns.push(attachSeekTooltip(this.progressContainer, () => engine.duration));
677
770
  }
678
771
  const onTimeUpdate = (d) => {
679
772
  if (this.progressBar) {
@@ -717,14 +810,7 @@ var MiniPlayer = class extends EventEmitter {
717
810
  const imageUrl = getEpisodeImage(episode);
718
811
  if (this.titleEl) this.titleEl.textContent = episode.title;
719
812
  if (this.showEl) this.showEl.textContent = episode.feedName;
720
- if (this.artworkEl) {
721
- if (imageUrl) {
722
- this.artworkEl.src = imageUrl;
723
- this.artworkEl.style.display = "block";
724
- } else {
725
- this.artworkEl.style.display = "none";
726
- }
727
- }
813
+ setArtwork(this.artworkEl, imageUrl);
728
814
  }
729
815
  updatePlayPauseState(playing) {
730
816
  if (this.playBtn) this.playBtn.style.display = playing ? "none" : "flex";
@@ -815,6 +901,7 @@ var FullPlayer = class extends EventEmitter {
815
901
  speed: true,
816
902
  progress: true,
817
903
  playlist: true,
904
+ download: true,
818
905
  ...opts.controls
819
906
  };
820
907
  this.feedPageUrl = opts.feedPageUrl;
@@ -842,7 +929,7 @@ var FullPlayer = class extends EventEmitter {
842
929
  const c = this.controls;
843
930
  let mainControlsHtml = "";
844
931
  if (c.skipBackward) {
845
- mainControlsHtml += `<button class="pw-btn pw-btn--skip pw-btn--skip-back" aria-label="Back ${this.controller.config.skipSeconds} seconds">${ICONS.skipBack}</button>`;
932
+ mainControlsHtml += renderSkipButton("back", this.controller.config.skipSeconds);
846
933
  }
847
934
  if (c.playPause) {
848
935
  mainControlsHtml += `
@@ -851,7 +938,7 @@ var FullPlayer = class extends EventEmitter {
851
938
  `;
852
939
  }
853
940
  if (c.skipForward) {
854
- mainControlsHtml += `<button class="pw-btn pw-btn--skip pw-btn--skip-fwd" aria-label="Forward ${this.controller.config.skipSeconds} seconds">${ICONS.skipForward}</button>`;
941
+ mainControlsHtml += renderSkipButton("fwd", this.controller.config.skipSeconds);
855
942
  }
856
943
  let secondaryHtml = "";
857
944
  if (c.volume) {
@@ -869,6 +956,9 @@ var FullPlayer = class extends EventEmitter {
869
956
  ).join("");
870
957
  secondaryHtml += `<select class="pw-speed-select" aria-label="Playback speed">${options}</select>`;
871
958
  }
959
+ if (c.download) {
960
+ secondaryHtml += `<button class="pw-btn pw-btn--download" aria-label="Download episode">${ICONS.download}</button>`;
961
+ }
872
962
  const progressHtml = c.progress ? `<div class="pw-progress pw-progress-container" role="slider" aria-label="Seek" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"><div class="pw-progress-bar"></div></div>` : "";
873
963
  const playlistHtml = c.playlist ? `<div class="pw-playlist"><div class="pw-playlist-header">Latest Episodes</div><div class="pw-playlist-items"><div class="pw-playlist-item pw-loading">Loading episodes...</div></div></div>` : "";
874
964
  this.container.innerHTML = `
@@ -876,7 +966,7 @@ var FullPlayer = class extends EventEmitter {
876
966
  <div class="pw-hero-bg"></div>
877
967
  <button class="pw-btn-close" aria-label="Minimize player">${ICONS.close}</button>
878
968
  <div class="pw-now-playing">
879
- <img class="pw-artwork" src="" alt="Podcast artwork" style="display:none">
969
+ ${renderArtwork("pw-artwork")}
880
970
  <div class="pw-track-info">
881
971
  <div class="pw-track-name" aria-live="polite">Select an episode to play</div>
882
972
  <div class="pw-track-time" aria-live="off">00:00 / 00:00</div>
@@ -890,7 +980,7 @@ var FullPlayer = class extends EventEmitter {
890
980
  </div>
891
981
  ${playlistHtml}
892
982
  `;
893
- this.artworkEl = this.container.querySelector(".pw-artwork");
983
+ this.artworkEl = this.container.querySelector(".pw-artwork img");
894
984
  this.trackNameEl = this.container.querySelector(".pw-track-name");
895
985
  this.trackTimeEl = this.container.querySelector(".pw-track-time");
896
986
  this.playBtn = this.container.querySelector(".pw-play-btn");
@@ -902,6 +992,13 @@ var FullPlayer = class extends EventEmitter {
902
992
  }
903
993
  bindEvents() {
904
994
  const { engine } = this.controller;
995
+ this.cleanupFns.push(hideBrokenImages(this.container));
996
+ const art = this.artworkEl;
997
+ if (art) {
998
+ const onArtLoad = () => this.setHeroBackground(art.currentSrc || art.src);
999
+ art.addEventListener("load", onArtLoad);
1000
+ this.cleanupFns.push(() => art.removeEventListener("load", onArtLoad));
1001
+ }
905
1002
  this.addClick(".pw-btn-close", () => this.hide());
906
1003
  if (this.backdrop) {
907
1004
  const handler = () => this.hide();
@@ -919,6 +1016,7 @@ var FullPlayer = class extends EventEmitter {
919
1016
  this.addClick(".pw-btn--skip-fwd", () => engine.skipForward());
920
1017
  this.addInput(".pw-volume-slider", (val) => this.controller.setVolume(parseInt(val) / 100));
921
1018
  this.addChange(".pw-speed-select", (val) => this.controller.setSpeed(parseFloat(val)));
1019
+ this.addClick(".pw-btn--download", () => this.downloadCurrentEpisode());
922
1020
  if (this.progressContainer) {
923
1021
  const handler = (e) => {
924
1022
  const rect = this.progressContainer.getBoundingClientRect();
@@ -927,6 +1025,7 @@ var FullPlayer = class extends EventEmitter {
927
1025
  };
928
1026
  this.progressContainer.addEventListener("click", handler);
929
1027
  this.cleanupFns.push(() => this.progressContainer?.removeEventListener("click", handler));
1028
+ this.cleanupFns.push(attachSeekTooltip(this.progressContainer, () => engine.duration));
930
1029
  }
931
1030
  const onKeyDown = (e) => {
932
1031
  if (!this.isVisible) return;
@@ -994,23 +1093,13 @@ var FullPlayer = class extends EventEmitter {
994
1093
  if (this.trackNameEl) {
995
1094
  this.trackNameEl.textContent = `${episode.feedName}: ${episode.title}`;
996
1095
  }
997
- if (this.artworkEl) {
998
- if (imageUrl) {
999
- this.artworkEl.src = imageUrl;
1000
- this.artworkEl.style.display = "block";
1001
- } else {
1002
- this.artworkEl.style.display = "none";
1003
- }
1004
- }
1005
- if (this.heroBg) {
1006
- if (imageUrl) {
1007
- this.heroBg.style.backgroundImage = `url("${imageUrl}")`;
1008
- this.heroBg.classList.add("pw-active");
1009
- } else {
1010
- this.heroBg.classList.remove("pw-active");
1011
- this.heroBg.style.backgroundImage = "";
1012
- }
1013
- }
1096
+ this.setHeroBackground("");
1097
+ setArtwork(this.artworkEl, imageUrl);
1098
+ }
1099
+ setHeroBackground(url) {
1100
+ if (!this.heroBg) return;
1101
+ this.heroBg.style.backgroundImage = url ? `url("${url.replace(/"/g, "%22")}")` : "";
1102
+ this.heroBg.classList.toggle("pw-active", !!url);
1014
1103
  }
1015
1104
  updatePlayPauseState(playing) {
1016
1105
  if (this.playBtn) this.playBtn.style.display = playing ? "none" : "flex";
@@ -1025,7 +1114,7 @@ var FullPlayer = class extends EventEmitter {
1025
1114
  if (!this.playlistEl) return;
1026
1115
  this.playlistEl.querySelectorAll(".pw-playlist-item").forEach((item) => {
1027
1116
  item.addEventListener("click", (e) => {
1028
- if (e.target.closest("[data-feed-link]")) return;
1117
+ if (e.target.closest("[data-feed-link]") || e.target.closest("[data-download]")) return;
1029
1118
  const index = parseInt(item.dataset.index || "0");
1030
1119
  this.controller.playEpisode(index);
1031
1120
  });
@@ -1037,6 +1126,18 @@ var FullPlayer = class extends EventEmitter {
1037
1126
  item.classList.toggle("pw-playing", i === index);
1038
1127
  });
1039
1128
  }
1129
+ // ─── Download ────────────────────────────────────────────────
1130
+ downloadCurrentEpisode() {
1131
+ const episode = this.controller.getCurrentEpisode();
1132
+ if (!episode) return;
1133
+ const a = document.createElement("a");
1134
+ a.href = episode.audioUrl;
1135
+ a.target = "_blank";
1136
+ a.download = episode.title || "episode";
1137
+ document.body.appendChild(a);
1138
+ a.click();
1139
+ document.body.removeChild(a);
1140
+ }
1040
1141
  // ─── Helpers ───────────────────────────────────────────────────
1041
1142
  addClick(selector, handler) {
1042
1143
  const el = this.container.querySelector(selector);
@@ -1073,6 +1174,7 @@ export {
1073
1174
  FullPlayer,
1074
1175
  ICONS,
1075
1176
  MiniPlayer,
1177
+ PLAYER_STATE_VERSION,
1076
1178
  PlaybackStorage,
1077
1179
  PlayerController,
1078
1180
  PlayerStateStorage,