From f6dc1f953e071be7fc88f2cb0727ab73c58ccf9a Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 11 Sep 2023 11:16:26 +0400 Subject: [PATCH] Moved engine selecting logic into separate function --- src/handlers/main.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 61e0bb3..392b08e 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -4,6 +4,8 @@ import axios from "../types/axios"; import { JSDOM } from "jsdom"; import { DOMWindow } from "jsdom"; +import micromatch from "micromatch"; + import readability from "./readability"; import google, { GoogleDomains } from "./google"; import stackoverflow, { StackOverflowDomains } from "./stackoverflow/main"; @@ -11,8 +13,6 @@ import stackoverflow, { StackOverflowDomains } from "./stackoverflow/main"; import { generateProxyUrl } from "../utils/generate"; import isLocalResource from "../utils/islocal"; -import micromatch from "micromatch"; - import { LocalResourceError, NotHtmlMimetypeError } from "../errors/main"; export default async function handlePage( @@ -49,20 +49,19 @@ export default async function handlePage( } }); - if (engine) { - return engines[engine](window); + return getFallbackEngine(urlObj.hostname, engine)(window); +} + +function getFallbackEngine(host: string, specified?: string): EngineFunction { + if (specified) { + return engines[specified]; } - - const title = window.document.title; - const lang = window.document.documentElement.lang; - - for (const match of fallback) { - if (micromatch.isMatch(urlObj.hostname, match.pattern)) { - return { title, lang, ...match.engine(window) }; + for (const engine of fallback) { + if (micromatch.isMatch(host, engine.pattern)) { + return engine.engine; } } - - return engines.readability(window); + return engines.readability; } interface Engines {