From 0d8287119b293ac2220abe4c8d6e621e1f369313 Mon Sep 17 00:00:00 2001 From: Breizh Date: Wed, 23 Nov 2022 14:13:29 +0100 Subject: [PATCH] Ajout du lecteur --- assets/player.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 assets/player.js diff --git a/assets/player.js b/assets/player.js new file mode 100644 index 0000000..56c49b7 --- /dev/null +++ b/assets/player.js @@ -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 = ` + + `; + 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); + }); +} + +