-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_cleaner.ts
162 lines (151 loc) · 4.31 KB
/
dom_cleaner.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { resolve, parse } from 'url'
import { BlacklistCtrlFunc } from './blacklist';
interface CleanupProps {
baseURL?: string
debug?: boolean
isBacklisted?: BlacklistCtrlFunc
}
interface FilterFunc {
(node: Element): Element | null
}
/**
* Remove all blacklisted attributes from a HTML element.
* @param blacklist the list of attributes to remove
* @returns the filtering function
*/
export function removeAttributes(blacklist: string[]): FilterFunc {
return function (node: Element) {
blacklist.forEach(attr => node.hasAttribute(attr) && node.removeAttribute(attr))
return node
}
}
/**
* Remove a image that can be a tracker (1px square).
* @returns the filtering function
*/
export function removeImageTraker(): FilterFunc {
return function (node: Element) {
if (node.tagName !== 'IMG') {
return node
}
if (node.hasAttribute('height') || node.hasAttribute('width')) {
const height = node.getAttribute('height')
const width = node.getAttribute('width')
if (height === '1' && width === '1' && node.parentNode) {
node.parentNode.removeChild(node)
return null
}
}
return node
}
}
/**
* Delete all HTML elements that refer to a blacklisted site.
* @param isBlacklist function to control if the hostname is blacklisted
* @returns the filtering function
*/
export function removeBlacklistedLinks(isBlacklisted: BlacklistCtrlFunc): FilterFunc {
return function (node: Element) {
const src = node.getAttribute('src') || node.getAttribute('href')
if (src) {
const hostname = parse(src).hostname || 'undefined'
if (isBlacklisted(hostname) && node.parentNode) {
node.parentNode.removeChild(node)
return null
}
}
return node
}
}
/**
* Update link to target new blank window.
* @returns the filtering function
*/
export function externalizeLinks(): FilterFunc {
return function (node: Element) {
if (node.tagName !== 'A') {
return node
}
if (node.hasAttribute('href')) {
node.setAttribute('target', '_blank')
}
return node
}
}
/**
* Update relative link to be absolute.
* @param baseURL the base URL used to make the link absolute
* @returns the filtering function
*/
export function rebaseSrcAttribute(baseURL: string): FilterFunc {
const absoluteUrlRe = new RegExp('^https?://')
return function (node: Element) {
let attr = node.hasAttribute('src') ? 'src' : node.hasAttribute('href') ? 'href' : null
if (attr) {
const src = node.getAttribute(attr)
if (src && !absoluteUrlRe.test(src)) {
node.setAttribute(attr, resolve(baseURL, src))
}
}
return node
}
}
/**
* Move attribute to another.
* @param attr1 source attribute to remove
* @param attr2 target attribute
* @returns the filtering function
*/
export function moveAttribute(attr1: string, attr2: string): FilterFunc {
return function (node: Element) {
if (node.hasAttribute(attr1)) {
const val1 = node.getAttribute(attr1)
const val2 = node.getAttribute(attr2) || ''
node.setAttribute(attr2, val1 || val2)
node.removeAttribute(attr1)
}
return node
}
}
const DefaultFilterChain = [
removeAttributes(['id', 'class']),
removeImageTraker(),
externalizeLinks(),
// moveAttribute('data-src', 'src')
]
/**
* Clean a DOM using a filter chain.
* @param doc DOM to clean
* @param props properties used by filters
* @param filters chain filter
*/
export function clean(doc: Document, props: CleanupProps, filters: FilterFunc[] = DefaultFilterChain) {
if (props.baseURL) {
filters.push(rebaseSrcAttribute(props.baseURL))
}
if (props.isBacklisted) {
filters = [removeBlacklistedLinks(props.isBacklisted), ...filters]
}
const $nodes = Array.from(doc.getElementsByTagName('*'))
$nodes.forEach(($node: Element) => {
if ($node.tagName.match(/HTML|HEAD|BODY|META|LINK|STYLE/)) {
return
}
if (props.debug) {
console.log(`[DEBUG] - HTML Element: ${$node.outerHTML}`)
}
for (const filter of filters) {
const $n = filter($node)
if (!$n) {
if (props.debug) {
console.log(`[DEBUG] - HTML Element removed: ${$node.outerHTML}`)
}
break
}
$node = $n
}
if (props.debug && $node) {
console.log(`[DEBUG] - Cleaned HTML Element: ${$node.outerHTML}`)
}
});
}