Replacing all hrefs/srcs

This commit is contained in:
DarkCat09 2023-09-21 17:58:43 +04:00
parent 645542795d
commit 64054ff627
No known key found for this signature in database
GPG Key ID: 0A26CD5B3345D6E3
2 changed files with 82 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
import { generateProxyUrl } from "../utils/generate"; import { generateParserUrl, generateProxyUrl } from "../utils/generate";
import getConfig from "../config/main";
export class HandlerInput { export class HandlerInput {
private data: string; private data: string;
@ -23,6 +24,10 @@ export class HandlerInput {
this.redirectPath = redirectPath; this.redirectPath = redirectPath;
} }
getUrl(): string {
return this.url;
}
parseDom(): JSDOM { parseDom(): JSDOM {
if (this.dom) { if (this.dom) {
return this.dom; return this.dom;
@ -30,24 +35,78 @@ export class HandlerInput {
this.dom = new JSDOM(this.data, { url: this.url }); this.dom = new JSDOM(this.data, { url: this.url });
const links = this.dom.window.document.getElementsByTagName("a"); const bytag =
for (const link of links) { (dom: JSDOM, tag: string) => dom.window.document.getElementsByTagName(tag);
try { const bycss =
link.href = generateProxyUrl( (dom: JSDOM, css: string) => dom.window.document.querySelectorAll(css);
this.requestUrl,
link.href, const parserUrl = (href: string) => generateParserUrl(
this.engine, this.requestUrl,
this.redirectPath, href,
); this.engine,
} catch (_err) { this.redirectPath,
// ignore TypeError: Invalid URL );
const proxyUrl = (href: string) => generateProxyUrl(
this.requestUrl,
href,
);
this.modifyLinks(
bytag(this.dom, "a"),
"href",
parserUrl,
);
this.modifyLinks(
bycss(this.dom, "frame,iframe"),
"src",
parserUrl,
);
if (getConfig().proxy_res) {
this.modifyLinks(
bycss(this.dom, "img,image,video,audio,embed,track,source"),
"src",
proxyUrl,
);
this.modifyLinks(
bytag(this.dom, "object"),
"data",
proxyUrl,
);
const sources = bytag(this.dom, "source");
for (const source of sources) {
// split srcset by comma
// @ts-ignore
source.srcset = source.srcset.split(",").map(
(src: string) => {
// split src by space
const parts = src.split(" ");
try {
// first part is URL
parts[0] = proxyUrl(parts[0]);
} catch (_err) { }
// join by space after splitting
return parts.join(" ");
}
).join(","); // join by comma
} }
} }
return this.dom; return this.dom;
} }
getUrl(): string { private modifyLinks(
return this.url; nodeList: NodeListOf<Element> | HTMLCollectionOf<Element>,
property: string,
generateLink: (value: string) => string,
) {
for (const node of nodeList) {
try {
// @ts-ignore
node[property] = generateLink(node[property]);
} catch (_err) { }
}
} }
} }

View File

@ -6,7 +6,7 @@ export function generateRequestUrl(
return new URL(`${protocol}://${host}${originalUrl}`); return new URL(`${protocol}://${host}${originalUrl}`);
} }
export function generateProxyUrl( export function generateParserUrl(
requestUrl: URL, requestUrl: URL,
href: string, href: string,
engine?: string, engine?: string,
@ -22,3 +22,11 @@ export function generateProxyUrl(
return `${requestUrl.origin}/${redirect_url}${urlParam}${engineParam}${hash}`; return `${requestUrl.origin}/${redirect_url}${urlParam}${engineParam}${hash}`;
} }
export function generateProxyUrl(
requestUrl: URL,
href: string,
): string {
const urlParam = `?url=${encodeURIComponent(href)}`;
return `${requestUrl.origin}/proxy${urlParam}`;
}