From ec6e82ccb0f15c7b6f336d4fed7ae624e3863bdd Mon Sep 17 00:00:00 2001 From: VampireChicken12 Date: Thu, 5 Dec 2024 04:26:13 -0500 Subject: [PATCH] fix(playerSpeed): playback speed change listener was broken --- src/features/playerSpeed/index.ts | 38 ++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/features/playerSpeed/index.ts b/src/features/playerSpeed/index.ts index 4cfe7748..aba7b824 100644 --- a/src/features/playerSpeed/index.ts +++ b/src/features/playerSpeed/index.ts @@ -80,6 +80,7 @@ export function restorePlayerSpeed() { // Set the video playback speed video.playbackRate = Number(playerSpeed); } +const speedValueRegex = /(?("div.ytp-settings-menu:not(#yte-feature-menu)"); const speedMenuItemClickListener = (event: Event) => { @@ -88,7 +89,6 @@ export function setupPlaybackSpeedChangeListener() { const { textContent: speedValue } = speedMenuItem; // If the playback speed is not available, return if (!speedValue) return; - const speedValueRegex = /(?("div.ytp-panel > div.ytp-panel-header"); const panelMenu = targetElement.querySelector("div.ytp-panel > div.ytp-panel-menu"); const menuItems = panelMenu?.querySelectorAll("div.ytp-menuitem"); - if (panelHeader && panelMenu && menuItems && menuItems.length === 8) { - menuItems.forEach((menuItem) => { - eventManager.addEventListener(menuItem, "click", speedMenuItemClickListener, "playerSpeed"); - }); + if (panelHeader && panelMenu && menuItems && menuItems.length === 9) { + const [customSpeedMenuItem] = menuItems; + const customSpeedActive = customSpeedMenuItem.getAttribute("aria-checked") === "true"; + if (customSpeedActive) { + const customSpeedLabel = customSpeedMenuItem.querySelector("div.ytp-menuitem-label"); + if (!customSpeedLabel) return; + const { textContent: customSpeedLabelValue } = customSpeedLabel; + if (!customSpeedLabelValue) return; + if (speedValueRegex.test(customSpeedLabelValue)) { + const speedValueMatch = customSpeedLabelValue.match(speedValueRegex); + if (speedValueMatch) { + const playerSpeed = Number(speedValueMatch[1]); + window.localStorage.setItem("playerSpeed", String(playerSpeed)); + } + } + } else { + menuItems.forEach((menuItem) => { + eventManager.addEventListener(menuItem, "click", speedMenuItemClickListener, "playerSpeed"); + }); + } } }); }); const customSpeedSliderObserver = new MutationObserver((mutationsList: MutationRecord[]) => { mutationsList.forEach((mutation) => { const { target: targetElement } = mutation as { target: HTMLDivElement } & MutationRecord; - const speedValue = targetElement.getAttribute("aria-valuenow"); + if (!targetElement.matches(".ytp-speedslider-text")) return; + const { textContent: speedValue } = targetElement; // If the playback speed is not available, return if (!speedValue) return; const playerSpeed = parseFloat(speedValue); @@ -132,13 +149,8 @@ export function setupPlaybackSpeedChangeListener() { childList: true, subtree: true }); - const menuItems = [ - ...document.querySelectorAll('div.ytp-settings-menu:not(#yte-feature-menu) > .ytp-panel > .ytp-panel-menu [role="menuitem"]') - ]; - const speedMenuItem = menuItems.find( - (el) => - el.children[0].innerHTML === - `` + const speedMenuItem = document.querySelector( + ".ytp-menuitem:has(.ytp-menuitem-icon svg path[d='M10,8v8l6-4L10,8L10,8z M6.3,5L5.7,4.2C7.2,3,9,2.2,11,2l0.1,1C9.3,3.2,7.7,3.9,6.3,5z M5,6.3L4.2,5.7C3,7.2,2.2,9,2,11 l1,.1C3.2,9.3,3.9,7.7,5,6.3z M5,17.7c-1.1-1.4-1.8-3.1-2-4.8L2,13c0.2,2,1,3.8,2.2,5.4L5,17.7z M11.1,21c-1.8-0.2-3.4-0.9-4.8-2 l-0.6,.8C7.2,21,9,21.8,11,22L11.1,21z M22,12c0-5.2-3.9-9.4-9-10l-0.1,1c4.6,.5,8.1,4.3,8.1,9s-3.5,8.5-8.1,9l0.1,1 C18.2,21.5,22,17.2,22,12z'])" ); if (!speedMenuItem) return; const {