Last active
September 17, 2023 14:42
-
-
Save kirisakow/62f6fd669cf52a6fe174717ae2cbca2f to your computer and use it in GitHub Desktop.
UserScript for hotmixradio.com that forces an update of the currently played song's info. Hotmixradio.com native update mechanism actually lags a big deal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name updateNowPlayingSongInfo.user.js | |
// @description UserScript for hotmixradio.com that forces an update of the currently played song's info. Hotmixradio.com native update mechanism actually lags a big deal. | |
// @include https://hotmixradio.com/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
var has_my_function_completed_at_least_once = false; | |
const api_call_interval_in_ms = 5000; | |
const request_headers = { | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', | |
'Accept-Language': 'en,fr-FR;q=0.5', | |
'Accept-Encoding': 'gzip, deflate, br', | |
// 'Cache-Control': 'no-cache', | |
'Connection': 'keep-alive', | |
'DNT': '1', | |
'If-None-Match': '55d456f5369b63e1f54dea96a3fef393', | |
'Sec-Fetch-Dest': 'document', | |
'Sec-Fetch-Mode': 'navigate', | |
'Sec-Fetch-Site': 'cross-site', | |
'Upgrade-Insecure-Requests': '1', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0' | |
}; | |
function isPlaying() { | |
const stop_icon = document.querySelector('.stop'); | |
const is_stop_icon_hidden = window.getComputedStyle(stop_icon).getPropertyValue('display') === 'none'; | |
return !is_stop_icon_hidden; | |
} | |
function updateNowPlayingSongInfo() { | |
if (!isPlaying()) { | |
console.log("No song is playing right now."); | |
has_my_function_completed_at_least_once = false; | |
return; | |
} | |
var player_dom_el = document.getElementById('player-front-1'); | |
if (!player_dom_el) { | |
return; | |
} | |
var current_station_id = player_dom_el.getAttribute('data-id'); | |
var current_station_label = document.getElementById('text-animation-1').innerText; | |
var raw_api_url = 'https://api3.mooovdigital.com/hotmix/current/'; | |
var url = raw_api_url + current_station_id; | |
var request_options = {'method': 'GET', 'headers': request_headers}; | |
fetch(url, request_options) | |
.then(response => { | |
if (!response.ok) { | |
throw new Error("Failed to fetch data"); | |
} | |
return response.json(); | |
}) | |
.then(data => { | |
var artist = data.artist.trim(); | |
var title = data.title.trim(); | |
var album = data.album.trim(); | |
var cover_600x600_src = (data.cover['600'] || '').trim(); | |
var title_album_info = `${title || 'unknown title'} (album: ${album || 'unknown'})`; | |
var artist_title_album_info = `${artist || 'unknown artist'} – ${title_album_info}`; | |
if (has_my_function_completed_at_least_once | |
&& document.querySelector('img.player__info-image').getAttribute('src') == cover_600x600_src) { | |
return; | |
} | |
document.querySelector('img.player__info-image').outerHTML = `<img class="player__info-image" src="${cover_600x600_src}" alt="${artist_title_album_info}" title="${artist_title_album_info}">` | |
document.querySelector('h4#text-animation-2').outerHTML = `<h4 class="player__info-player-name" id="text-animation-2"><span>${artist}</span></h4>` | |
document.querySelector('h4#text-animation-3').outerHTML = `<h4 class="player__info-song-name" id="text-animation-3"><span>${title_album_info}</span></h4>` | |
console.log(`Now playing:\n${artist_title_album_info}\n(hotmix radio station ${current_station_label}, ID: ${current_station_id})`); | |
has_my_function_completed_at_least_once = true; | |
}) | |
.catch(error => console.error('Error:', error)); | |
} | |
setInterval(updateNowPlayingSongInfo, api_call_interval_in_ms); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment