From 3100d0760979fc41a518219dbb0484b4656ffaf7 Mon Sep 17 00:00:00 2001 From: velzie Date: Sat, 12 Oct 2024 15:16:16 -0400 Subject: [PATCH] unrewriteBlob parity --- src/client/dom/element.ts | 11 ++++++++--- src/shared/rewriters/html.ts | 14 ++++++++++++-- src/worker/fetch.ts | 5 ++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/client/dom/element.ts b/src/client/dom/element.ts index c56cb35..aa4ea8c 100644 --- a/src/client/dom/element.ts +++ b/src/client/dom/element.ts @@ -1,7 +1,12 @@ import { SCRAMJETCLIENT } from "../../symbols"; import { ScramjetClient } from "../client"; import { nativeGetOwnPropertyDescriptor } from "../natives"; -import { unrewriteUrl, htmlRules, unrewriteHtml } from "../../shared"; +import { + unrewriteUrl, + htmlRules, + unrewriteHtml, + unrewriteBlob, +} from "../../shared"; import { rewriteUrl, rewriteCss, @@ -71,8 +76,8 @@ export default function (client: ScramjetClient, self: typeof window) { ["src", "data", "href", "action", "formaction"].includes(attr) ) { if (element === HTMLMediaElement && value.startsWith("blob:")) { - let origin = new URL(value.substring("blob:".length)); - value = "blob:" + location.origin + origin.pathname; + // mediasource blobs cannot be handled in the service worker and must be sourced here + value = unrewriteBlob(value); } else { value = rewriteUrl(value, client.meta); } diff --git a/src/shared/rewriters/html.ts b/src/shared/rewriters/html.ts index d0f6e30..d0b4f16 100644 --- a/src/shared/rewriters/html.ts +++ b/src/shared/rewriters/html.ts @@ -5,6 +5,7 @@ import { URLMeta, rewriteUrl } from "./url"; import { rewriteCss } from "./css"; import { rewriteJs } from "./js"; import { CookieStore } from "../cookie"; +import { unrewriteBlob } from "../../shared"; export function rewriteHtml( html: string, @@ -120,8 +121,6 @@ export const htmlRules: { "image", "iframe", "source", - "video", - "audio", "input", "track", ], @@ -132,6 +131,17 @@ export const htmlRules: { poster: ["video"], "xlink:href": ["image"], }, + { + fn: (value: string, meta: URLMeta) => { + if (value.startsWith("blob:")) { + // for media elements specifically they must take the original blob + // because they can't be fetch'd + return unrewriteBlob(value); + } + return rewriteUrl(value, meta); + }, + src: ["video", "audio"], + }, { fn: () => null, diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index f107422..ff22db2 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -12,6 +12,7 @@ import { rewriteHtml, rewriteJs, rewriteWorkers, + unrewriteBlob, } from "../shared"; import type { URLMeta } from "../shared/rewriters/url"; @@ -53,9 +54,7 @@ export async function swfetch( ) { let dataurl = requesturl.pathname.substring(this.config.prefix.length); if (dataurl.startsWith("blob:")) { - let origin = new URL(dataurl.substring("blob:".length)); - dataurl = "blob:" + location.origin + origin.pathname; - console.log(dataurl); + dataurl = unrewriteBlob(dataurl); } let response: Response = await fetch(dataurl, {});