blaze/index.ts

51 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-06-18 21:20:52 +00:00
import express from "express";
import { Readability } from "@mozilla/readability";
import { JSDOM } from "jsdom";
import got from "got";
2023-06-18 22:44:17 +00:00
import path from "path";
import { fileURLToPath } from "url";
2023-06-18 21:20:52 +00:00
const app = express();
const port = 5763;
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);
2023-06-18 21:20:52 +00:00
app.get("/", (req, res) => {
2023-06-18 23:22:20 +00:00
let url = req.query.at as string;
2023-06-18 21:20:52 +00:00
if (!url) {
2023-06-18 22:44:17 +00:00
res.sendFile(path.join(__dirname + "/dist/index.html"));
2023-06-18 21:20:52 +00:00
}
2023-06-18 23:22:20 +00:00
var re = new RegExp("^(http|https)://", "i");
var match = re.test(url);
if (!match) {
url = `https://${url}`
}
2023-06-18 21:20:52 +00:00
got(url)
.then((response) => {
const dom = new JSDOM(response.body);
let reader = new Readability(dom.window.document);
let article = reader.parse();
if (!article) {
res.send("Something went wrong");
return;
}
res.send(article.content);
})
.catch((err) => {
console.log(err);
});
});
2023-06-18 22:52:20 +00:00
app.listen(port, () => {
console.log(`Got request`);
});