Skip to content

Commit

Permalink
move yt setting setting helper out of valueSetters
Browse files Browse the repository at this point in the history
  • Loading branch information
GooseOb committed Jan 6, 2025
1 parent 7360107 commit d4eb8c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yt-defaulter",
"author": "GooseOb",
"version": "1.11.16",
"version": "1.11.17",
"repository": {
"type": "git",
"url": "git+https://github.com/GooseOb/YT-Defaulter.git"
Expand Down
1 change: 0 additions & 1 deletion src/menu/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ export const fixPosition = () => {
const { y, height, width, left } = btn.getBoundingClientRect();
element.style.top = y + height + 8 + 'px';
element.style.left = left + width - menuWidth + 'px';
console.log(element.style.left);
};
52 changes: 23 additions & 29 deletions src/player/value-setters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,45 @@ import * as menu from './menu';
import type { YtSettingName } from './types';

type Comparator = (target: string, current: string) => boolean;
const comparators: { readonly [P in YtSettingName]: Comparator } = {
const comparators = {
// assuming the search is from the top
[QUALITY]: (target, current) =>
+target >= parseInt(current) &&
(config.value.flags.enhancedBitrate ||
!current.toLowerCase().includes('premium')),
[SPEED]: (target, current) => target === current,
};
type ValueSetterHelpers = {
_ytSettingItem(settingName: YtSettingName, value: string): void;
};
type ValueSetters = {
[P in config.Setting]: (value: Required<config.Cfg>[P]) => void;
} satisfies { readonly [P in YtSettingName]: Comparator };

const setYT = (settingName: YtSettingName) => (value: string) => {
const isOpen = menu.isOpen();
const compare = comparators[settingName];
const btn = menu.findInItem(settingName, (btn) =>
compare(value, btn.textContent)
);
if (btn) {
btn.click();
}
menu.setOpen(isOpen);
};

export const valueSetters: ValueSetters & ValueSetterHelpers = {
_ytSettingItem(settingName, value) {
const isOpen = menu.isOpen();
const compare = comparators[settingName];
const btn = menu.findInItem(settingName, (btn) =>
compare(value, btn.textContent)
);
if (btn) {
btn.click();
menu.setOpen(isOpen);
}
},
speed(value) {
this._ytSettingItem(SPEED, plr.isSpeedApplied ? plr.speedNormal : value);
export const valueSetters = {
speed: (value) => {
setYT(SPEED)(plr.isSpeedApplied ? plr.speedNormal : value);
plr.toggleSpeed();
},
customSpeed(value) {
customSpeed: (value) => {
try {
plr.video.playbackRate = plr.isSpeedApplied ? 1 : +value;
plr.toggleSpeed();
} catch {
logger.outOfRange('Custom speed');
return;
}
plr.toggleSpeed();
},
subtitles(value) {
subtitles: (value) => {
if (plr.subtitlesBtn.ariaPressed !== value.toString())
plr.subtitlesBtn.click();
},
volume(value) {
volume: (value) => {
const num = +value;
const isMuted = plr.muteBtn.dataset.titleNoTooltip !== 'Mute';
if (num === 0) {
Expand All @@ -63,7 +57,7 @@ export const valueSetters: ValueSetters & ValueSetterHelpers = {
}
}
},
quality(value) {
this._ytSettingItem(QUALITY, value);
},
quality: setYT(QUALITY),
} satisfies {
[P in config.Setting]: (value: Required<config.Cfg>[P]) => void;
};

0 comments on commit d4eb8c1

Please sign in to comment.