Skip to content

Commit

Permalink
Merge pull request #48 from TxtDot/fix-google
Browse files Browse the repository at this point in the history
feat: fix google parsing
  • Loading branch information
DarkCat09 authored Sep 17, 2023
2 parents 9257933 + 8fc3c2a commit 66ae522
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "txtdot",
"version": "1.2.0",
"version": "1.3.0",
"private": true,
"description": "",
"main": "dist/app.js",
Expand Down
32 changes: 5 additions & 27 deletions src/handlers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
);
}

Expand Down Expand Up @@ -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}">
Expand All @@ -76,7 +54,7 @@ export default async function google(
`;

return {
content: `${searchForm}${content.join("")}${navigation}`,
content: `${searchForm}${content.join("")}`,
textContent: textContent.join("\n"),
};
}
Expand Down

0 comments on commit 66ae522

Please sign in to comment.