2023-06-18 21:20:52 +00:00
|
|
|
import express from "express";
|
2023-06-19 10:05:59 +00:00
|
|
|
import { Readability, isProbablyReaderable } from "@mozilla/readability";
|
2023-06-18 21:20:52 +00:00
|
|
|
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();
|
2023-06-19 10:05:59 +00:00
|
|
|
const port = 8888;
|
2023-06-18 21:20:52 +00:00
|
|
|
|
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-19 10:33:56 +00:00
|
|
|
const searchEngine = "https://html.duckduckgo.com/html/";
|
2023-06-19 10:05:59 +00:00
|
|
|
let query = req.query.q as string;
|
2023-06-18 21:20:52 +00:00
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
if (!query) {
|
2023-06-18 22:44:17 +00:00
|
|
|
res.sendFile(path.join(__dirname + "/dist/index.html"));
|
2023-06-19 10:05:59 +00:00
|
|
|
return
|
2023-06-18 21:20:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
got(`${searchEngine}?q=${query}`)
|
2023-06-18 21:20:52 +00:00
|
|
|
.then((response) => {
|
2023-06-19 10:20:11 +00:00
|
|
|
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
console.log("Error code:", response.statusCode)
|
|
|
|
res.send(`Error code ${response.statusCode}`)
|
|
|
|
return;
|
|
|
|
}
|
2023-06-19 10:33:56 +00:00
|
|
|
|
2023-06-18 21:20:52 +00:00
|
|
|
const dom = new JSDOM(response.body);
|
2023-06-19 10:05:59 +00:00
|
|
|
const links = dom.window.document.querySelectorAll('link')
|
|
|
|
const results = dom.window.document.querySelectorAll('.result')
|
|
|
|
|
|
|
|
// add custom style
|
|
|
|
const style = dom.window.document.createElement('style')
|
|
|
|
dom.window.document.querySelector('head')?.appendChild(style)
|
|
|
|
style.innerHTML = `
|
|
|
|
.result {
|
|
|
|
padding: 1rem;
|
|
|
|
border: 1px solid black;
|
|
|
|
margin-bottom: 10px:
|
|
|
|
}
|
|
|
|
|
|
|
|
.result__snippet, .result__url {
|
|
|
|
text-decoration: none;
|
|
|
|
color: black
|
|
|
|
}
|
|
|
|
`
|
|
|
|
// clean up dom
|
|
|
|
links.forEach(linkTag => linkTag.remove())
|
|
|
|
dom.window.document.querySelector('form')?.remove()
|
|
|
|
dom.window.document.querySelector('#header')?.remove()
|
2023-06-18 21:20:52 +00:00
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
// modify urls
|
|
|
|
results.forEach(result => {
|
|
|
|
const hrefArray = result.querySelector('.result__snippet')?.getAttribute('href')?.split('')
|
|
|
|
hrefArray?.splice(0,25)
|
|
|
|
const cleanHref = hrefArray?.join('')
|
2023-06-19 10:09:21 +00:00
|
|
|
const newHref = `https://ill-red-skunk-wig.cyclic.app/blazed?url=${cleanHref}`;
|
2023-06-19 10:05:59 +00:00
|
|
|
result.querySelector('.result__snippet')?.setAttribute('href', newHref)
|
|
|
|
result.querySelector('.result__url')?.setAttribute('href', newHref)
|
|
|
|
result.querySelector('.result__a')?.setAttribute('href', newHref)
|
|
|
|
})
|
2023-06-18 21:20:52 +00:00
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
res.send(dom.serialize())
|
|
|
|
return;
|
2023-06-18 21:20:52 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
});
|
2023-06-18 22:52:20 +00:00
|
|
|
|
2023-06-19 10:05:59 +00:00
|
|
|
app.get("/blazed", (req, res) => {
|
|
|
|
const pageToBlaze = req.query.url as string
|
|
|
|
|
|
|
|
got(pageToBlaze)
|
|
|
|
.then((response) => {
|
|
|
|
const dom = new JSDOM(response.body);
|
|
|
|
|
|
|
|
if (!isProbablyReaderable(dom.window.document)) {
|
|
|
|
res.send("This page can not be blazed... Sorry!")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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`);
|
|
|
|
});
|