randomise background if no album cover exists

This commit is contained in:
flash 2020-08-19 17:19:13 +02:00
parent 718fc88b09
commit 3d598dfb60
2 changed files with 38 additions and 0 deletions

View file

@ -7,6 +7,16 @@ if($reqPath === '/about' || $reqPath === '/about.html' || $reqPath === '/about.p
return FM_HIT | 302; return FM_HIT | 302;
} }
if($reqPath === '/header-bgs.json') {
if($reqMethod !== 'GET')
return FM_ERR | 405;
header('Content-Type: application/json; charset=utf-8');
echo json_encode(FM_BGS);
return FM_HIT;
}
if($reqPath === '/now-listening') { if($reqPath === '/now-listening') {
if($reqMethod !== 'GET') if($reqMethod !== 'GET')
return FM_ERR | 405; return FM_ERR | 405;

View file

@ -6,6 +6,26 @@ window.fm = (function() {
this.indexPlayingCover = null; this.indexPlayingCover = null;
this.indexPlayingTitle = null; this.indexPlayingTitle = null;
this.indexPlayingArtist = null; this.indexPlayingArtist = null;
this.indexLastNp = null;
if(sessionStorage.getItem('header-bgs') === null
|| sessionStorage.getItem('header-bgs-loaded') < Date.now() - 86400000) {
var hXhr = new XMLHttpRequest;
hXhr.onload = function() {
console.log(JSON.parse(hXhr.responseText));
sessionStorage.setItem('header-bgs', hXhr.responseText);
sessionStorage.setItem('header-bgs-loaded', Date.now());
};
hXhr.open('GET', '/header-bgs.json');
hXhr.send();
}
this.getRandomHeaderBackground = function() {
var set = JSON.parse(sessionStorage.getItem('header-bgs'));
if(!set)
return '/assets/errors/404.jpg';
return set[parseInt(Math.random() * set.length) - 1];
};
this.selectTextInElement = function(elem) { this.selectTextInElement = function(elem) {
// MSIE // MSIE
@ -53,6 +73,14 @@ window.fm = (function() {
this.updateIndexNowListening = function() { this.updateIndexNowListening = function() {
window.fm.getNowListening(function(info) { window.fm.getNowListening(function(info) {
if(this.indexLastNp === null
|| this.indexLastNp.url != info.url
|| this.indexLastNp.now_playing != info.now_playing) {
if(this.indexLastNp !== null)
this.originalHeaderBackground = this.getRandomHeaderBackground();
this.indexLastNp = info;
} else return;
this.indexPlayingContainer.classList[info.now_playing ? 'remove' : 'add']('header-now-playing-hidden'); this.indexPlayingContainer.classList[info.now_playing ? 'remove' : 'add']('header-now-playing-hidden');
this.indexPlayingCover.alt = this.indexPlayingCover.src = (info.cover !== this.defaultCoverImage ? info.cover : '//now.flash.moe/resources/no-cover.png'); this.indexPlayingCover.alt = this.indexPlayingCover.src = (info.cover !== this.defaultCoverImage ? info.cover : '//now.flash.moe/resources/no-cover.png');
this.indexPlayingTitle.textContent = this.indexPlayingTitle.title = info.name; this.indexPlayingTitle.textContent = this.indexPlayingTitle.title = info.name;