feat: names of sites

This commit is contained in:
Artemy 2023-08-17 18:17:15 +03:00
parent f99504fe7d
commit 9f1a3dc7c8

View File

@ -5,24 +5,37 @@ import { EngineParseError } from "../errors/main";
export default async function google( export default async function google(
window: DOMWindow window: DOMWindow
): Promise<IHandlerOutput> { ): Promise<IHandlerOutput> {
const googleAnchors = window.document.querySelectorAll("a[jsname=ACyKwe]"); const googleAnchors = [
...window.document.querySelectorAll("a[jsname=ACyKwe]"),
] as HTMLAnchorElement[];
const googleNames = [...window.document.querySelectorAll(".VuuXrf")];
const results = googleAnchors.map(
(a: HTMLAnchorElement, i: number): GoogleProps => {
return {
href: a.href!,
siteName: googleNames[i].textContent!,
heading: a.childNodes[1].textContent!,
};
}
);
if (!googleAnchors) { if (!googleAnchors) {
throw new EngineParseError( throw new EngineParseError(
"Failed to find anchors in search result [google]" "Failed to find anchors in search result [google]"
); );
} }
const results = [...googleAnchors];
const convertToFormat = (result: Element, isHtml: boolean) => { if (!googleNames) {
const anchor = result as HTMLAnchorElement; throw new EngineParseError(
const heading = anchor.childNodes[1] as HTMLHeadingElement; "Failed to find names in search result [google]"
if (!heading) { );
return "";
} }
const convertToFormat = (result: GoogleProps, isHtml: boolean) => {
return isHtml return isHtml
? `<p><a href="${anchor.href}">${heading.innerHTML}</p>` ? `<p><a href="${result.href}">${result.siteName} - ${result.heading}</p>`
: `${heading.innerHTML} > ${anchor.href}`; : `${result.siteName} - ${result.heading} > ${result.href}`;
}; };
const content = results.map((result) => { const content = results.map((result) => {
@ -73,3 +86,9 @@ export default async function google(
lang: window.document.documentElement.lang, lang: window.document.documentElement.lang,
}; };
} }
interface GoogleProps {
href: string;
siteName: string;
heading: string;
}