From f99504fe7d9cc87e3d8c5e0bbc4e4faacc2384f4 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 17 Aug 2023 17:58:09 +0300 Subject: [PATCH 1/2] feat: pagination in google --- src/handlers/google.ts | 24 +++++++++++++++++++++++- static/common.css | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/handlers/google.ts b/src/handlers/google.ts index c3c3d7d..a547c18 100644 --- a/src/handlers/google.ts +++ b/src/handlers/google.ts @@ -44,8 +44,30 @@ export default async function google( `; + const navLinks = [ + ...window.document.querySelectorAll( + "table[class=AaVjTc] > tbody > tr > td > a" + ), + ].map((l) => { + const link = l as HTMLAnchorElement; + return `${link.innerHTML}`; + }); + + const currPage = ( + window.document.querySelector(".YyVfkd") as HTMLTableCellElement + ).cellIndex; + + const pageTd = `${currPage}`; + + if (currPage === 1) navLinks.splice(currPage - 1, 0, pageTd); + else navLinks.splice(currPage, 0, pageTd); + + const navigation = ` + ${navLinks.join("")} +
`; + return { - content: `${searchForm}${content.join("")}`, + content: `${searchForm}${content.join("")}${navigation}`, textContent: textContent.join("\n"), title: window.document.title, lang: window.document.documentElement.lang, diff --git a/static/common.css b/static/common.css index 58f8e49..7c455f4 100644 --- a/static/common.css +++ b/static/common.css @@ -62,3 +62,7 @@ main { background: var(--bg2); color: var(--fg); } + +a { + text-decoration: none; +} From 9f1a3dc7c88b05f5f7fccac9d99c7f289e528b19 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 17 Aug 2023 18:17:15 +0300 Subject: [PATCH 2/2] 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; +}