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" "APjFqb"
) as HTMLTextAreaElement; ) 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 = [ const navLinks = [
...window.document.querySelectorAll( ...window.document.querySelectorAll(
"table[class=AaVjTc] > tbody > tr > td > a" "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>`; return `<td><a href="${link.href}">${link.innerHTML}</a></td>`;
}); });
const currPage = ( const currPage =
window.document.querySelector(".YyVfkd") as HTMLTableCellElement (window.document.querySelector(".YyVfkd") as HTMLTableCellElement)
).cellIndex; ?.cellIndex || 0;
const pageTd = `<td>${currPage}</td>`; const pageTd = `<td>${currPage}</td>`;
if (currPage === 1) navLinks.splice(currPage - 1, 0, pageTd); 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> const navigation = `<table>
<tbody><tr>${navLinks.join("")}</tr></tbody> <tbody><tr>${navLinks.join("")}</tr></tbody>
</table>`; </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 { return {
content: `${searchForm}${content.join("")}${navigation}`, content: `${searchForm}${content.join("")}${navigation}`,
textContent: textContent.join("\n"), textContent: textContent.join("\n"),

View File

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

View File

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

View File

@ -1,22 +1,13 @@
import { EngineRequest, IParseSchema, parseSchema } from "../types/requests"; import { EngineRequest, IParseSchema, parseSchema } from "../types/requests";
import { FastifyInstance } from "fastify"; import { FastifyInstance } from "fastify";
import handlePage from "../handlers/main"; import handlePage from "../handlers/main";
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>(
"/parse", "/parse",
{ schema: parseSchema }, { schema: parseSchema },
async (request: EngineRequest) => { async (request: EngineRequest) => {
return await handlePage( return await handlePage(request.query.url, request.query.engine);
request.query.url,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
),
request.query.engine
);
} }
); );
} }

View File

@ -2,23 +2,13 @@ import { FastifyInstance } from "fastify";
import { GetRequest, IParseSchema, rawHtmlSchema } from "../types/requests"; import { GetRequest, IParseSchema, rawHtmlSchema } from "../types/requests";
import handlePage from "../handlers/main"; import handlePage from "../handlers/main";
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>(
"/raw-html", "/raw-html",
{ schema: rawHtmlSchema }, { schema: rawHtmlSchema },
async (request: GetRequest) => { async (request: GetRequest) => {
return ( return (await handlePage(request.query.url)).content;
await handlePage(
request.query.url,
generateRequestUrl(
request.protocol,
request.hostname,
request.originalUrl
)
)
).content;
} }
); );
} }

View File

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