mirror of
https://github.com/txtdot/txtdot
synced 2024-10-18 14:40:19 +03:00
Replacing all hrefs/srcs
This commit is contained in:
parent
645542795d
commit
64054ff627
@ -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);
|
||||||
|
|
||||||
|
const parserUrl = (href: string) => generateParserUrl(
|
||||||
this.requestUrl,
|
this.requestUrl,
|
||||||
link.href,
|
href,
|
||||||
this.engine,
|
this.engine,
|
||||||
this.redirectPath,
|
this.redirectPath,
|
||||||
);
|
);
|
||||||
} catch (_err) {
|
const proxyUrl = (href: string) => generateProxyUrl(
|
||||||
// ignore TypeError: Invalid URL
|
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) { }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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}`;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user