From 3d598dfb60a3ac0c8c1bf53ebcd243daf2fb409a Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 19 Aug 2020 17:19:13 +0200 Subject: [PATCH] randomise background if no album cover exists --- pages/index.php | 10 ++++++++++ public/assets/2020v2.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pages/index.php b/pages/index.php index b010409..0a30423 100644 --- a/pages/index.php +++ b/pages/index.php @@ -7,6 +7,16 @@ if($reqPath === '/about' || $reqPath === '/about.html' || $reqPath === '/about.p 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($reqMethod !== 'GET') return FM_ERR | 405; diff --git a/public/assets/2020v2.js b/public/assets/2020v2.js index 1f4d3f1..8b6c4cc 100644 --- a/public/assets/2020v2.js +++ b/public/assets/2020v2.js @@ -6,6 +6,26 @@ window.fm = (function() { this.indexPlayingCover = null; this.indexPlayingTitle = 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) { // MSIE @@ -53,6 +73,14 @@ window.fm = (function() { this.updateIndexNowListening = function() { 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.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;