mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 02:51:46 +00:00
feat: run prettier
This commit is contained in:
@ -21,7 +21,8 @@ export type PreciseVolumePluginConfig = {
|
||||
|
||||
export default createPlugin({
|
||||
name: 'Precise Volume',
|
||||
description: 'Control the volume precisely using mousewheel/hotkeys, with a custom HUD and customizable volume steps',
|
||||
description:
|
||||
'Control the volume precisely using mousewheel/hotkeys, with a custom HUD and customizable volume steps',
|
||||
restartNeeded: true,
|
||||
config: {
|
||||
enabled: false,
|
||||
@ -37,45 +38,76 @@ export default createPlugin({
|
||||
menu: async ({ setConfig, getConfig, window }) => {
|
||||
const config = await getConfig();
|
||||
|
||||
function changeOptions(changedOptions: Partial<PreciseVolumePluginConfig>, options: PreciseVolumePluginConfig) {
|
||||
function changeOptions(
|
||||
changedOptions: Partial<PreciseVolumePluginConfig>,
|
||||
options: PreciseVolumePluginConfig,
|
||||
) {
|
||||
for (const option in changedOptions) {
|
||||
// HACK: Weird TypeScript error
|
||||
(options as Record<string, unknown>)[option] = (changedOptions as Record<string, unknown>)[option];
|
||||
(options as Record<string, unknown>)[option] = (
|
||||
changedOptions as Record<string, unknown>
|
||||
)[option];
|
||||
}
|
||||
|
||||
setConfig(options);
|
||||
}
|
||||
|
||||
// Helper function for globalShortcuts prompt
|
||||
const kb = (label_: string, value_: string, default_: string): KeybindOptions => ({ 'value': value_, 'label': label_, 'default': default_ || undefined });
|
||||
const kb = (
|
||||
label_: string,
|
||||
value_: string,
|
||||
default_: string,
|
||||
): KeybindOptions => ({
|
||||
value: value_,
|
||||
label: label_,
|
||||
default: default_ || undefined,
|
||||
});
|
||||
|
||||
async function promptVolumeSteps(options: PreciseVolumePluginConfig) {
|
||||
const output = await prompt({
|
||||
title: 'Volume Steps',
|
||||
label: 'Choose Volume Increase/Decrease Steps',
|
||||
value: options.steps || 1,
|
||||
type: 'counter',
|
||||
counterOptions: { minimum: 0, maximum: 100, multiFire: true },
|
||||
width: 380,
|
||||
...promptOptions(),
|
||||
}, window);
|
||||
const output = await prompt(
|
||||
{
|
||||
title: 'Volume Steps',
|
||||
label: 'Choose Volume Increase/Decrease Steps',
|
||||
value: options.steps || 1,
|
||||
type: 'counter',
|
||||
counterOptions: { minimum: 0, maximum: 100, multiFire: true },
|
||||
width: 380,
|
||||
...promptOptions(),
|
||||
},
|
||||
window,
|
||||
);
|
||||
|
||||
if (output || output === 0) { // 0 is somewhat valid
|
||||
if (output || output === 0) {
|
||||
// 0 is somewhat valid
|
||||
changeOptions({ steps: output }, options);
|
||||
}
|
||||
}
|
||||
|
||||
async function promptGlobalShortcuts(options: PreciseVolumePluginConfig, item: MenuItem) {
|
||||
const output = await prompt({
|
||||
title: 'Global Volume Keybinds',
|
||||
label: 'Choose Global Volume Keybinds:',
|
||||
type: 'keybind',
|
||||
keybindOptions: [
|
||||
kb('Increase Volume', 'volumeUp', options.globalShortcuts?.volumeUp),
|
||||
kb('Decrease Volume', 'volumeDown', options.globalShortcuts?.volumeDown),
|
||||
],
|
||||
...promptOptions(),
|
||||
}, window);
|
||||
async function promptGlobalShortcuts(
|
||||
options: PreciseVolumePluginConfig,
|
||||
item: MenuItem,
|
||||
) {
|
||||
const output = await prompt(
|
||||
{
|
||||
title: 'Global Volume Keybinds',
|
||||
label: 'Choose Global Volume Keybinds:',
|
||||
type: 'keybind',
|
||||
keybindOptions: [
|
||||
kb(
|
||||
'Increase Volume',
|
||||
'volumeUp',
|
||||
options.globalShortcuts?.volumeUp,
|
||||
),
|
||||
kb(
|
||||
'Decrease Volume',
|
||||
'volumeDown',
|
||||
options.globalShortcuts?.volumeDown,
|
||||
),
|
||||
],
|
||||
...promptOptions(),
|
||||
},
|
||||
window,
|
||||
);
|
||||
|
||||
if (output) {
|
||||
const newGlobalShortcuts: {
|
||||
@ -83,12 +115,15 @@ export default createPlugin({
|
||||
volumeDown: string;
|
||||
} = { volumeUp: '', volumeDown: '' };
|
||||
for (const { value, accelerator } of output) {
|
||||
newGlobalShortcuts[value as keyof typeof newGlobalShortcuts] = accelerator;
|
||||
newGlobalShortcuts[value as keyof typeof newGlobalShortcuts] =
|
||||
accelerator;
|
||||
}
|
||||
|
||||
changeOptions({ globalShortcuts: newGlobalShortcuts }, options);
|
||||
|
||||
item.checked = Boolean(options.globalShortcuts.volumeUp) || Boolean(options.globalShortcuts.volumeDown);
|
||||
item.checked =
|
||||
Boolean(options.globalShortcuts.volumeUp) ||
|
||||
Boolean(options.globalShortcuts.volumeDown);
|
||||
} else {
|
||||
// Reset checkbox if prompt was canceled
|
||||
item.checked = !item.checked;
|
||||
@ -107,7 +142,10 @@ export default createPlugin({
|
||||
{
|
||||
label: 'Global Hotkeys',
|
||||
type: 'checkbox',
|
||||
checked: Boolean(config.globalShortcuts?.volumeUp ?? config.globalShortcuts?.volumeDown),
|
||||
checked: Boolean(
|
||||
config.globalShortcuts?.volumeUp ??
|
||||
config.globalShortcuts?.volumeDown,
|
||||
),
|
||||
click: (item) => promptGlobalShortcuts(config, item),
|
||||
},
|
||||
{
|
||||
@ -121,11 +159,15 @@ export default createPlugin({
|
||||
const config = await getConfig();
|
||||
|
||||
if (config.globalShortcuts?.volumeUp) {
|
||||
globalShortcut.register(config.globalShortcuts.volumeUp, () => ipc.send('changeVolume', true));
|
||||
globalShortcut.register(config.globalShortcuts.volumeUp, () =>
|
||||
ipc.send('changeVolume', true),
|
||||
);
|
||||
}
|
||||
|
||||
if (config.globalShortcuts?.volumeDown) {
|
||||
globalShortcut.register(config.globalShortcuts.volumeDown, () => ipc.send('changeVolume', false));
|
||||
globalShortcut.register(config.globalShortcuts.volumeDown, () =>
|
||||
ipc.send('changeVolume', false),
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
@ -135,5 +177,5 @@ export default createPlugin({
|
||||
},
|
||||
onPlayerApiReady,
|
||||
onConfigChange,
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@ -13,11 +13,12 @@ function overrideAddEventListener() {
|
||||
// eslint-disable-next-line @typescript-eslint/unbound-method
|
||||
Element.prototype._addEventListener = Element.prototype.addEventListener;
|
||||
// Override addEventListener to Ignore specific events in volume-slider
|
||||
Element.prototype.addEventListener = function(type: string, listener: (event: Event) => void, useCapture = false) {
|
||||
if (!(
|
||||
ignored.id.includes(this.id)
|
||||
&& ignored.types.includes(type)
|
||||
)) {
|
||||
Element.prototype.addEventListener = function (
|
||||
type: string,
|
||||
listener: (event: Event) => void,
|
||||
useCapture = false,
|
||||
) {
|
||||
if (!(ignored.id.includes(this.id) && ignored.types.includes(type))) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
|
||||
(this as any)._addEventListener(type, listener, useCapture);
|
||||
} else if (window.electronIs.dev()) {
|
||||
@ -29,11 +30,16 @@ function overrideAddEventListener() {
|
||||
export const overrideListener = () => {
|
||||
overrideAddEventListener();
|
||||
// Restore original function after finished loading to avoid keeping Element.prototype altered
|
||||
window.addEventListener('load', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access
|
||||
Element.prototype.addEventListener = (Element.prototype as any)._addEventListener;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access
|
||||
(Element.prototype as any)._addEventListener = undefined;
|
||||
|
||||
}, { once: true });
|
||||
window.addEventListener(
|
||||
'load',
|
||||
() => {
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access */
|
||||
Element.prototype.addEventListener = (
|
||||
Element.prototype as any
|
||||
)._addEventListener;
|
||||
(Element.prototype as any)._addEventListener = undefined;
|
||||
/* eslint-enable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-member-access */
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
};
|
||||
|
||||
@ -24,7 +24,10 @@ export const moveVolumeHud = debounce((showVideo: boolean) => {
|
||||
|
||||
let options: PreciseVolumePluginConfig;
|
||||
|
||||
export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: RendererContext<PreciseVolumePluginConfig>) => {
|
||||
export const onPlayerApiReady = async (
|
||||
playerApi: YoutubePlayer,
|
||||
context: RendererContext<PreciseVolumePluginConfig>,
|
||||
) => {
|
||||
options = await context.getConfig();
|
||||
api = playerApi;
|
||||
|
||||
@ -57,14 +60,20 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
setupLocalArrowShortcuts();
|
||||
|
||||
// Workaround: computedStyleMap().get(string) returns CSSKeywordValue instead of CSSStyleValue
|
||||
const noVid = ($('#main-panel')?.computedStyleMap().get('display') as CSSKeywordValue)?.value === 'none';
|
||||
const noVid =
|
||||
($('#main-panel')?.computedStyleMap().get('display') as CSSKeywordValue)
|
||||
?.value === 'none';
|
||||
injectVolumeHud(noVid);
|
||||
if (!noVid) {
|
||||
setupVideoPlayerOnwheel();
|
||||
if (!window.mainConfig.plugins.isEnabled('video-toggle')) {
|
||||
// Video-toggle handles hud positioning on its own
|
||||
const videoMode = () => api.getPlayerResponse().videoDetails?.musicVideoType !== 'MUSIC_VIDEO_TYPE_ATV';
|
||||
$('video')?.addEventListener('srcChanged', () => moveVolumeHud(videoMode()));
|
||||
const videoMode = () =>
|
||||
api.getPlayerResponse().videoDetails?.musicVideoType !==
|
||||
'MUSIC_VIDEO_TYPE_ATV';
|
||||
$('video')?.addEventListener('srcChanged', () =>
|
||||
moveVolumeHud(videoMode()),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,7 +89,8 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
);
|
||||
} else {
|
||||
const position = 'top: 10px; left: 10px;';
|
||||
const mainStyle = 'font-size: xxx-large; webkit-text-stroke: 1px black; font-weight: 600;';
|
||||
const mainStyle =
|
||||
'font-size: xxx-large; webkit-text-stroke: 1px black; font-weight: 600;';
|
||||
|
||||
$('#song-video')?.insertAdjacentHTML(
|
||||
'afterend',
|
||||
@ -149,8 +159,11 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
// This checks that volume-slider was manually set
|
||||
const target = mutation.target;
|
||||
const targetValueNumeric = Number(target.value);
|
||||
if (mutation.oldValue !== target.value
|
||||
&& (typeof options.savedVolume !== 'number' || Math.abs(options.savedVolume - targetValueNumeric) > 4)) {
|
||||
if (
|
||||
mutation.oldValue !== target.value &&
|
||||
(typeof options.savedVolume !== 'number' ||
|
||||
Math.abs(options.savedVolume - targetValueNumeric) > 4)
|
||||
) {
|
||||
// Diff>4 means it was manually set
|
||||
setTooltip(targetValueNumeric);
|
||||
saveVolume(targetValueNumeric);
|
||||
@ -189,9 +202,11 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
function changeVolume(toIncrease: boolean) {
|
||||
// Apply volume change if valid
|
||||
const steps = Number(options.steps || 1);
|
||||
setVolume(toIncrease
|
||||
? Math.min(api.getVolume() + steps, 100)
|
||||
: Math.max(api.getVolume() - steps, 0));
|
||||
setVolume(
|
||||
toIncrease
|
||||
? Math.min(api.getVolume() + steps, 100)
|
||||
: Math.max(api.getVolume() - steps, 0),
|
||||
);
|
||||
}
|
||||
|
||||
function updateVolumeSlider() {
|
||||
@ -200,7 +215,9 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
for (const slider of ['#volume-slider', '#expand-volume-slider']) {
|
||||
const silderElement = $<HTMLInputElement>(slider);
|
||||
if (silderElement) {
|
||||
silderElement.value = String(savedVolume > 0 && savedVolume < 5 ? 5 : savedVolume);
|
||||
silderElement.value = String(
|
||||
savedVolume > 0 && savedVolume < 5 ? 5 : savedVolume,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -235,7 +252,9 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
function setupLocalArrowShortcuts() {
|
||||
if (options.arrowsShortcut) {
|
||||
window.addEventListener('keydown', (event) => {
|
||||
if ($<HTMLElement & { opened: boolean }>('ytmusic-search-box')?.opened) {
|
||||
if (
|
||||
$<HTMLElement & { opened: boolean }>('ytmusic-search-box')?.opened
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -256,7 +275,9 @@ export const onPlayerApiReady = async (playerApi: YoutubePlayer, context: Render
|
||||
}
|
||||
}
|
||||
|
||||
context.ipc.on('changeVolume', (toIncrease: boolean) => changeVolume(toIncrease));
|
||||
context.ipc.on('changeVolume', (toIncrease: boolean) =>
|
||||
changeVolume(toIncrease),
|
||||
);
|
||||
context.ipc.on('setVolume', (value: number) => setVolume(value));
|
||||
|
||||
firstRun();
|
||||
|
||||
@ -8,6 +8,6 @@
|
||||
text-shadow: rgba(0, 0, 0, 0.5) 0px 0px 12px;
|
||||
}
|
||||
|
||||
ytmusic-player[player-ui-state_="MINIPLAYER"] #volumeHud {
|
||||
ytmusic-player[player-ui-state_='MINIPLAYER'] #volumeHud {
|
||||
top: 0 !important;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user