Ajout du lecteur

This commit is contained in:
Breizh 2022-11-23 14:13:29 +01:00
parent 7eb1a9e112
commit 0d8287119b
1 changed files with 63 additions and 0 deletions

63
assets/player.js Normal file
View File

@ -0,0 +1,63 @@
// ==UserScript==
// @name Breizh autoplay
// @version 1
// @grant none
// @include https://fichiers.breizh.pm/*
// ==/UserScript==
function ap_next(event) {
let item = event.target;
let autoplay = document.getElementById("ap-autoplay").checked;
let random = document.getElementById("ap-random").checked;
let loop = document.getElementById("ap-loop").checked;
let currentId = medias.indexOf(item);
let nextId = -1;
// If no autoplay, exit
if(!autoplay) return;
// If function applied to something that is not an audio song, exit
// Note that this should not happen in a regular use
if(currentId == -1) return;
// If random, pick a random song in the list
// Else pick the next one
if(random) {
nextId = Math.floor(Math.random() * medias.length);
} else {
nextId = currentId + 1;
}
// If looping, stay in the range
if(loop) {
nextId %= medias.length;
}
// If the next song is out, exit
if(nextId == medias.length) return;
// Finally, set the volume back and jump to the selected song
medias[nextId].volume = item.volume;
medias[nextId].play();
}
// Get all audio medias on the page
let medias = Array.from(document.querySelectorAll("audio"));
// If there is at least one media
if(media.length > 0) {
// Create the menu
var ap_menu = document.createElement("div");
ap_menu.id = "ap-menu";
ap_menu.innerHTML = `<label for="ap-autoplay">Autoplay</label><input type="checkbox" id="ap-autoplay" />
<label for="ap-random">Aléatoire</label><input type="checkbox" id="ap-random" />
<label for="ap-loop">Boucle</label><input type="checkbox" id="ap-loop" />`;
document.querySelector("body").insertBefore(ap_menu, document.getElementById("maintable"));
// Apply the event listener to all medias
medias.map(function(e) {
e.addEventListener("ended", ap_next);
});
}