Merge pull request #48 from TxtDot/fix-google

feat: fix google parsing
This commit is contained in:
Andrey 2023-09-17 12:38:45 +04:00 committed by GitHub
commit 66ae5226b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 30 deletions

View File

@ -1,6 +1,9 @@
<h1 align="center">
<a href="https://github.com/TxtDot/txtdot"><img src="https://github.com/TxtDot/.github/blob/main/imgs/TXTDot%20gh.png?raw=true" alt="txt." width="200"></a>
<br>
<a href="https://txtdot.github.io/documentation"><img alt="Documentation" src="https://img.shields.io/badge/Documentation-green"></a>
<a href="https://github.com/TxtDot/instances"><img alt="Instances" src="https://img.shields.io/badge/Instances-green"></a>
<br>
<a href="https://github.com/TxtDot/txtdot/blob/main/LICENSE"><img alt="MIT license" src="https://img.shields.io/github/license/txtdot/txtdot?color=blue"></a>
<a href="https://github.com/TxtDot/txtdot/releases/latest"><img alt="Latest release" src="https://img.shields.io/github/v/release/TxtDot/txtdot?display_name=release"></a>
<a href="https://matrix.to/#/#txtdot:matrix.org"><img alt="Matrix chat" src="https://img.shields.io/badge/chat-matrix-blue"></a>

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "txtdot",
"version": "1.2.0",
"version": "1.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "txtdot",
"version": "1.2.0",
"version": "1.3.0",
"license": "MIT",
"dependencies": {
"@fastify/static": "^6.10.2",

View File

@ -1,6 +1,6 @@
{
"name": "txtdot",
"version": "1.2.0",
"version": "1.3.0",
"private": true,
"description": "",
"main": "dist/app.js",

View File

@ -3,17 +3,17 @@ import { IHandlerOutput } from "./handler.interface";
import { EngineParseError } from "../errors/main";
export default async function google(
input: HandlerInput,
input: HandlerInput
): Promise<IHandlerOutput> {
const window = input.parseDom().window;
const googleAnchors = [
...window.document.querySelectorAll("a[jsname=ACyKwe]"),
...window.document.querySelectorAll("a[jsname=UWckNb]"),
] as HTMLAnchorElement[];
if (!googleAnchors) {
throw new EngineParseError(
"Failed to find anchors in search result [google]",
"Failed to find anchors in search result [google]"
);
}
@ -43,31 +43,9 @@ export default async function google(
});
const search = window.document.getElementById(
"APjFqb",
"APjFqb"
) as HTMLTextAreaElement;
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 || 0;
const pageTd = `<td>${currPage}</td>`;
if (currPage === 1) navLinks.splice(currPage - 1, 0, pageTd);
else if (currPage !== 0) navLinks.splice(currPage, 0, pageTd);
const navigation = `<table>
<tbody><tr>${navLinks.join("")}</tr></tbody>
</table>`;
const searchForm = `
<form onsubmit="window.location.href = '/get?url=https://www.google.com/search?q='+ document.getElementById('q').value.split(' ').join('+'); return false">
<input type="text" name="q" id="q" value="${search?.value}">
@ -76,7 +54,7 @@ export default async function google(
`;
return {
content: `${searchForm}${content.join("")}${navigation}`,
content: `${searchForm}${content.join("")}`,
textContent: textContent.join("\n"),
};
}