Moved engine selecting logic into separate function
This commit is contained in:
parent
da67666a6c
commit
f6dc1f953e
@ -4,6 +4,8 @@ import axios from "../types/axios";
|
|||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
import { DOMWindow } from "jsdom";
|
import { DOMWindow } from "jsdom";
|
||||||
|
|
||||||
|
import micromatch from "micromatch";
|
||||||
|
|
||||||
import readability from "./readability";
|
import readability from "./readability";
|
||||||
import google, { GoogleDomains } from "./google";
|
import google, { GoogleDomains } from "./google";
|
||||||
import stackoverflow, { StackOverflowDomains } from "./stackoverflow/main";
|
import stackoverflow, { StackOverflowDomains } from "./stackoverflow/main";
|
||||||
@ -11,8 +13,6 @@ import stackoverflow, { StackOverflowDomains } from "./stackoverflow/main";
|
|||||||
import { generateProxyUrl } from "../utils/generate";
|
import { generateProxyUrl } from "../utils/generate";
|
||||||
import isLocalResource from "../utils/islocal";
|
import isLocalResource from "../utils/islocal";
|
||||||
|
|
||||||
import micromatch from "micromatch";
|
|
||||||
|
|
||||||
import { LocalResourceError, NotHtmlMimetypeError } from "../errors/main";
|
import { LocalResourceError, NotHtmlMimetypeError } from "../errors/main";
|
||||||
|
|
||||||
export default async function handlePage(
|
export default async function handlePage(
|
||||||
@ -49,20 +49,19 @@ export default async function handlePage(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (engine) {
|
return getFallbackEngine(urlObj.hostname, engine)(window);
|
||||||
return engines[engine](window);
|
}
|
||||||
|
|
||||||
|
function getFallbackEngine(host: string, specified?: string): EngineFunction {
|
||||||
|
if (specified) {
|
||||||
|
return engines[specified];
|
||||||
}
|
}
|
||||||
|
for (const engine of fallback) {
|
||||||
const title = window.document.title;
|
if (micromatch.isMatch(host, engine.pattern)) {
|
||||||
const lang = window.document.documentElement.lang;
|
return engine.engine;
|
||||||
|
|
||||||
for (const match of fallback) {
|
|
||||||
if (micromatch.isMatch(urlObj.hostname, match.pattern)) {
|
|
||||||
return { title, lang, ...match.engine(window) };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return engines.readability;
|
||||||
return engines.readability(window);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Engines {
|
interface Engines {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user