fix: raw html links and engine

This commit is contained in:
Artemy Egorov 2023-08-26 12:00:07 +03:00
parent 739d029339
commit a3567cfb34
3 changed files with 11 additions and 6 deletions

View File

@ -19,7 +19,8 @@ import {
export default async function handlePage( export default async function handlePage(
url: string, // remote URL url: string, // remote URL
requestUrl: URL, // proxy URL requestUrl: URL, // proxy URL
engine?: string engine?: string,
redirect_path: string = "get"
): Promise<IHandlerOutput> { ): Promise<IHandlerOutput> {
const urlObj = new URL(url); const urlObj = new URL(url);
@ -38,7 +39,7 @@ export default async function handlePage(
[...window.document.getElementsByTagName("a")].forEach((link) => { [...window.document.getElementsByTagName("a")].forEach((link) => {
try { try {
link.href = generateProxyUrl(requestUrl, link.href, engine); link.href = generateProxyUrl(requestUrl, link.href, engine, redirect_path);
} catch (_err) { } catch (_err) {
// ignore TypeError: Invalid URL // ignore TypeError: Invalid URL
} }

View File

@ -9,7 +9,8 @@ export default async function rawHtml(fastify: FastifyInstance) {
fastify.get<IParseSchema>( fastify.get<IParseSchema>(
"/api/raw-html", "/api/raw-html",
{ schema: rawHtmlSchema }, { schema: rawHtmlSchema },
async (request) => { async (request, reply) => {
reply.type("text/html; charset=utf-8");
return ( return (
await handlePage( await handlePage(
request.query.url, request.query.url,
@ -17,7 +18,9 @@ export default async function rawHtml(fastify: FastifyInstance) {
request.protocol, request.protocol,
request.hostname, request.hostname,
request.originalUrl request.originalUrl
) ),
request.query.engine,
"api/raw-html"
) )
).content; ).content;
} }

View File

@ -9,7 +9,8 @@ export function generateRequestUrl(
export function generateProxyUrl( export function generateProxyUrl(
requestUrl: URL, requestUrl: URL,
href: string, href: string,
engine?: string engine?: string,
redirect_url: string = "get"
): string { ): string {
const parsedHref = new URL(href); const parsedHref = new URL(href);
@ -19,5 +20,5 @@ export function generateProxyUrl(
const urlParam = `?url=${encodeURIComponent(parsedHref.toString())}`; const urlParam = `?url=${encodeURIComponent(parsedHref.toString())}`;
const engineParam = engine ? `&engine=${engine}` : ""; const engineParam = engine ? `&engine=${engine}` : "";
return `${requestUrl.origin}/get${urlParam}${engineParam}${hash}`; return `${requestUrl.origin}/${redirect_url}${urlParam}${engineParam}${hash}`;
} }