blaze/index.ts

190 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-06-18 21:20:52 +00:00
import express from "express";
import { Readability, isProbablyReaderable } from "@mozilla/readability";
2023-06-18 21:20:52 +00:00
import got from "got";
2023-06-18 22:44:17 +00:00
import path from "path";
import { fileURLToPath } from "url";
2023-06-19 12:11:22 +00:00
import "dotenv/config";
2023-06-21 00:26:24 +00:00
import { parseHTML, parseJSON } from "linkedom";
2023-06-20 10:19:17 +00:00
// @ts-ignore
import XHR2 from "xhr2";
const XMLHttpRequest = XHR2.XMLHttpRequest;
import { minify } from "html-minifier";
2023-06-20 23:00:31 +00:00
import { blazeUrl, injectBlazeToPageLinks } from "./utils.js";
2023-06-18 21:20:52 +00:00
const app = express();
const port = 8888;
2023-06-18 21:20:52 +00:00
const minifierOptions = {
collapseWhitespace: true,
removeComments: true,
removeOptionalTags: true,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeTagWhitespace: true,
useShortDoctype: true,
minifyCSS: true,
};
2023-06-18 22:46:43 +00:00
// @ts-ignore
2023-06-18 22:44:17 +00:00
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
app.get("/", async (req, res) => {
const searchEngine = "https://api.search.brave.com/res/v1/web/search";
2023-06-20 10:19:17 +00:00
const query = req.query.q as string;
2023-06-18 21:20:52 +00:00
if (!query) {
2023-06-20 10:19:17 +00:00
return res.sendFile(path.join(__dirname, "/dist/index.html"));
2023-06-18 21:20:52 +00:00
}
2023-06-20 07:32:07 +00:00
const key = process.env.CYCLIC_BRAVE_KEY;
2023-06-19 12:01:17 +00:00
if (!key) {
throw new Error("No brave key found");
}
2023-06-20 10:19:17 +00:00
try {
const xhr = new XMLHttpRequest();
2023-06-20 23:00:31 +00:00
xhr.open("GET", `${searchEngine}?q=${query}&safesearch=moderate`, true);
2023-06-20 10:19:17 +00:00
xhr.setRequestHeader("Accept", "*/*");
xhr.setRequestHeader("X-Subscription-Token", key);
xhr.onreadystatechange = () => {
if (xhr.readyState !== 4) {
return;
}
if (xhr.status !== 200) {
console.error("XHR request failed:", xhr.status, xhr.statusText);
return;
}
const data = JSON.parse(xhr.responseText);
// @ts-ignore
const results = data.web.results.map(
(result: any) => `
2023-06-20 07:45:18 +00:00
<article>
2023-06-20 23:00:31 +00:00
<a href="${blazeUrl}/blazed?url=${result.url}">
2023-06-20 10:19:17 +00:00
<h2>${result.title}</h2>
</a>
2023-06-19 13:27:14 +00:00
<span>${result.meta_url.hostname}</span>
<p>${result.description}</p>
2023-06-20 07:45:18 +00:00
</article>
2023-06-19 12:11:22 +00:00
<hr />
2023-06-20 10:19:17 +00:00
`
);
const html = `
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Blaze - ${query}</title>
<style>
body {font-family:sans-serif}
h2 {margin-bottom:0}
span {font-size:.9rem}
</style>
</head>
<body>
${results.join("")}
</body>
</html>
`;
const minifiedSerp = minify(html, minifierOptions);
res.send(minifiedSerp);
2023-06-20 10:19:17 +00:00
};
xhr.send();
} catch (err) {
console.error(err);
}
2023-06-18 21:20:52 +00:00
});
2023-06-18 22:52:20 +00:00
2023-06-20 09:48:11 +00:00
app.get("/blazed", async (req, res) => {
2023-06-20 07:32:07 +00:00
const pageToBlaze = req.query.url as string;
2023-06-20 09:48:11 +00:00
try {
2023-06-20 10:59:26 +00:00
const response = await got(pageToBlaze, {
headers: { Accept: "text/html" },
});
2023-06-20 09:48:11 +00:00
const { document } = parseHTML(response.body);
2023-06-21 00:26:24 +00:00
// TODO: missing handling of 404. The idea is to send the blaze 404 page, otherwise will show error page on client
2023-06-20 09:48:11 +00:00
if (!isProbablyReaderable(document)) {
2023-06-21 00:26:24 +00:00
// TODO: send minimalized version of the page instead the read mode
// implementation draft:
// document.querySelectorAll("link").forEach((l) => {
// l.remove();
// });
// document.querySelectorAll("style").forEach((s) => {
// s.remove;
// });
// document.querySelectorAll("script").forEach((s) => {
// s.remove();
// });
// // @ts-ignore
// const jsonDocument = document.toJSON();
// const cleanDocument = parseJSON(jsonDocument);
// return res.send(document.toString());
2023-06-20 09:48:11 +00:00
return res.sendFile(path.join(__dirname, "/dist/not_blazed.html"));
}
2023-06-20 07:32:07 +00:00
2023-06-20 10:36:47 +00:00
//TODO: find if there are more performant ways to remove images or evaluate if is the case to remove images
document.querySelectorAll("img").forEach((img) => img.remove());
2023-06-20 09:48:11 +00:00
const reader = new Readability(document);
const article = reader.parse();
2023-06-20 07:32:07 +00:00
2023-06-20 09:48:11 +00:00
if (!article) {
return res.send("Something went wrong");
}
2023-06-20 07:32:07 +00:00
const blazedPage = `<html><head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<style>body {font-family: sans-serif}</style>
</head>
<body>
${article.content}
2023-06-20 23:00:31 +00:00
<script>
${injectBlazeToPageLinks}
const url = "${blazeUrl}"
2023-06-21 00:26:24 +00:00
const currentUrl = "${req.query.url}"
injectBlazeToPageLinks(url, currentUrl)
2023-06-20 23:00:31 +00:00
</script>
</body></html>
`;
const minifiedBlazedPage = minify(blazedPage, minifierOptions);
res.send(minifiedBlazedPage);
2023-06-20 09:48:11 +00:00
} catch (err) {
console.log(err);
}
2023-06-20 07:32:07 +00:00
});
2023-06-19 12:55:28 +00:00
app.get("/info", (_, res) => {
res.sendFile(path.join(__dirname + "/dist/info.html"));
2023-06-20 07:32:07 +00:00
});
app.get("/ooops", (_, res) => {
res.sendFile(path.join(__dirname + "/dist/info_not_blazed.html"));
});
2023-06-19 12:55:28 +00:00
2023-06-20 07:41:34 +00:00
app.get("/favicon.svg", (_, res) => {
res.sendFile(path.join(__dirname + "/favicon.svg"));
});
2023-06-18 22:52:20 +00:00
app.listen(port, () => {
console.log(`Got request`);
});