From 6a905bf517a459d4d082012c791e45e790b9b718 Mon Sep 17 00:00:00 2001 From: Jorrin Date: Sun, 31 Mar 2024 20:20:10 +0200 Subject: [PATCH] Also route hls audio tracks through extension --- src/backend/extension/messaging.ts | 1 + src/components/player/display/base.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/backend/extension/messaging.ts b/src/backend/extension/messaging.ts index c280f9bd..bf0e3253 100644 --- a/src/backend/extension/messaging.ts +++ b/src/backend/extension/messaging.ts @@ -9,6 +9,7 @@ import { ExtensionMakeRequestResponse } from "@/backend/extension/plasmo"; export const RULE_IDS = { PREPARE_STREAM: 1, SET_DOMAINS_HLS: 2, + SET_DOMAINS_HLS_AUDIO: 3, }; // for some reason, about 500 ms is needed after diff --git a/src/components/player/display/base.ts b/src/components/player/display/base.ts index 8e155f69..8322b82b 100644 --- a/src/components/player/display/base.ts +++ b/src/components/player/display/base.ts @@ -191,6 +191,21 @@ export function makeVideoElementDisplayInterface(): DisplayInterface { }, }); }); + hls.on(Hls.Events.AUDIO_TRACK_LOADED, async (_, data) => { + const chunkUrlsDomains = data.details.fragments.map( + (v) => new URL(v.url).hostname, + ); + const chunkUrls = [...new Set(chunkUrlsDomains)]; + + await setDomainRule({ + ruleId: RULE_IDS.SET_DOMAINS_HLS_AUDIO, + targetDomains: chunkUrls, + requestHeaders: { + ...src.preferredHeaders, + ...src.headers, + }, + }); + }); } }); hls.on(Hls.Events.LEVEL_SWITCHED, () => {