-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.js
More file actions
88 lines (73 loc) · 2.06 KB
/
app.js
File metadata and controls
88 lines (73 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Ensure console.log spits out timestamps
require("log-timestamp");
// Express
const app = require("express")();
const bodyParser = require("body-parser").json();
const port = 3000;
// HTTP client
const axios = require("axios").default;
// Readability, dom and dom purify
const { JSDOM } = require("jsdom");
const { Readability } = require("@mozilla/readability");
const createDOMPurify = require("dompurify");
const DOMPurify = createDOMPurify(new JSDOM("").window);
// Not too happy to allow iframe, but it's the only way to get youtube vids
const domPurifyOptions = {
ADD_TAGS: ["iframe", "video"],
};
app.get("/", (req, res) => {
return res.status(400).send({
error: 'POST (not GET) JSON, like so: {"url": "https://url/to/whatever"}',
}).end;
});
app.post("/", bodyParser, (req, res) => {
const url = req.body.url;
if (url === undefined || url === "") {
return res
.status(400)
.send({
error: 'Send JSON, like so: {"url": "https://url/to/whatever"}',
})
.end();
}
console.log("Fetching " + url + "...");
axios
.get(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (X11; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0",
},
})
.then((response) => {
const sanitized = DOMPurify.sanitize(response.data, domPurifyOptions);
const dom = new JSDOM(sanitized, {
url: url,
});
const parsed = new Readability(dom.window.document).parse();
console.log("Fetched and parsed " + url + " successfully");
return res
.status(200)
.send({
url,
...parsed,
})
.end();
})
.catch((error) => {
return res
.status(500)
.send({
error: "Some weird error fetching the content",
details: error,
})
.end();
});
});
// Start server and dump current server version
const version = require("fs")
.readFileSync("./release")
.toString()
.split(" ")[0];
app.listen(port, () =>
console.log(`Readability.js server v${version} listening on port ${port}!`)
);