-
Notifications
You must be signed in to change notification settings - Fork 170
/
html.ts
45 lines (42 loc) · 1.33 KB
/
html.ts
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
import type { Comment, Element } from "https://deno.land/x/lol_html@0.0.3/types.d.ts";
import initLolHtml, { HTMLRewriter } from "https://deno.land/x/lol_html@0.0.3/mod.js";
import decodeLolHtmlWasm from "https://deno.land/x/lol_html@0.0.3/wasm.js";
import util from "./util.ts";
await initLolHtml(decodeLolHtmlWasm());
export function parseHtmlLinks(html: string | Uint8Array): Promise<string[]> {
return new Promise((resolve, reject) => {
try {
const links: string[] = [];
const rewriter = new HTMLRewriter("utf8", (_chunk: Uint8Array) => {});
const linkHandler = {
element(el: Element) {
const href = el.getAttribute("href");
if (href) {
links.push(href);
}
},
};
const scriptHandler = {
nomoduleInserted: false,
element(el: Element) {
const src = el.getAttribute("src");
if (src) {
links.push(src);
}
},
};
rewriter.on("link", linkHandler);
rewriter.on("script", scriptHandler);
try {
rewriter.write(typeof html === "string" ? util.utf8TextEncoder.encode(html) : html);
rewriter.end();
} finally {
rewriter.free();
}
resolve(links);
} catch (error) {
reject(error);
}
});
}
export { Comment, Element, HTMLRewriter };