import { DOMWindow } from "jsdom"; import { IHandlerOutput } from "./handler.interface"; export default async function google( window: DOMWindow ): Promise { 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 (!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; 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 search = window.document.getElementById( "APjFqb" ) as HTMLTextAreaElement; const searchForm = `
`; return { content: `${searchForm}${content.join("")}`, textContent: textContent.join("\n"), title: window.document.title, lang: window.document.documentElement.lang, }; }