fix: lang, textContent

This commit is contained in:
Artemy 2023-08-15 13:37:21 +03:00
parent 23a6484e20
commit 1630dbfa17

View File

@ -4,32 +4,42 @@ import { IHandlerOutput } from "./handler.interface";
export default async function google( export default async function google(
window: DOMWindow window: DOMWindow
): Promise<IHandlerOutput> { ): Promise<IHandlerOutput> {
const searchEl = window.document.querySelectorAll( 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)" "#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 (!searchEl) { if (!googleAnchors) {
throw new Error("Failed to find search element [google]"); throw new Error("Failed to find anchors in search result [google]");
} }
const results = [...searchEl]; const results = [...googleAnchors];
const content = results.map((result) => { const convertToFormat = (result: Element, isHtml: boolean) => {
const anchor = result as HTMLAnchorElement; const anchor = result as HTMLAnchorElement;
const heading = anchor.childNodes[1] as HTMLHeadingElement; const heading = anchor.childNodes[1] as HTMLHeadingElement;
return `<p><a href="${anchor.href}">${heading.innerHTML}</p>`; return isHtml
? `<p><a href="${anchor.href}">${heading.innerHTML}</p>`
: `${heading.innerHTML} > ${anchor.href}`;
};
const content = results.map((result) => {
return convertToFormat(result, true);
});
const textContent = results.map((result) => {
return convertToFormat(result, false);
}); });
const searchForm = ` const searchForm = `
<div id="searchform" method="get" id="searchform" method="get"> <form onsubmit="window.location.href = '/?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+'); return false">
<input type="text" name="q" id="q"> <input type="text" name="q" id="q">
<input type="button" value="Search" onclick="window.location.href = '/?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+');"> <input type="button" value="Search" onclick="window.location.href = '/?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+');">
</div> </form>
`; `;
return { return {
content: `${searchForm}${content.join("")}`, content: `${searchForm}${content.join("")}`,
textContent: "parsed.textContent", textContent: textContent.join("\n"),
title: window.document.title, title: window.document.title,
lang: "parsed.lang", lang: window.document.documentElement.lang,
}; };
} }