refactor: change handlers argument to window

This commit is contained in:
Artemy 2023-08-15 11:18:08 +03:00
parent c1dfd4d517
commit 239994ac5e
2 changed files with 15 additions and 9 deletions

View File

@ -1,9 +1,10 @@
import axios from "../types/axios";
import { IHandlerOutput } from "./handler.interface";
import { readability } from "./readability";
import axios from "../types/axios";
import { JSDOM } from "jsdom";
type EngineFunction = (url: Document) => Promise<IHandlerOutput>;
import readability from "./readability";
import { DOMWindow } from "jsdom";
export default async function handlePage(
url: string,
@ -15,27 +16,29 @@ export default async function handlePage(
}
const response = await axios.get(url);
const document = new JSDOM(response.data, { url: url }).window.document;
const window = new JSDOM(response.data, { url: url }).window;
const UrlParsed = new URL(originalUrl);
[...document.getElementsByTagName("a")].forEach((link) => {
[...window.document.getElementsByTagName("a")].forEach((link) => {
link.href = `${UrlParsed.origin}/?url=${link.href}${
engine && `&engine=${engine}`
}`;
});
if (engine) {
return engines[engine](document);
return engines[engine](window);
}
const host = new URL(url).hostname;
return fallback[host](document) || fallback["*"](document);
return fallback[host](window) || fallback["*"](window);
}
interface Engines {
[key: string]: EngineFunction;
}
type EngineFunction = (window: DOMWindow) => Promise<IHandlerOutput>;
export const engines: Engines = {
readability,
};

View File

@ -1,8 +1,11 @@
import { Readability } from "@mozilla/readability";
import { IHandlerOutput } from "./handler.interface";
import { DOMWindow } from "jsdom";
export async function readability(document: Document): Promise<IHandlerOutput> {
const reader = new Readability(document);
export default async function readability(
window: DOMWindow
): Promise<IHandlerOutput> {
const reader = new Readability(window.document);
const parsed = reader.parse();
if (!parsed) {