diff --git a/src/handlers/google.ts b/src/handlers/google.ts index b5fc05d..ef7729d 100644 --- a/src/handlers/google.ts +++ b/src/handlers/google.ts @@ -4,32 +4,42 @@ import { IHandlerOutput } from "./handler.interface"; export default async function google( window: DOMWindow ): Promise { - const searchEl = window.document.querySelectorAll( + const googleAnchors = window.document.querySelectorAll( "#rso > div > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(1)" ); - if (!searchEl) { - throw new Error("Failed to find search element [google]"); + if (!googleAnchors) { + throw new Error("Failed to find anchors in search result [google]"); } - const results = [...searchEl]; + const results = [...googleAnchors]; - const content = results.map((result) => { + const convertToFormat = (result: Element, isHtml: boolean) => { const anchor = result as HTMLAnchorElement; const heading = anchor.childNodes[1] as HTMLHeadingElement; - return `

${heading.innerHTML}

`; + return isHtml + ? `

${heading.innerHTML}

` + : `${heading.innerHTML} > ${anchor.href}`; + }; + + const content = results.map((result) => { + return convertToFormat(result, true); + }); + + const textContent = results.map((result) => { + return convertToFormat(result, false); }); const searchForm = ` -
+
-
+ `; return { content: `${searchForm}${content.join("")}`, - textContent: "parsed.textContent", + textContent: textContent.join("\n"), title: window.document.title, - lang: "parsed.lang", + lang: window.document.documentElement.lang, }; }