From 121a71449f229467c3524cdea7fdf7d7b66aefa1 Mon Sep 17 00:00:00 2001 From: Jorrin Date: Thu, 15 Feb 2024 14:17:57 +0100 Subject: [PATCH] route subtitles through extension if installed --- src/backend/helpers/subs.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/backend/helpers/subs.ts b/src/backend/helpers/subs.ts index 455f05a9..092de6a9 100644 --- a/src/backend/helpers/subs.ts +++ b/src/backend/helpers/subs.ts @@ -5,6 +5,11 @@ import { convertSubtitlesToSrt } from "@/components/player/utils/captions"; import { CaptionListItem } from "@/stores/player/slices/source"; import { SimpleCache } from "@/utils/cache"; +import { + isExtensionActiveCached, + sendExtensionRequest, +} from "../extension/messaging"; + export const subtitleTypeList = list().map((type) => `.${type}`); const downloadCache = new SimpleCache(); downloadCache.setCompare((a, b) => a === b); @@ -21,7 +26,22 @@ export async function downloadCaption( let data: string | undefined; if (caption.needsProxy) { - data = await proxiedFetch(caption.url, { responseType: "text" }); + if (isExtensionActiveCached()) { + const extensionResponse = await sendExtensionRequest({ + url: caption.url, + method: "GET", + }); + if ( + !extensionResponse?.success || + typeof extensionResponse.response.body !== "string" + ) { + throw new Error("failed to get caption data from extension"); + } + + data = extensionResponse.response.body; + } else { + data = await proxiedFetch(caption.url, { responseType: "text" }); + } } else { data = await fetch(caption.url).then((v) => v.text()); }