flash.moe/assets/makai.js/np/init.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

#include utility.js
#include np/client.js
#include np/element.jsx
const MakaiNowPlayingInit = siteHeader => {
const target = $q('.js-np-target');
if(!(target instanceof Element))
return;
const userName = target.dataset.username;
if(typeof userName !== 'string')
return;
const client = new MakaiNowPlaying(userName);
const element = new MakaiNowPlayingElement;
$rp(target, element.element);
const update = () => {
client.fetch().then(result => {
if(!result.changed)
return;
element.hidden = !result.now_playing;
element.coverUrl = result.cover;
element.titleText = result.name;
element.titleUrl = result.url;
element.artistText = result.artist.name;
element.artistUrl = result.artist.url;
if(result.now_playing && !element.isDefaultCover)
siteHeader.setBackgroundImage(element.coverUrl);
else if(!result.first)
siteHeader.images.random().then(image => {
siteHeader.setBackgroundImage(image);
});
});
};
let interval;
let currentRate;
const stop = () => {
if(interval !== undefined)
clearInterval(interval);
interval = undefined;
};
const start = rate => {
if(typeof rate !== 'number')
rate = 30000;
if(interval !== undefined || currentRate !== rate)
stop();
update();
setInterval(update, rate);
};
start();
return {
get client() { return client; },
get element() { return element; },
start: start,
stop: stop,
};
};