1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { DOMParser } from "https://esm.sh/linkedom";
export default async function server(request: Request): Promise<Response> {
if (request.method !== "GET") {
return new Response("Method not allowed", { status: 405 });
}
const url = new URL(request.url);
const targetUrl = url.searchParams.get("url");
if (!targetUrl) {
return new Response("Missing 'url' query parameter", { status: 400 });
}
try {
const response = await fetch(targetUrl);
if (!response.ok) {
throw new Error(await response.text());
}
const html = await response.text();
const document = new DOMParser().parseFromString(html, "text/html");
const script = document.querySelector<HTMLScriptElement>("script[type=\"application/ld+json\"]");
return new Response(script.textContent, {
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
} catch (error) {
console.error("Error fetching or processing URL:", error);
return new Response("Error processing the URL", { status: 500 });
}
}