···
<script type="text/javascript">
77
+
// We'll store the last title of the current mount point to check for changes
79
+
// Store the current mount point
80
+
let currentMount = ''
81
+
// Store the last notification
82
+
let lastNotification = null
84
+
// On every audio element, create a new Plyr instance
85
+
document.querySelectorAll('div[data-mount]').forEach((e) => {
86
+
const plyr = new Plyr(
87
+
e.querySelector('audio'),
89
+
controls: ['play', 'current-time', 'mute', 'volume'],
91
+
toggleInvert: false,
95
+
// When we start playing, store current mount point, clear last title and stop all other players
99
+
currentMount = e.dataset.mount
102
+
document.querySelectorAll('[data-mount]').forEach((a) => {
104
+
a.querySelector('audio').stop()
110
+
// When we pause, clear current mount point and stop all data
114
+
// To prevent overlaps, we only clear the current mount if it's the same as the one we're pausing
115
+
if (currentMount === e.dataset.mount) {
78
-
document.querySelectorAll('div[data-mount]').forEach(e => {
125
+
// Find all mount points and fetch their status
126
+
document.querySelectorAll('div[data-mount]').forEach((e) => {
fetch('./status-json.xsl?mount=' + e.dataset.mount)
80
-
.then(r => r.json())
128
+
.then((r) => r.json())
130
+
// We delay the update to match the usual delay of the audio stream
131
+
setTimeout((_) => {
132
+
// The title may not be present, so we check for it
if (j.icestats.source.title) {
134
+
// Update the now playing text
e.querySelector('.playing').innerHTML = j.icestats.source.title
137
+
// If the title has changed and this is our current playing mount
138
+
if (lastTitle !== j.icestats.source.title && currentMount === e.dataset.mount) {
139
+
// Update the last title
140
+
lastTitle = j.icestats.source.title
142
+
// If we have permission, send a notification
143
+
if ('Notification' in window && Notification.permission === 'granted') {
144
+
lastNotification = new Notification(
145
+
'RRM - Now Playing',
147
+
body: j.icestats.source.title,
148
+
icon: 'https://rita.moe/rita-icon.png',
150
+
requireInteraction: false,
151
+
tag: 'now-playing',
157
+
// If the title is not present, clear the now playing text
e.querySelector('.playing').innerHTML = ''
···
165
+
// Enable interval to fetch now playing data
167
+
// And run it immediately on page load
96
-
document.querySelectorAll('audio').forEach(e => {
98
-
controls: ['play', 'current-time', 'mute', 'volume'],
100
-
toggleInvert: false
170
+
// Request notification permission if it's still default
171
+
if ('Notification' in window && Notification.permission === 'default') {
172
+
Notification.requestPermission()
175
+
document.addEventListener(
176
+
'visibilitychange',
178
+
// If the page is visible, close the last notification
179
+
if (document.visibilityState === 'visible') {
180
+
lastNotification?.close()