Public
Script
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
export function cleanHtml(html: string) {
// Remove script, link tags
html = html.replace(
/<\s*(script|link|time|form)\b[^<]*(?:(?!<\/\s*\1\s*>)<[^<]*)*<\/\s*\1\s*>/gi,
"",
);
// Remove inputs because date initial values can change
html = html.replace(/<\s*(input)\b[^<]*\s*>/gi, "");
// Remove style attributes
html = html.replace(/ style="[^"]*"/gi, "");
// Separate all tags by newlines
html = html.replace(/><+/g, ">\n<");
// Remove leading/trailing whitespace on each line
html = html.replace(/^\s+|\s+$/gm, "");
// Remove blank lines
html = html.replace(/\n\s*\n/g, "\n");
// Parse out only the contents of the body
var bodyStart = html.indexOf("<body");
if (bodyStart !== -1) {
bodyStart = html.indexOf(">", bodyStart) + 1;
var bodyEnd = html.indexOf("</body", bodyStart);
if (bodyEnd !== -1) {
html = html.substring(bodyStart, bodyEnd);
}
}
return html;
}
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!
January 5, 2024