Skip to content

Commit

Permalink
move setting computation out of onPageChange
Browse files Browse the repository at this point in the history
  • Loading branch information
GooseOb committed Dec 1, 2024
1 parent 4f9e094 commit bc15bc0
Showing 1 changed file with 30 additions and 22 deletions.
52 changes: 30 additions & 22 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,24 @@ class Hint {
element: HTMLDivElement;
}

const computeSettings = (doNotChangeSpeed: boolean) => {
const settings = {
...cfg.global,
...channelConfig,
};
const isChannelSpeed = 'speed' in channelConfig;
const isChannelCustomSpeed = 'customSpeed' in channelConfig;
if (doNotChangeSpeed) {
settings.speed = speedNormal;
delete settings.customSpeed;
} else if (isChannelCustomSpeed) {
delete settings.speed;
} else if (isChannelSpeed) {
delete settings.customSpeed;
}
return settings;
};

const delay = (ms: number) => new Promise((res) => setTimeout(res, ms));
const onPageChange = async () => {
if (location.pathname !== '/watch') return;
Expand Down Expand Up @@ -506,33 +524,24 @@ const onPageChange = async () => {
(btn) => !+btn.textContent
).textContent;
});
const doNotChangeSpeed =
cfg.flags.standardMusicSpeed && isMusicChannel(aboveTheFold);
const settings = {
...cfg.global,
...channelConfig,
};
const isChannelSpeed = 'speed' in channelConfig;
const isChannelCustomSpeed = 'customSpeed' in channelConfig;
if ((doNotChangeSpeed && !isChannelCustomSpeed) || isChannelSpeed)
delete settings.customSpeed;
if (doNotChangeSpeed && !isChannelSpeed) settings.speed = speedNormal;
if (doNotChangeSpeed) {
settings.speed = speedNormal;
delete settings.customSpeed;
}
const { customSpeed } = settings;
delete settings.customSpeed;

isSpeedApplied = false;
video ||= plr.querySelector('.html5-main-video');
subtitlesBtn ||= plr.querySelector('.ytp-subtitles-button');
restoreFocusAfter(() => {
for (const setting in settings)
valueSetters[setting as Setting](settings[setting as never]);
if (!isNaN(+customSpeed)) {
const settings = computeSettings(
cfg.flags.standardMusicSpeed && isMusicChannel(aboveTheFold)
);

if (!isNaN(+settings.customSpeed)) {
isSpeedApplied = false;
valueSetters.customSpeed(customSpeed);
valueSetters.customSpeed(settings.customSpeed);
}

delete settings.customSpeed;

for (const setting in settings)
valueSetters[setting as Setting](settings[setting as never]);
ytMenu.setOpen(false);
});

Expand Down Expand Up @@ -676,7 +685,6 @@ const onPageChange = async () => {
min: '0',
max: '100',
oninput(this: ReadonlyInputWithHint) {
settings.volume = this.value;
const warning = validateVolume(this.value);
if (warning) {
this.hint.show(warning);
Expand Down

0 comments on commit bc15bc0

Please sign in to comment.