mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-14 11:51:47 +00:00
refactor + lint
This commit is contained in:
@ -1,24 +1,29 @@
|
|||||||
const { setOptions } = require("../../config/plugins");
|
const { setOptions } = require("../../config/plugins");
|
||||||
const { ipcRenderer } = require("electron");
|
const { ipcRenderer } = require("electron");
|
||||||
|
function $(selector){ return document.querySelector(selector); }
|
||||||
|
|
||||||
module.exports = (options) => {
|
module.exports = (options) => {
|
||||||
setPlaybarOnwheel(options);
|
setPlaybarOnwheel(options);
|
||||||
setupObserver(options);
|
setupSliderObserver(options);
|
||||||
setupArrowShortcuts(options);
|
setupArrowShortcuts(options);
|
||||||
firstRun(options);
|
firstRun(options);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function toPercent(volume) {
|
||||||
|
return Math.round(Number.parseFloat(volume) * 100);
|
||||||
|
}
|
||||||
|
|
||||||
function saveVolume(volume, options) {
|
function saveVolume(volume, options) {
|
||||||
options.savedVolume = volume;
|
options.savedVolume = volume;
|
||||||
setOptions("precise-volume", options);
|
setOptions("precise-volume", options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function firstRun(options) {
|
function firstRun(options) {
|
||||||
const videoStream = document.querySelector(".video-stream");
|
const videoStream = $(".video-stream");
|
||||||
const slider = document.querySelector("#volume-slider");
|
const slider = $("#volume-slider");
|
||||||
|
// Those elements load abit after DOMContentLoaded
|
||||||
if (videoStream && slider) {
|
if (videoStream && slider) {
|
||||||
// Set saved volume if it pass checks
|
// Set saved volume IF it pass checks
|
||||||
if (options.savedVolume
|
if (options.savedVolume
|
||||||
&& options.savedVolume >= 0 && options.savedVolume <= 100
|
&& options.savedVolume >= 0 && options.savedVolume <= 100
|
||||||
&& Math.abs(slider.value - options.savedVolume) < 5
|
&& Math.abs(slider.value - options.savedVolume) < 5
|
||||||
@ -27,7 +32,6 @@ function firstRun(options) {
|
|||||||
videoStream.volume = options.savedVolume / 100;
|
videoStream.volume = options.savedVolume / 100;
|
||||||
slider.value = options.savedVolume;
|
slider.value = options.savedVolume;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set current volume as tooltip
|
// Set current volume as tooltip
|
||||||
setTooltip(toPercent(videoStream.volume));
|
setTooltip(toPercent(videoStream.volume));
|
||||||
} else {
|
} else {
|
||||||
@ -37,13 +41,67 @@ function firstRun(options) {
|
|||||||
|
|
||||||
function setPlaybarOnwheel(options) {
|
function setPlaybarOnwheel(options) {
|
||||||
// Add onwheel event to play bar
|
// Add onwheel event to play bar
|
||||||
document.querySelector("ytmusic-player-bar").onwheel = event => {
|
$("ytmusic-player-bar").onwheel = event => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
// Event.deltaY < 0 => wheel up
|
// Event.deltaY < 0 means wheel-up
|
||||||
changeVolume(event.deltaY < 0, options);
|
changeVolume(event.deltaY < 0, options);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// (increase = false) means volume decrease
|
||||||
|
function changeVolume(increase, options) {
|
||||||
|
// Need to change both the actual volume and the slider
|
||||||
|
const videoStream = $(".video-stream");
|
||||||
|
|
||||||
|
// Apply volume change if valid
|
||||||
|
const steps = options.steps / 100;
|
||||||
|
videoStream.volume = increase ?
|
||||||
|
Math.min(videoStream.volume + steps, 1) :
|
||||||
|
Math.max(videoStream.volume - steps, 0);
|
||||||
|
|
||||||
|
// Save the new volume
|
||||||
|
saveVolume(toPercent(videoStream.volume), options);
|
||||||
|
// Slider value automatically rounds to multiples of 5
|
||||||
|
$("#volume-slider").value = options.savedVolume;
|
||||||
|
// Finally change tooltip to new value
|
||||||
|
setTooltip(options.savedVolume);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save volume + Update the volume tooltip when volume-slider is manually changed
|
||||||
|
function setupSliderObserver(options) {
|
||||||
|
const sliderObserver = new MutationObserver(mutations => {
|
||||||
|
for (const mutation of mutations) {
|
||||||
|
// This checks that volume-slider was manually set
|
||||||
|
if (mutation.oldValue !== mutation.target.value &&
|
||||||
|
(!options.savedVolume || Math.abs(options.savedVolume - mutation.target.value) > 4)) {
|
||||||
|
// Diff>4 means it was manually set
|
||||||
|
setTooltip(mutation.target.value);
|
||||||
|
saveVolume(mutation.target.value, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Observing only changes in 'value' of volume-slider
|
||||||
|
sliderObserver.observe($("#volume-slider"), {
|
||||||
|
attributeFilter: ["value"],
|
||||||
|
attributeOldValue: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set new volume as tooltip for volume slider and icon + expanding slider (appears when window size is small)
|
||||||
|
const tooltipTargets = [
|
||||||
|
"#volume-slider",
|
||||||
|
"tp-yt-paper-icon-button.volume.style-scope.ytmusic-player-bar",
|
||||||
|
"#expand-volume-slider",
|
||||||
|
"#expand-volume"
|
||||||
|
];
|
||||||
|
|
||||||
|
function setTooltip(volume) {
|
||||||
|
for (target of tooltipTargets) {
|
||||||
|
$(target).title = `${volume}%`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function setupArrowShortcuts(options) {
|
function setupArrowShortcuts(options) {
|
||||||
// Register shortcuts if enabled
|
// Register shortcuts if enabled
|
||||||
if (options.arrowsShortcut) {
|
if (options.arrowsShortcut) {
|
||||||
@ -51,10 +109,10 @@ function setupArrowShortcuts(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Change options from renderer to keep sync
|
// Change options from renderer to keep sync
|
||||||
ipcRenderer.on("setArrowsShortcut", (event, isEnabled) => {
|
ipcRenderer.on("setArrowsShortcut", (_event, isEnabled) => {
|
||||||
options.arrowsShortcut = isEnabled;
|
options.arrowsShortcut = isEnabled;
|
||||||
setOptions("precise-volume", options);
|
setOptions("precise-volume", options);
|
||||||
// Can setting without restarting app
|
// This allows changing setting without restarting app
|
||||||
if (isEnabled) {
|
if (isEnabled) {
|
||||||
addListener();
|
addListener();
|
||||||
} else {
|
} else {
|
||||||
@ -72,71 +130,12 @@ function setupArrowShortcuts(options) {
|
|||||||
|
|
||||||
function callback(event) {
|
function callback(event) {
|
||||||
switch (event.code) {
|
switch (event.code) {
|
||||||
case `ArrowUp`:
|
case "ArrowUp":
|
||||||
changeVolume(true, options);
|
changeVolume(true, options);
|
||||||
break;
|
break;
|
||||||
case `ArrowDown`:
|
case "ArrowDown":
|
||||||
changeVolume(false, options);
|
changeVolume(false, options);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function changeVolume(increase, options) {
|
|
||||||
// Need to change both the slider and the actual volume
|
|
||||||
const videoStream = document.querySelector(".video-stream");
|
|
||||||
const slider = document.querySelector("#volume-slider");
|
|
||||||
|
|
||||||
// Apply volume change if valid
|
|
||||||
const steps = options.steps / 100;
|
|
||||||
videoStream.volume = increase ?
|
|
||||||
Math.min(videoStream.volume + steps, 1) :
|
|
||||||
Math.max(videoStream.volume - steps, 0);
|
|
||||||
|
|
||||||
// Save the new volume
|
|
||||||
saveVolume(toPercent(videoStream.volume), options);
|
|
||||||
// Slider value automatically rounds to multiples of 5
|
|
||||||
slider.value = options.savedVolume;
|
|
||||||
// Finally change tooltip to new value
|
|
||||||
setTooltip(options.savedVolume);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save volume + Update the volume tooltip when volume-slider is manually changed
|
|
||||||
function setupObserver(options) {
|
|
||||||
const observer = new MutationObserver(mutations => {
|
|
||||||
for (const mutation of mutations) {
|
|
||||||
// This checks that volume-slider was manually set
|
|
||||||
if (mutation.oldValue !== mutation.target.value &&
|
|
||||||
(!options.savedVolume || Math.abs(options.savedVolume - mutation.target.value) > 4)) {
|
|
||||||
// Diff>4 means it was manually set
|
|
||||||
setTooltip(mutation.target.value);
|
|
||||||
saveVolume(mutation.target.value, options);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Observing only changes in 'value' of volume-slider
|
|
||||||
observer.observe(document.querySelector("#volume-slider"), {
|
|
||||||
attributeFilter: ["value"],
|
|
||||||
attributeOldValue: true
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set new volume as tooltip for volume slider and icon + expanding slider (appears when window size is small)
|
|
||||||
const tooltipTargets = [
|
|
||||||
"#volume-slider",
|
|
||||||
"tp-yt-paper-icon-button.volume.style-scope.ytmusic-player-bar",
|
|
||||||
"#expand-volume-slider",
|
|
||||||
"#expand-volume"
|
|
||||||
];
|
|
||||||
|
|
||||||
function setTooltip(volume) {
|
|
||||||
const tooltip = volume + "%";
|
|
||||||
for (target of tooltipTargets) {
|
|
||||||
document.querySelector(target).title = tooltip;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function toPercent(volume) {
|
|
||||||
return Math.round(Number.parseFloat(volume) * 100);
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user