save volume to settings

This commit is contained in:
Araxeus
2021-04-17 01:58:33 +03:00
parent 06958c424c
commit b65bc65d7c

View File

@ -1,31 +1,42 @@
module.exports = () => { const { setOptions } = require("../../config/plugins");
setPlaybarOnwheel();
setObserver(); module.exports = (options) => {
setFirstTooltip(); setPlaybarOnwheel(options);
setObserver(options);
firstRun(options);
}; };
function setFirstTooltip() { function saveVolume(volume, options) {
options.savedVolume = volume;
setOptions("precise-volume", options);
}
function firstRun(options) {
const videoStream = document.querySelector(".video-stream"); const videoStream = document.querySelector(".video-stream");
if (videoStream?.volume) { if (videoStream) {
// Set saved volume if it exists and is valid
if (options.savedVolume && options.savedVolume >= 0 && options.savedVolume <= 100) {
videoStream.volume = options.savedVolume / 100;
document.querySelector("#volume-slider").value = options.savedVolume;
}
// Set current volume as tooltip
setTooltip(toPercent(videoStream.volume)); setTooltip(toPercent(videoStream.volume));
} else { } else {
setTimeout(setFirstTooltip, 500); // Try again in 500 milliseconds setTimeout(firstRun, 500, options); // Try again in 500 milliseconds
} }
} }
function setPlaybarOnwheel() { function setPlaybarOnwheel(options) {
// Add onwheel event to play bar // Add onwheel event to play bar
document.querySelector("ytmusic-player-bar").onwheel = event => { document.querySelector("ytmusic-player-bar").onwheel = event => {
event.preventDefault(); event.preventDefault();
// Event.deltaY < 0 => wheel up // Event.deltaY < 0 => wheel up
changeVolume(event.deltaY < 0); changeVolume(event.deltaY < 0, options);
}; };
} }
// The last volume set by changeVolume() is stored here function changeVolume(increase, options) {
let newVolume; // Used to determine if volume-slider was manually moved
function changeVolume(increase) {
// Need to change both the slider and the actual volume // Need to change both the slider and the actual volume
const videoStream = document.querySelector(".video-stream"); const videoStream = document.querySelector(".video-stream");
const slider = document.querySelector("#volume-slider"); const slider = document.querySelector("#volume-slider");
@ -36,22 +47,23 @@ function changeVolume(increase) {
Math.max(videoStream.volume - 0.01, 0); Math.max(videoStream.volume - 0.01, 0);
// Save the new volume // Save the new volume
newVolume = toPercent(videoStream.volume); saveVolume(toPercent(videoStream.volume), options);
// Slider value automatically rounds to multiples of 5 // Slider value automatically rounds to multiples of 5
slider.value = newVolume; slider.value = options.savedVolume;
// Finally change tooltip to new value // Finally change tooltip to new value
setTooltip(newVolume); setTooltip(options.savedVolume);
} }
// Update the volume tooltip when volume-slider is manually changed // Save volume + Update the volume tooltip when volume-slider is manually changed
function setObserver() { function setObserver(options) {
const observer = new MutationObserver(mutations => { const observer = new MutationObserver(mutations => {
for (const mutation of mutations) { for (const mutation of mutations) {
// This checks that volume-slider was manually set // This checks that volume-slider was manually set
if (mutation.oldValue !== mutation.target.value && if (mutation.oldValue !== mutation.target.value &&
(!newVolume || Math.abs(newVolume - mutation.target.value) > 4)) { (!options.savedVolume || Math.abs(options.savedVolume - mutation.target.value) > 4)) {
// Diff>4 means it was manually set, so update tooltip accordingly // Diff>4 means it was manually set
setTooltip(mutation.target.value); setTooltip(mutation.target.value);
saveVolume(mutation.target.value, options);
} }
} }
}); });