From 51e2e9cb0c0c8efec2dde955ed6c5f136250cda1 Mon Sep 17 00:00:00 2001 From: Jimmy <9859727+Quantizr@users.noreply.github.com> Date: Thu, 9 Mar 2023 00:56:37 -0800 Subject: [PATCH 1/2] fix playback speed issue on certain detected media --- src/contents/lib/SpeedController.ts | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/contents/lib/SpeedController.ts b/src/contents/lib/SpeedController.ts index bea8ed8..2041840 100644 --- a/src/contents/lib/SpeedController.ts +++ b/src/contents/lib/SpeedController.ts @@ -1,4 +1,5 @@ import debugging from "debug" +import inspectMediaElements from "./inspectMediaElements" import type { MediaElement } from "../../shared/types" @@ -100,18 +101,7 @@ export default class SpeedController { } setPlaybackRate(rate: number) { - this.elements = [ - ...document.querySelectorAll("video, audio") - ] as MediaElement[] - debug( - "Setting playback rate to", - rate, - "on", - this.elements.length, - "elements" - ) - - this.elements.forEach((element) => { + inspectMediaElements((element: MediaElement) => { this.setPlaybackRateForElement(rate, element) }) } From f2e29c48b701d884f5136cf523499ba951b4e9d8 Mon Sep 17 00:00:00 2001 From: Jimmy <9859727+Quantizr@users.noreply.github.com> Date: Thu, 9 Mar 2023 02:05:12 -0800 Subject: [PATCH 2/2] Prevent unnecessary creation of MutationObservers Moved inspectMediaElements to the constructor where it dynamically updates this.elements --- src/contents/lib/SpeedController.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/contents/lib/SpeedController.ts b/src/contents/lib/SpeedController.ts index 2041840..3125880 100644 --- a/src/contents/lib/SpeedController.ts +++ b/src/contents/lib/SpeedController.ts @@ -17,6 +17,12 @@ export default class SpeedController { elements: MediaElement[] = [] + constructor() { + inspectMediaElements((element: MediaElement) => { + this.elements.push(element) + }) + } + /** * Fixes issues changing the playback rate by temporarily blocking `ratechange` event listeners. */ @@ -101,7 +107,7 @@ export default class SpeedController { } setPlaybackRate(rate: number) { - inspectMediaElements((element: MediaElement) => { + this.elements.forEach((element) => { this.setPlaybackRateForElement(rate, element) }) }