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
import { DOMParser } from "https://deno.land/x/deno_dom/deno-dom-wasm.ts";
import { Readability } from "npm:@mozilla/readability";
// inspired by https://www.val.town/v/stevekrouse/fetchJSON
interface Readable {
title: string;
content: string;
textContent: string;
length: number;
excerpt: string;
byline: string;
dir: string;
siteName: string;
lang: string;
publishedTime: string;
// url is final url after redirects
url: string;
}
export const fetchReadable = async (url: string): Promise<Readable> => {
const r = await fetch(url, { headers: { "content-type": "text/html" }, redirect: "follow" });
try {
const text = await r.text();
const parser = new DOMParser();
const node = parser.parseFromString(text, "text/html");
const relative = node.createElement("base");
relative.href = url;
node.head.appendChild(relative);
let data = new Readability(node).parse();
data.url = r.url;
return data;
} catch (err) {
throw new Error(`fetchReadable error: ${err.message} in ${url}`);
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v31
January 1, 2024