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 fastifySwaggerUi from "@fastify/swagger-ui";
|
||||||
import ejs from "ejs";
|
import ejs from "ejs";
|
||||||
|
|
||||||
import getRoute from "./routes/get";
|
import getRoute from "./routes/browser/get";
|
||||||
import parseRoute from "./routes/parse";
|
import parseRoute from "./routes/api/parse";
|
||||||
import indexRoute from "./routes/index";
|
import indexRoute from "./routes/browser/index";
|
||||||
import rawHtml from "./routes/raw-html";
|
import rawHtml from "./routes/api/raw-html";
|
||||||
|
|
||||||
import publicConfig from "./publicConfig";
|
import publicConfig from "./publicConfig";
|
||||||
import errorHandler from "./errors/handler";
|
import errorHandler from "./errors/handler";
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
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 handlePage from "../../handlers/main";
|
||||||
import { generateRequestUrl } from "../utils/generate";
|
import { generateRequestUrl } from "../../utils/generate";
|
||||||
|
|
||||||
export default async function parseRoute(fastify: FastifyInstance) {
|
export default async function parseRoute(fastify: FastifyInstance) {
|
||||||
fastify.get<IParseSchema>(
|
fastify.get<IParseSchema>(
|
@ -1,15 +1,16 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
|
|
||||||
import { IParseSchema, rawHtmlSchema } from "../types/requests/api";
|
import { IParseSchema, rawHtmlSchema } from "../../types/requests/api";
|
||||||
|
|
||||||
import handlePage from "../handlers/main";
|
import handlePage from "../../handlers/main";
|
||||||
import { generateRequestUrl } from "../utils/generate";
|
import { generateRequestUrl } from "../../utils/generate";
|
||||||
|
|
||||||
export default async function rawHtml(fastify: FastifyInstance) {
|
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;
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
|
|
||||||
import { GetSchema, IGetSchema } from "../types/requests/browser";
|
import { GetSchema, IGetSchema } from "../../types/requests/browser";
|
||||||
import handlePage from "../handlers/main";
|
import handlePage from "../../handlers/main";
|
||||||
import { generateRequestUrl } from "../utils/generate";
|
import { generateRequestUrl } from "../../utils/generate";
|
||||||
|
|
||||||
export default async function getRoute(fastify: FastifyInstance) {
|
export default async function getRoute(fastify: FastifyInstance) {
|
||||||
fastify.get<IGetSchema>(
|
fastify.get<IGetSchema>(
|
@ -1,6 +1,6 @@
|
|||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import { engineList } from "../handlers/main";
|
import { engineList } from "../../handlers/main";
|
||||||
import { indexSchema } from "../types/requests/browser";
|
import { indexSchema } from "../../types/requests/browser";
|
||||||
|
|
||||||
export default async function indexRoute(fastify: FastifyInstance) {
|
export default async function indexRoute(fastify: FastifyInstance) {
|
||||||
fastify.get("/", { schema: indexSchema }, async (_, reply) => {
|
fastify.get("/", { schema: indexSchema }, async (_, reply) => {
|
@ -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}`;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user