forked from I2P_Developers/i2p.i2p
244 lines
6.6 KiB
JavaScript
244 lines
6.6 KiB
JavaScript
/* @license http://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 */
|
|
/* see also licenses/LICENSE-GPLv2.txt */
|
|
|
|
var __i2psnark_isplaying = false;
|
|
var __i2psnark_autoplay = false;
|
|
var __i2psnark_playindex = -1;
|
|
var __i2psnark_playsize = 0;
|
|
|
|
// note that we use currentTime = 0, not fastSeek(0), because
|
|
// Chrome doesn't support it.
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
|
|
|
|
const setupplaybuttons=()=>{
|
|
let button = document.getElementById('playall');
|
|
if (button === null)
|
|
return;
|
|
const audios = document.getElementsByClassName("audio");
|
|
__i2psnark_playsize = audios.length;
|
|
for (var i = 0; i < __i2psnark_playsize; i++) {
|
|
const audio = audios[i];
|
|
audio.addEventListener("ended", function() {
|
|
audioended();
|
|
});
|
|
audio.addEventListener("pause", function() {
|
|
audiopause();
|
|
});
|
|
audio.addEventListener("play", function() {
|
|
audioplay();
|
|
});
|
|
}
|
|
if (__i2psnark_playsize > 1) {
|
|
button.addEventListener("click", function() {
|
|
playall();
|
|
event.preventDefault();
|
|
});
|
|
} else {
|
|
button.display = false;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playpause');
|
|
button.disabled = true;
|
|
if (__i2psnark_playsize > 1) {
|
|
button.addEventListener("click", function() {
|
|
playpause();
|
|
event.preventDefault();
|
|
});
|
|
} else {
|
|
button.display = false;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playprev');
|
|
if (__i2psnark_playsize > 1) {
|
|
button.disabled = true;
|
|
button.addEventListener("click", function() {
|
|
playprev();
|
|
event.preventDefault();
|
|
});
|
|
} else {
|
|
button.display = false;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playnext');
|
|
if (__i2psnark_playsize > 1) {
|
|
button.addEventListener("click", function() {
|
|
playnext();
|
|
event.preventDefault();
|
|
});
|
|
} else {
|
|
button.display = false;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
}
|
|
|
|
// buttons
|
|
|
|
const playprev=()=>{
|
|
if (__i2psnark_playindex <= 0)
|
|
return;
|
|
const audios = document.getElementsByClassName("audio");
|
|
if (__i2psnark_isplaying) {
|
|
audios[__i2psnark_playindex].pause();
|
|
audios[__i2psnark_playindex].currentTime = 0;
|
|
}
|
|
__i2psnark_playindex--;
|
|
audios[__i2psnark_playindex].currentTime = 0;
|
|
audios[__i2psnark_playindex].play();
|
|
playing();
|
|
}
|
|
|
|
const playnext=()=>{
|
|
if (__i2psnark_playindex >= __i2psnark_playsize - 1)
|
|
return;
|
|
const audios = document.getElementsByClassName("audio");
|
|
if (__i2psnark_isplaying) {
|
|
audios[__i2psnark_playindex].pause();
|
|
audios[__i2psnark_playindex].currentTime = 0;
|
|
}
|
|
__i2psnark_playindex++;
|
|
audios[__i2psnark_playindex].currentTime = 0;
|
|
audios[__i2psnark_playindex].play();
|
|
playing();
|
|
}
|
|
|
|
const playpause=()=>{
|
|
__i2psnark_autoplay = false;
|
|
if (!__i2psnark_isplaying)
|
|
return;
|
|
const audios = document.getElementsByClassName("audio");
|
|
audios[__i2psnark_playindex].pause();
|
|
notplaying();
|
|
}
|
|
|
|
const playall=()=>{
|
|
__i2psnark_autoplay = true;
|
|
if (__i2psnark_isplaying)
|
|
return;
|
|
if (__i2psnark_playindex >= __i2psnark_playsize - 1 || __i2psnark_playindex < 0)
|
|
__i2psnark_playindex = 0;
|
|
const audios = document.getElementsByClassName("audio");
|
|
// no, start where we were before
|
|
//audios[__i2psnark_playindex].currentTime = 0;
|
|
audios[__i2psnark_playindex].play();
|
|
playing();
|
|
}
|
|
|
|
// events
|
|
|
|
const audioended=()=>{
|
|
const audio = event.target;
|
|
audio.currentTime = 0;
|
|
__i2psnark_playindex = Number(audio.getAttribute("audioindex"));
|
|
if (__i2psnark_playindex < __i2psnark_playsize - 1) {
|
|
__i2psnark_playindex++;
|
|
if (__i2psnark_autoplay) {
|
|
const audios = document.getElementsByClassName("audio");
|
|
audios[__i2psnark_playindex].play();
|
|
playing();
|
|
} else {
|
|
notplaying();
|
|
}
|
|
} else {
|
|
__i2psnark_playindex = -1;
|
|
notplaying();
|
|
}
|
|
}
|
|
|
|
const audiopause=()=>{
|
|
const audio = event.target;
|
|
__i2psnark_playindex = Number(audio.getAttribute("audioindex"));
|
|
notplaying();
|
|
}
|
|
|
|
const audioplay=()=>{
|
|
const audio = event.target;
|
|
const old__i2psnark_playindex = __i2psnark_playindex;
|
|
__i2psnark_playindex = Number(audio.getAttribute("audioindex"));
|
|
if (__i2psnark_isplaying && old__i2psnark_playindex != __i2psnark_playindex && old__i2psnark_playindex >= 0 && old__i2psnark_playindex <= __i2psnark_playsize - 1) {
|
|
// prevent two at once
|
|
const audios = document.getElementsByClassName("audio");
|
|
audios[old__i2psnark_playindex].pause();
|
|
audios[old__i2psnark_playindex].currentTime = 0;
|
|
}
|
|
playing();
|
|
}
|
|
|
|
// state changes
|
|
|
|
const playing=()=>{
|
|
__i2psnark_isplaying = true;
|
|
let button = document.getElementById('playprev');
|
|
if (__i2psnark_playindex > 0) {
|
|
button.disabled=false;
|
|
button.classList.remove("controld");
|
|
button.classList.add("control");
|
|
} else {
|
|
button.disabled=true;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playnext');
|
|
if (__i2psnark_playindex < __i2psnark_playsize - 1) {
|
|
button.disabled=false;
|
|
button.classList.remove("controld");
|
|
button.classList.add("control");
|
|
} else {
|
|
button.disabled=true;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playall');
|
|
button.disabled=true;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
button = document.getElementById('playpause');
|
|
button.disabled=false;
|
|
button.classList.remove("controld");
|
|
button.classList.add("control");
|
|
// getbyclassname[__i2psnark_playindex].getElementsByTagName("source")[0].getAttribute("src")
|
|
// lastindexof /
|
|
// escape, set text
|
|
}
|
|
|
|
const notplaying=()=>{
|
|
__i2psnark_isplaying = false;
|
|
let button = document.getElementById('playprev');
|
|
if (__i2psnark_playindex > 0) {
|
|
button.disabled=false;
|
|
button.classList.remove("controld");
|
|
button.classList.add("control");
|
|
} else {
|
|
button.disabled=true;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playnext');
|
|
if (__i2psnark_playindex < __i2psnark_playsize - 1) {
|
|
button.disabled=false;
|
|
button.classList.remove("controld");
|
|
button.classList.add("control");
|
|
} else {
|
|
button.disabled=true;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
button = document.getElementById('playall');
|
|
button.disabled=false;
|
|
button.classList.remove("controld");
|
|
button.classList.add("control");
|
|
button = document.getElementById('playpause');
|
|
button.disabled=true;
|
|
button.classList.remove("control");
|
|
button.classList.add("controld");
|
|
}
|
|
|
|
document.addEventListener("DOMContentLoaded", function() {
|
|
setupplaybuttons();
|
|
}, true);
|
|
|
|
/* @license-end */
|