// ==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(medias.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); }); }