flash.moe/public/assets/2020v2.js
2020-07-30 00:55:37 +00:00

113 lines
4.6 KiB
JavaScript

window.fm = (function() {
this.headerBackground = null;
this.originalHeaderBackground = null;
this.defaultCoverImage = 'https://lastfm.freetls.fastly.net/i/u/174s/2a96cbd8b46e442fc41c2b86b821562f.png';
this.indexPlayingContainer = null;
this.indexPlayingCover = null;
this.indexPlayingTitle = null;
this.indexPlayingArtist = null;
this.selectTextInElement = function(elem) {
// MSIE
if(document.body.createTextRange) {
var range = document.body.createTextRange();
range.moveToElementText(elem);
range.select();
return;
}
// Mozilla
if(window.getSelection) {
var select = window.getSelection(),
range = document.createRange();
range.selectNodeContents(elem);
select.removeAllRanges();
select.addRange(range);
return;
}
console.warn('Unable to select text.');
};
this.copySelectedText = function() {
if(document.execCommand) {
document.execCommand('copy');
return;
}
console.warn('Unable to copy text.');
};
this.getNowListening = function(callback) {
if(!callback)
return;
var xhr = new XMLHttpRequest;
xhr.onreadystatechange = function() {
if(xhr.readyState !== 4 || xhr.status !== 200)
return;
callback.call(this, JSON.parse(xhr.responseText));
}.bind(this);
xhr.open('GET', '/now-listening.json');
xhr.send();
};
this.updateIndexNowListening = function() {
window.fm.getNowListening(function(info) {
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;
this.indexPlayingTitle.href = info.url;
this.indexPlayingArtist.textContent = this.indexPlayingArtist.title = info.artist.name;
this.indexPlayingArtist.href = info.artist.url;
this.switchHeaderBackground(
info.now_playing && info.cover !== this.defaultCoverImage
? this.indexPlayingCover.src
: this.originalHeaderBackground
);
});
};
this.getCurrentHeaderBackground = function() {
return this.headerBackground.querySelector('img').src;
};
this.switchHeaderBackground = function(url) {
if(this.getCurrentHeaderBackground() === url)
return;
var newImg = document.createElement('img'),
oldImg = this.headerBackground.querySelector('img');
newImg.alt = newImg.src = url;
newImg.style.opacity = '0';
oldImg.style.zIndex = '-1';
newImg.style.zIndex = '0';
this.headerBackground.appendChild(newImg);
newImg.onload = function() {
setTimeout(function() {
newImg.style.opacity = null;
setTimeout(function() {
newImg.style.zIndex = null;
this.headerBackground.removeChild(oldImg);
}.bind(this), 500);
}.bind(this), 50);
}.bind(this);
newImg.onerror = function() {
this.switchHeaderBackground(this.originalHeaderBackground);
}.bind(this);
};
this.initIndex = function(npInterval) {
this.headerBackground = document.querySelector('.header-background');
this.originalHeaderBackground = this.headerBackground.querySelector('img').src;
this.indexPlayingContainer = document.querySelector('.header-now-playing');
this.indexPlayingCover = window.fm.indexPlayingContainer.querySelector('.header-now-playing-cover img');
this.indexPlayingCover.onerror = function() {
this.indexPlayingCover.src = '//now.flash.moe/resources/no-cover.png';
}.bind(this);
this.indexPlayingTitle = window.fm.indexPlayingContainer.querySelector('.header-now-playing-title a');
this.indexPlayingArtist = window.fm.indexPlayingContainer.querySelector('.header-now-playing-artist a');
this.updateIndexNowListening();
setInterval(this.updateIndexNowListening, (npInterval || 30) * 1000);
};
return this;
}).call(window.fm || {});