Merge pull request #24 from TxtDot/google-add-nav

feat: pagination in google and website names
This commit is contained in:
Andrey 2023-08-17 19:54:45 +04:00 committed by GitHub
commit 0ee5a3ffee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 11 deletions

View File

@ -5,24 +5,37 @@ import { EngineParseError } from "../errors/main";
export default async function google(
window: DOMWindow
): 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) {
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
? `<p><a href="${anchor.href}">${heading.innerHTML}</p>`
: `${heading.innerHTML} > ${anchor.href}`;
? `<p><a href="${result.href}">${result.siteName} - ${result.heading}</p>`
: `${result.siteName} - ${result.heading} > ${result.href}`;
};
const content = results.map((result) => {
@ -44,10 +57,38 @@ export default async function google(
</form>
`;
const navLinks = [
...window.document.querySelectorAll(
"table[class=AaVjTc] > tbody > tr > td > a"
),
].map((l) => {
const link = l as HTMLAnchorElement;
return `<td><a href="${link.href}">${link.innerHTML}</a></td>`;
});
const currPage = (
window.document.querySelector(".YyVfkd") as HTMLTableCellElement
).cellIndex;
const pageTd = `<td>${currPage}</td>`;
if (currPage === 1) navLinks.splice(currPage - 1, 0, pageTd);
else navLinks.splice(currPage, 0, pageTd);
const navigation = `<table>
<tbody><tr>${navLinks.join("")}</tr></tbody>
</table>`;
return {
content: `${searchForm}${content.join("")}`,
content: `${searchForm}${content.join("")}${navigation}`,
textContent: textContent.join("\n"),
title: window.document.title,
lang: window.document.documentElement.lang,
};
}
interface GoogleProps {
href: string;
siteName: string;
heading: string;
}

View File

@ -62,3 +62,7 @@ main {
background: var(--bg2);
color: var(--fg);
}
a {
text-decoration: none;
}