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(
|
export default async function google(
|
||||||
window: DOMWindow
|
window: DOMWindow
|
||||||
): Promise<IHandlerOutput> {
|
): 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) {
|
if (!googleAnchors) {
|
||||||
throw new EngineParseError(
|
throw new EngineParseError(
|
||||||
"Failed to find anchors in search result [google]"
|
"Failed to find anchors in search result [google]"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const results = [...googleAnchors];
|
|
||||||
|
|
||||||
const convertToFormat = (result: Element, isHtml: boolean) => {
|
if (!googleNames) {
|
||||||
const anchor = result as HTMLAnchorElement;
|
throw new EngineParseError(
|
||||||
const heading = anchor.childNodes[1] as HTMLHeadingElement;
|
"Failed to find names in search result [google]"
|
||||||
if (!heading) {
|
);
|
||||||
return "";
|
}
|
||||||
}
|
|
||||||
|
const convertToFormat = (result: GoogleProps, isHtml: boolean) => {
|
||||||
return isHtml
|
return isHtml
|
||||||
? `<p><a href="${anchor.href}">${heading.innerHTML}</p>`
|
? `<p><a href="${result.href}">${result.siteName} - ${result.heading}</p>`
|
||||||
: `${heading.innerHTML} > ${anchor.href}`;
|
: `${result.siteName} - ${result.heading} > ${result.href}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
const content = results.map((result) => {
|
const content = results.map((result) => {
|
||||||
@ -44,10 +57,38 @@ export default async function google(
|
|||||||
</form>
|
</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 {
|
return {
|
||||||
content: `${searchForm}${content.join("")}`,
|
content: `${searchForm}${content.join("")}${navigation}`,
|
||||||
textContent: textContent.join("\n"),
|
textContent: textContent.join("\n"),
|
||||||
title: window.document.title,
|
title: window.document.title,
|
||||||
lang: window.document.documentElement.lang,
|
lang: window.document.documentElement.lang,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface GoogleProps {
|
||||||
|
href: string;
|
||||||
|
siteName: string;
|
||||||
|
heading: string;
|
||||||
|
}
|
||||||
|
@ -62,3 +62,7 @@ main {
|
|||||||
background: var(--bg2);
|
background: var(--bg2);
|
||||||
color: var(--fg);
|
color: var(--fg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a {
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user