refactor: change handlers argument to window
This commit is contained in:
parent
c1dfd4d517
commit
239994ac5e
@ -1,9 +1,10 @@
|
|||||||
import axios from "../types/axios";
|
|
||||||
import { IHandlerOutput } from "./handler.interface";
|
import { IHandlerOutput } from "./handler.interface";
|
||||||
import { readability } from "./readability";
|
|
||||||
|
import axios from "../types/axios";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
|
||||||
type EngineFunction = (url: Document) => Promise<IHandlerOutput>;
|
import readability from "./readability";
|
||||||
|
import { DOMWindow } from "jsdom";
|
||||||
|
|
||||||
export default async function handlePage(
|
export default async function handlePage(
|
||||||
url: string,
|
url: string,
|
||||||
@ -15,27 +16,29 @@ export default async function handlePage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.get(url);
|
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);
|
const UrlParsed = new URL(originalUrl);
|
||||||
|
|
||||||
[...document.getElementsByTagName("a")].forEach((link) => {
|
[...window.document.getElementsByTagName("a")].forEach((link) => {
|
||||||
link.href = `${UrlParsed.origin}/?url=${link.href}${
|
link.href = `${UrlParsed.origin}/?url=${link.href}${
|
||||||
engine && `&engine=${engine}`
|
engine && `&engine=${engine}`
|
||||||
}`;
|
}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (engine) {
|
if (engine) {
|
||||||
return engines[engine](document);
|
return engines[engine](window);
|
||||||
}
|
}
|
||||||
|
|
||||||
const host = new URL(url).hostname;
|
const host = new URL(url).hostname;
|
||||||
return fallback[host](document) || fallback["*"](document);
|
return fallback[host](window) || fallback["*"](window);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Engines {
|
interface Engines {
|
||||||
[key: string]: EngineFunction;
|
[key: string]: EngineFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EngineFunction = (window: DOMWindow) => Promise<IHandlerOutput>;
|
||||||
|
|
||||||
export const engines: Engines = {
|
export const engines: Engines = {
|
||||||
readability,
|
readability,
|
||||||
};
|
};
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
import { Readability } from "@mozilla/readability";
|
import { Readability } from "@mozilla/readability";
|
||||||
import { IHandlerOutput } from "./handler.interface";
|
import { IHandlerOutput } from "./handler.interface";
|
||||||
|
import { DOMWindow } from "jsdom";
|
||||||
|
|
||||||
export async function readability(document: Document): Promise<IHandlerOutput> {
|
export default async function readability(
|
||||||
const reader = new Readability(document);
|
window: DOMWindow
|
||||||
|
): Promise<IHandlerOutput> {
|
||||||
|
const reader = new Readability(window.document);
|
||||||
const parsed = reader.parse();
|
const parsed = reader.parse();
|
||||||
|
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user