2022-11-23 14:13:29 +01:00
|
|
|
// ==UserScript==
|
2024-06-09 04:48:36 +02:00
|
|
|
// @name Breizh autoplay
|
|
|
|
// @namespace http://tampermonkey.net/
|
|
|
|
// @version 2024-04-10
|
|
|
|
// @description Add an audio player for audio files on fichiers.breizh.pm
|
|
|
|
// @author Eldeberen
|
|
|
|
// @match https://fichiers.breizh.pm/*
|
|
|
|
// @grant none
|
2022-11-23 14:13:29 +01:00
|
|
|
// ==/UserScript==
|
|
|
|
|
2024-06-09 04:48:36 +02:00
|
|
|
|
|
|
|
// Add current song to history
|
|
|
|
function ap_add2history(event) {
|
|
|
|
ap_history.push(event.target);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play the next track
|
2022-11-23 14:13:29 +01:00
|
|
|
function ap_next(event) {
|
2024-06-09 04:48:36 +02:00
|
|
|
let item = ap_history.at(-1);
|
|
|
|
let autoplay = document.getElementById("ap-player").open;
|
2022-11-23 14:13:29 +01:00
|
|
|
let random = document.getElementById("ap-random").checked;
|
|
|
|
let loop = document.getElementById("ap-loop").checked;
|
2024-06-09 04:48:36 +02:00
|
|
|
let currentId = ap_medias.indexOf(item);
|
2022-11-23 14:13:29 +01:00
|
|
|
let nextId = -1;
|
2024-06-09 04:48:36 +02:00
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// If no autoplay, exit
|
|
|
|
if(!autoplay) return;
|
2024-06-09 04:48:36 +02:00
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// 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;
|
2024-06-09 04:48:36 +02:00
|
|
|
|
|
|
|
// Make sure to pause the current track
|
|
|
|
item.pause();
|
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// If random, pick a random song in the list
|
|
|
|
// Else pick the next one
|
|
|
|
if(random) {
|
2024-06-09 04:48:36 +02:00
|
|
|
nextId = Math.floor(Math.random() * ap_medias.length);
|
2022-11-23 14:13:29 +01:00
|
|
|
} else {
|
|
|
|
nextId = currentId + 1;
|
|
|
|
}
|
2024-06-09 04:48:36 +02:00
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// If looping, stay in the range
|
|
|
|
if(loop) {
|
2024-06-09 04:48:36 +02:00
|
|
|
nextId %= ap_medias.length;
|
2022-11-23 14:13:29 +01:00
|
|
|
}
|
2024-06-09 04:48:36 +02:00
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// If the next song is out, exit
|
2024-06-09 04:48:36 +02:00
|
|
|
if(nextId == ap_medias.length) return;
|
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// Finally, set the volume back and jump to the selected song
|
2024-06-09 04:48:36 +02:00
|
|
|
ap_medias[nextId].volume = item.volume;
|
|
|
|
ap_medias[nextId].play();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play/pause current
|
|
|
|
function ap_playpause() {
|
|
|
|
if(ap_paused == null) {
|
|
|
|
ap_paused = ap_history.pop();
|
|
|
|
ap_paused.pause();
|
|
|
|
} else {
|
|
|
|
ap_paused.play();
|
|
|
|
ap_paused = null;
|
|
|
|
}
|
2022-11-23 14:13:29 +01:00
|
|
|
}
|
|
|
|
|
2024-06-09 04:48:36 +02:00
|
|
|
// Play previous song
|
|
|
|
function ap_prev() {
|
|
|
|
let current = ap_history.pop();
|
|
|
|
let prev = ap_history.pop();
|
|
|
|
current.pause();
|
|
|
|
prev.currentTime = 0;
|
|
|
|
prev.play();
|
|
|
|
}
|
2022-11-23 14:13:29 +01:00
|
|
|
// Get all audio medias on the page
|
2024-06-09 04:48:36 +02:00
|
|
|
let ap_medias = Array.from(document.querySelectorAll("audio"));
|
|
|
|
|
|
|
|
// Define a playing history
|
|
|
|
let ap_history = [];
|
|
|
|
let ap_paused = null;
|
2022-11-23 14:13:29 +01:00
|
|
|
|
|
|
|
// If there is at least one media
|
2024-06-09 04:48:36 +02:00
|
|
|
if(ap_medias.length > 0) {
|
2022-11-23 14:13:29 +01:00
|
|
|
// Create the menu
|
|
|
|
var ap_menu = document.createElement("div");
|
2024-06-09 04:48:36 +02:00
|
|
|
ap_menu.id = "ap-manu";
|
|
|
|
ap_menu.innerHTML = `<details id="ap-player">
|
|
|
|
<summary>Player</summary>
|
|
|
|
<button id="ap-prev">⏮</button>
|
|
|
|
<button id="ap-playpause">⏯</button>
|
|
|
|
<button id="ap-next">⏭</button>
|
|
|
|
<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" />
|
|
|
|
</details>`;
|
2022-11-23 14:13:29 +01:00
|
|
|
document.querySelector("body").insertBefore(ap_menu, document.getElementById("maintable"));
|
|
|
|
|
2024-06-09 04:48:36 +02:00
|
|
|
// Add event listeners
|
|
|
|
[["ap-prev", ap_prev],
|
|
|
|
["ap-playpause", ap_playpause],
|
|
|
|
["ap-next", ap_next]].map((e) => {
|
|
|
|
document.getElementById(e[0]).addEventListener("click", e[1]);
|
|
|
|
});
|
|
|
|
|
2022-11-23 14:13:29 +01:00
|
|
|
// Apply the event listener to all medias
|
2024-06-09 04:48:36 +02:00
|
|
|
ap_medias.map(function(e) {
|
2022-11-23 14:13:29 +01:00
|
|
|
e.addEventListener("ended", ap_next);
|
2024-06-09 04:48:36 +02:00
|
|
|
e.addEventListener("play", ap_add2history);
|
2022-11-23 14:13:29 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|