-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
79 lines (67 loc) · 2.86 KB
/
script.js
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
const RSS_FEED_URL = "https://anchor.fm/s/db498af0/podcast/rss";
// Select DOM elements
const descriptionEl = document.getElementById("podcast-description");
const episodeListEl = document.getElementById("episode-list");
const playerTitleEl = document.getElementById("player-title");
const playerAudioEl = document.getElementById("player-audio");
const toggleButton = document.getElementById("theme-toggle");
const stickyPlayer = document.getElementById("audio-player");
// Load podcast data
async function loadPodcastData() {
try {
const response = await fetch(RSS_FEED_URL);
const rssText = await response.text();
const parser = new DOMParser();
const rss = parser.parseFromString(rssText, "application/xml");
// Podcast description
const channel = rss.querySelector("channel");
const description = channel.querySelector("description").textContent;
descriptionEl.textContent = description;
// Podcast episodes (Reversed order - latest first)
const items = channel.querySelectorAll("item");
const episodes = Array.from(items).map((item, index) => ({
number: items.length - index,
title: item.querySelector("title").textContent,
description: item.querySelector("description").textContent,
audioUrl: item.querySelector("enclosure").getAttribute("url"),
coverArt: item.querySelector("itunes\\:image, image")?.getAttribute("href") || "",
}));
renderEpisodes(episodes);
} catch (error) {
console.error("Error loading podcast data:", error);
}
}
// Render episodes
function renderEpisodes(episodes) {
episodeListEl.innerHTML = "";
episodes.forEach((episode) => {
const episodeEl = document.createElement("div");
episodeEl.className = "episode";
episodeEl.innerHTML = `
<img src="${episode.coverArt}" alt="Episode Cover Art">
<h3>${episode.number}. ${episode.title}</h3>
<p>${episode.description}</p>
<button onclick="playEpisode('${episode.title}', '${episode.audioUrl}')">Play</button>
`;
episodeListEl.appendChild(episodeEl);
});
}
// Play an episode
function playEpisode(title, audioUrl) {
playerTitleEl.textContent = title;
playerAudioEl.src = audioUrl;
playerAudioEl.play();
// Show the sticky player
stickyPlayer.style.bottom = "0";
}
// Toggle dark mode
toggleButton.addEventListener("click", () => {
document.body.classList.toggle("dark-mode");
document.body.classList.toggle("light-mode");
});
// Hide sticky player when audio ends
playerAudioEl.addEventListener("ended", () => {
stickyPlayer.style.bottom = "-100%";
});
// Load data on page load
document.addEventListener("DOMContentLoaded", loadPodcastData);