Merge pull request #25 from TxtDot/fix-proxy-url

Fix proxy url
This commit is contained in:
Artemy Egorov 2023-08-18 09:55:10 +03:00 committed by GitHub
commit 944e42e857
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 52 deletions

View File

@ -50,13 +50,6 @@ export default async function google(
"APjFqb"
) as HTMLTextAreaElement;
const searchForm = `
<form onsubmit="window.location.href = '/get?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+'); return false">
<input type="text" name="q" id="q" value="${search?.value}">
<input type="button" value="Search" onclick="window.location.href = '/get?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+');">
</form>
`;
const navLinks = [
...window.document.querySelectorAll(
"table[class=AaVjTc] > tbody > tr > td > a"
@ -66,19 +59,26 @@ export default async function google(
return `<td><a href="${link.href}">${link.innerHTML}</a></td>`;
});
const currPage = (
window.document.querySelector(".YyVfkd") as HTMLTableCellElement
).cellIndex;
const currPage =
(window.document.querySelector(".YyVfkd") as HTMLTableCellElement)
?.cellIndex || 0;
const pageTd = `<td>${currPage}</td>`;
if (currPage === 1) navLinks.splice(currPage - 1, 0, pageTd);
else navLinks.splice(currPage, 0, pageTd);
else if (currPage !== 0) navLinks.splice(currPage, 0, pageTd);
const navigation = `<table>
<tbody><tr>${navLinks.join("")}</tr></tbody>
</table>`;
const searchForm = `
<form onsubmit="window.location.href = '/get?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+'); return false">
<input type="text" name="q" id="q" value="${search?.value}">
<input type="button" value="Search" onclick="window.location.href = '/get?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+');">
</form>
`;
return {
content: `${searchForm}${content.join("")}${navigation}`,
textContent: textContent.join("\n"),

View File

@ -18,7 +18,6 @@ import {
export default async function handlePage(
url: string, // remote URL
requestUrl: URL, // proxy URL
engine?: string
): Promise<IHandlerOutput> {
const urlObj = new URL(url);
@ -41,7 +40,7 @@ export default async function handlePage(
const window = new JSDOM(response.data, { url }).window;
[...window.document.getElementsByTagName("a")].forEach((link) => {
link.href = generateProxyUrl(requestUrl, link.href, engine);
link.href = generateProxyUrl(link.href, engine);
});
if (engine) {

View File

@ -2,7 +2,6 @@ import { FastifyInstance } from "fastify";
import { GetSchema, IGetSchema } from "../types/requests";
import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils/generate";
export default async function getRoute(fastify: FastifyInstance) {
fastify.get<IGetSchema>(
@ -12,15 +11,7 @@ export default async function getRoute(fastify: FastifyInstance) {
const remoteUrl = request.query.url;
const engine = request.query.engine;
const parsed = await handlePage(
remoteUrl,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
engine
);
const parsed = await handlePage(remoteUrl, engine);
if (request.query.format === "text") {
reply.type("text/plain; charset=utf-8");

View File

@ -1,22 +1,13 @@
import { EngineRequest, IParseSchema, parseSchema } from "../types/requests";
import { FastifyInstance } from "fastify";
import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils/generate";
export default async function parseRoute(fastify: FastifyInstance) {
fastify.get<IParseSchema>(
"/parse",
{ schema: parseSchema },
async (request: EngineRequest) => {
return await handlePage(
request.query.url,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
request.query.engine
);
return await handlePage(request.query.url, request.query.engine);
}
);
}

View File

@ -2,23 +2,13 @@ import { FastifyInstance } from "fastify";
import { GetRequest, IParseSchema, rawHtmlSchema } from "../types/requests";
import handlePage from "../handlers/main";
import { generateRequestUrl } from "../utils/generate";
export default async function rawHtml(fastify: FastifyInstance) {
fastify.get<IParseSchema>(
"/raw-html",
{ schema: rawHtmlSchema },
async (request: GetRequest) => {
return (
await handlePage(
request.query.url,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
)
)
).content;
return (await handlePage(request.query.url)).content;
}
);
}

View File

@ -1,18 +1,13 @@
export function generateRequestUrl(
protocol: string,
host: string,
originalUrl: string,
originalUrl: string
): URL {
return new URL(`${protocol}://${host}${originalUrl}`);
}
export function generateProxyUrl(
requestUrl: URL,
href: string,
engine?: string,
): string {
export function generateProxyUrl(href: string, engine?: string): string {
const urlParam = `?url=${encodeURIComponent(href)}`;
const engineParam = engine ? `&engine=${engine}` : "";
// formatParam is not needed for now
return requestUrl.origin + "/get" + urlParam + engineParam;
return `/get${urlParam}${engineParam}`;
}