From 3015dff77d1dcdcb75b8b55858ba30b351336390 Mon Sep 17 00:00:00 2001 From: Terry <187264680+gennaroterry@users.noreply.github.com> Date: Mon, 11 Nov 2024 08:35:10 +0000 Subject: [PATCH 1/4] fix: Improve srcset rewriter to handle descriptors correctly. The previous implementation failed to properly rewrite srcset attributes when they contained descriptors (like 1.5x, 2x). Updated the parser to preserve descriptors while rewriting URLs. --- src/shared/rewriters/html.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/shared/rewriters/html.ts b/src/shared/rewriters/html.ts index 6e6b8f4..7fddf3f 100644 --- a/src/shared/rewriters/html.ts +++ b/src/shared/rewriters/html.ts @@ -283,17 +283,20 @@ function traverseParsedHtml( } export function rewriteSrcset(srcset: string, meta: URLMeta) { - const urls = srcset.split(/ [0-9]+x,? ?/g); - if (!urls) return ""; - const sufixes = srcset.match(/ [0-9]+x,? ?/g); - if (!sufixes) return ""; - const rewrittenUrls = urls.map((url, i) => { - if (url && sufixes[i]) { - return rewriteUrl(url, meta) + sufixes[i]; - } + const sources = srcset.split(",").map((src) => src.trim()); + const rewrittenSources = sources.map((source) => { + // Split into URLs and descriptors (if any) + // e.g. url0, url1 1.5x, url2 2x + const [url, ...descriptors] = source.split(/\s+/); + + // Rewrite the URLs and keep the descriptors (if any) + const rewrittenUrl = rewriteUrl(url.trim(), meta); + return descriptors.length > 0 + ? `${rewrittenUrl} ${descriptors.join(" ")}` + : rewrittenUrl; }); - return rewrittenUrls.join(""); + return rewrittenSources.join(", "); } // function base64ToBytes(base64) { From 4455930d28d2b31df6a0a8260fa72d1183591f73 Mon Sep 17 00:00:00 2001 From: Terry <187264680+gennaroterry@users.noreply.github.com> Date: Mon, 11 Nov 2024 13:38:51 +0000 Subject: [PATCH 2/4] chore: enhance worker error message readability and details. --- src/worker/error.ts | 42 +++++++++++++++++++++++++++++++++++++----- src/worker/fetch.ts | 22 ++++++++++++++++++++-- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/worker/error.ts b/src/worker/error.ts index 03e458c..229de07 100644 --- a/src/worker/error.ts +++ b/src/worker/error.ts @@ -9,6 +9,14 @@ export function errorTemplate(trace: string, fetchedURL: string) { reload.addEventListener("click", () => location.reload()); version.textContent = ${JSON.stringify($scramjet.version.version)}; build.textContent = ${JSON.stringify($scramjet.version.build)}; + + document.getElementById('copy-button').addEventListener('click', async () => { + const text = document.getElementById('errorTrace').value; + await navigator.clipboard.writeText(text); + const btn = document.getElementById('copy-button'); + btn.textContent = 'Copied!'; + setTimeout(() => btn.textContent = 'Copy', 2000); + }); `; return ` @@ -54,6 +62,7 @@ export function errorTemplate(trace: string, fetchedURL: string) { resize: none; height: 20em; text-align: left; + color: red; font-family: var(--font-monospace); } @@ -94,10 +103,11 @@ export function errorTemplate(trace: string, fetchedURL: string) { } #version-wrapper { - width: 100%; - text-align: center; + width: auto; + text-align: right; position: absolute; - bottom: 0.2rem; + top: 0.5rem; + right: 0.5rem; font-size: 0.8rem; color: var(--shore)!important; i { @@ -107,6 +117,26 @@ export function errorTemplate(trace: string, fetchedURL: string) { } z-index: 101; } + + #errorTrace-wrapper { + position: relative; + width: fit-content; + } + + #copy-button { + position: absolute; + top: 0.5em; + right: 0.5em; + padding: 0.23em; + cursor: pointer; + opacity: 0; + transition: opacity 0.4s; + font-size: 0.9em; + } + + #errorTrace-wrapper:hover #copy-button { + opacity: 1; + }
@@ -117,8 +147,10 @@ export function errorTemplate(trace: string, fetchedURL: string) {Try: