Merge pull request #24 from TxtDot/google-add-nav
feat: pagination in google and website names
This commit is contained in:
commit
0ee5a3ffee
@ -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;
|
||||
}
|
||||
|
@ -62,3 +62,7 @@ main {
|
||||
background: var(--bg2);
|
||||
color: var(--fg);
|
||||
}
|
||||
|
||||
a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user