From 9f1a3dc7c88b05f5f7fccac9d99c7f289e528b19 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 17 Aug 2023 18:17:15 +0300 Subject: [PATCH] feat: names of sites --- src/handlers/google.ts | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/handlers/google.ts b/src/handlers/google.ts index a547c18..0e3f5c3 100644 --- a/src/handlers/google.ts +++ b/src/handlers/google.ts @@ -5,24 +5,37 @@ import { EngineParseError } from "../errors/main"; export default async function google( window: DOMWindow ): Promise { - 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) { throw new EngineParseError( "Failed to find anchors in search result [google]" ); } - const results = [...googleAnchors]; - const convertToFormat = (result: Element, isHtml: boolean) => { - const anchor = result as HTMLAnchorElement; - const heading = anchor.childNodes[1] as HTMLHeadingElement; - if (!heading) { - return ""; - } + if (!googleNames) { + throw new EngineParseError( + "Failed to find names in search result [google]" + ); + } + + const convertToFormat = (result: GoogleProps, isHtml: boolean) => { return isHtml - ? `

${heading.innerHTML}

` - : `${heading.innerHTML} > ${anchor.href}`; + ? `

${result.siteName} - ${result.heading}

` + : `${result.siteName} - ${result.heading} > ${result.href}`; }; const content = results.map((result) => { @@ -73,3 +86,9 @@ export default async function google( lang: window.document.documentElement.lang, }; } + +interface GoogleProps { + href: string; + siteName: string; + heading: string; +}