Merge pull request #39 from TxtDot/raw-html-fix-and-refactor
Raw html fix and refactor
This commit is contained in:
commit
fee72debf4
@ -9,10 +9,10 @@ import fastifySwagger from "@fastify/swagger";
|
||||
import fastifySwaggerUi from "@fastify/swagger-ui";
|
||||
import ejs from "ejs";
|
||||
|
||||
import getRoute from "./routes/get";
|
||||
import parseRoute from "./routes/parse";
|
||||
import indexRoute from "./routes/index";
|
||||
import rawHtml from "./routes/raw-html";
|
||||
import getRoute from "./routes/browser/get";
|
||||
import parseRoute from "./routes/api/parse";
|
||||
import indexRoute from "./routes/browser/index";
|
||||
import rawHtml from "./routes/api/raw-html";
|
||||
|
||||
import publicConfig from "./publicConfig";
|
||||
import errorHandler from "./errors/handler";
|
||||
|
@ -19,7 +19,8 @@ import {
|
||||
export default async function handlePage(
|
||||
url: string, // remote URL
|
||||
requestUrl: URL, // proxy URL
|
||||
engine?: string
|
||||
engine?: string,
|
||||
redirect_path: string = "get"
|
||||
): Promise<IHandlerOutput> {
|
||||
const urlObj = new URL(url);
|
||||
|
||||
@ -38,7 +39,7 @@ export default async function handlePage(
|
||||
|
||||
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
||||
try {
|
||||
link.href = generateProxyUrl(requestUrl, link.href, engine);
|
||||
link.href = generateProxyUrl(requestUrl, link.href, engine, redirect_path);
|
||||
} catch (_err) {
|
||||
// ignore TypeError: Invalid URL
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
|
||||
import { EngineRequest, IParseSchema, parseSchema } from "../types/requests/api";
|
||||
import { EngineRequest, IParseSchema, parseSchema } from "../../types/requests/api";
|
||||
|
||||
import handlePage from "../handlers/main";
|
||||
import { generateRequestUrl } from "../utils/generate";
|
||||
import handlePage from "../../handlers/main";
|
||||
import { generateRequestUrl } from "../../utils/generate";
|
||||
|
||||
export default async function parseRoute(fastify: FastifyInstance) {
|
||||
fastify.get<IParseSchema>(
|
@ -1,15 +1,16 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
|
||||
import { IParseSchema, rawHtmlSchema } from "../types/requests/api";
|
||||
import { IParseSchema, rawHtmlSchema } from "../../types/requests/api";
|
||||
|
||||
import handlePage from "../handlers/main";
|
||||
import { generateRequestUrl } from "../utils/generate";
|
||||
import handlePage from "../../handlers/main";
|
||||
import { generateRequestUrl } from "../../utils/generate";
|
||||
|
||||
export default async function rawHtml(fastify: FastifyInstance) {
|
||||
fastify.get<IParseSchema>(
|
||||
"/api/raw-html",
|
||||
{ schema: rawHtmlSchema },
|
||||
async (request) => {
|
||||
async (request, reply) => {
|
||||
reply.type("text/html; charset=utf-8");
|
||||
return (
|
||||
await handlePage(
|
||||
request.query.url,
|
||||
@ -17,7 +18,9 @@ export default async function rawHtml(fastify: FastifyInstance) {
|
||||
request.protocol,
|
||||
request.hostname,
|
||||
request.originalUrl
|
||||
)
|
||||
),
|
||||
request.query.engine,
|
||||
"api/raw-html"
|
||||
)
|
||||
).content;
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
|
||||
import { GetSchema, IGetSchema } from "../types/requests/browser";
|
||||
import handlePage from "../handlers/main";
|
||||
import { generateRequestUrl } from "../utils/generate";
|
||||
import { GetSchema, IGetSchema } from "../../types/requests/browser";
|
||||
import handlePage from "../../handlers/main";
|
||||
import { generateRequestUrl } from "../../utils/generate";
|
||||
|
||||
export default async function getRoute(fastify: FastifyInstance) {
|
||||
fastify.get<IGetSchema>(
|
@ -1,6 +1,6 @@
|
||||
import { FastifyInstance } from "fastify";
|
||||
import { engineList } from "../handlers/main";
|
||||
import { indexSchema } from "../types/requests/browser";
|
||||
import { engineList } from "../../handlers/main";
|
||||
import { indexSchema } from "../../types/requests/browser";
|
||||
|
||||
export default async function indexRoute(fastify: FastifyInstance) {
|
||||
fastify.get("/", { schema: indexSchema }, async (_, reply) => {
|
@ -9,7 +9,8 @@ export function generateRequestUrl(
|
||||
export function generateProxyUrl(
|
||||
requestUrl: URL,
|
||||
href: string,
|
||||
engine?: string
|
||||
engine?: string,
|
||||
redirect_url: string = "get"
|
||||
): string {
|
||||
const parsedHref = new URL(href);
|
||||
|
||||
@ -19,5 +20,5 @@ export function generateProxyUrl(
|
||||
const urlParam = `?url=${encodeURIComponent(parsedHref.toString())}`;
|
||||
const engineParam = engine ? `&engine=${engine}` : "";
|
||||
|
||||
return `${requestUrl.origin}/get${urlParam}${engineParam}${hash}`;
|
||||
return `${requestUrl.origin}/${redirect_url}${urlParam}${engineParam}${hash}`;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user