html-get 2.28.2 → 2.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/package.json +1 -1
- package/src/index.js +3 -0
- package/src/mark-hidden.js +135 -0
package/README.md
CHANGED
|
@@ -171,6 +171,8 @@ The value `auto` means that that internally use a list of websites that don't ne
|
|
|
171
171
|
|
|
172
172
|
See [getMode parameter](#getMode) for know more.
|
|
173
173
|
|
|
174
|
+
A prerendered page comes back with the `hidden` attribute on every element the browser does not show (`display: none`, `visibility: hidden` or `opacity: 0`), so a closed cookie banner or modal is skipped by readers that honor `hidden`, such as Readability and defuddle. Only the outermost hidden element is marked. An element hidden with `visibility: hidden` is walked into when a descendant sets `visibility: visible`, and its own text is wrapped in a `<span hidden>`. Shadow DOM content is judged by its rendered shadow tree and marked in the flattened copy that is returned. Content inside a collapsed tab or accordion is marked too. `fetch` mode returns the HTML as served.
|
|
175
|
+
|
|
174
176
|
##### puppeteerOpts
|
|
175
177
|
|
|
176
178
|
Type: `object`
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "html-get",
|
|
3
3
|
"description": "Get the HTML from any website, fine-tuned for correction & speed",
|
|
4
4
|
"homepage": "https://nicedoc.com/microlinkhq/html-get",
|
|
5
|
-
"version": "2.
|
|
5
|
+
"version": "2.29.0",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"main": "src/index.js",
|
|
8
8
|
"bin": {
|
package/src/index.js
CHANGED
|
@@ -21,6 +21,7 @@ const { extractSafe, isPdf } = require('./pdf')
|
|
|
21
21
|
const { firstImage } = require('./pdf/first-image')
|
|
22
22
|
const autoDomains = require('./auto-domains')
|
|
23
23
|
const addHtml = require('./html')
|
|
24
|
+
const markHidden = require('./mark-hidden')
|
|
24
25
|
|
|
25
26
|
const REQ_TIMEOUT = 8000
|
|
26
27
|
|
|
@@ -280,6 +281,8 @@ const prerender = PCancelable.fn(
|
|
|
280
281
|
async (page, response) => {
|
|
281
282
|
if (!response) throw new AbortError('empty response')
|
|
282
283
|
|
|
284
|
+
await markHidden(page)
|
|
285
|
+
|
|
283
286
|
return {
|
|
284
287
|
headers: response.headers(),
|
|
285
288
|
html: await page.content(),
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const debug = require('debug-logfmt')('html-get:mark-hidden')
|
|
4
|
+
|
|
5
|
+
const VISIBILITY_OPTIONS = { visibilityProperty: true, opacityProperty: true }
|
|
6
|
+
|
|
7
|
+
const markHiddenInPage = options => {
|
|
8
|
+
const { Node, NodeFilter, getComputedStyle } = window
|
|
9
|
+
const HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'
|
|
10
|
+
const NEVER_RENDERED = new Set([
|
|
11
|
+
'AREA',
|
|
12
|
+
'BASE',
|
|
13
|
+
'BASEFONT',
|
|
14
|
+
'DATALIST',
|
|
15
|
+
'LINK',
|
|
16
|
+
'META',
|
|
17
|
+
'NOEMBED',
|
|
18
|
+
'NOFRAMES',
|
|
19
|
+
'NOSCRIPT',
|
|
20
|
+
'PARAM',
|
|
21
|
+
'RP',
|
|
22
|
+
'SCRIPT',
|
|
23
|
+
'SOURCE',
|
|
24
|
+
'STYLE',
|
|
25
|
+
'TEMPLATE',
|
|
26
|
+
'TITLE',
|
|
27
|
+
'TRACK'
|
|
28
|
+
])
|
|
29
|
+
const { body } = document
|
|
30
|
+
if (!body || !body.checkVisibility(options)) return 0
|
|
31
|
+
|
|
32
|
+
const knownToHaveVisibleDescendant = new Set()
|
|
33
|
+
const rememberAncestors = element => {
|
|
34
|
+
for (let node = element.parentElement; node; node = node.parentElement) {
|
|
35
|
+
knownToHaveVisibleDescendant.add(node)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const descendantsOf = element =>
|
|
40
|
+
element.shadowRoot
|
|
41
|
+
? [...element.shadowRoot.querySelectorAll('*'), ...element.querySelectorAll('*')]
|
|
42
|
+
: element.querySelectorAll('*')
|
|
43
|
+
|
|
44
|
+
const hasVisibleDescendant = element => {
|
|
45
|
+
if (knownToHaveVisibleDescendant.has(element)) return true
|
|
46
|
+
for (const descendant of descendantsOf(element)) {
|
|
47
|
+
const shown =
|
|
48
|
+
descendant.checkVisibility(options) ||
|
|
49
|
+
(descendant.shadowRoot !== null && hasVisibleDescendant(descendant))
|
|
50
|
+
if (shown) {
|
|
51
|
+
rememberAncestors(descendant)
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const isMarkedAsAWhole = element =>
|
|
59
|
+
element.namespaceURI !== HTML_NAMESPACE || element.tagName === 'SELECT'
|
|
60
|
+
|
|
61
|
+
const lightCopies = host => {
|
|
62
|
+
if (host.innerHTML !== host.shadowRoot.innerHTML) return null
|
|
63
|
+
const lightElements = host.querySelectorAll('*')
|
|
64
|
+
return new Map(
|
|
65
|
+
[...host.shadowRoot.querySelectorAll('*')].map((element, index) => [
|
|
66
|
+
element,
|
|
67
|
+
lightElements[index]
|
|
68
|
+
])
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const findHidden = root => {
|
|
73
|
+
const hidden = []
|
|
74
|
+
const hiddenText = []
|
|
75
|
+
|
|
76
|
+
const findInShadow = host => {
|
|
77
|
+
const copies = lightCopies(host)
|
|
78
|
+
if (!copies) return false
|
|
79
|
+
const inShadow = findHidden(host.shadowRoot)
|
|
80
|
+
hidden.push(...inShadow.hidden.map(element => copies.get(element)))
|
|
81
|
+
hiddenText.push(...inShadow.hiddenText.map(element => copies.get(element)))
|
|
82
|
+
return true
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const decide = element => {
|
|
86
|
+
if (NEVER_RENDERED.has(element.tagName)) return NodeFilter.FILTER_REJECT
|
|
87
|
+
const shown = element.checkVisibility(options)
|
|
88
|
+
if (shown && isMarkedAsAWhole(element)) return NodeFilter.FILTER_REJECT
|
|
89
|
+
const style = shown ? null : getComputedStyle(element)
|
|
90
|
+
const hidesOwnText = !shown && style.visibility !== 'visible'
|
|
91
|
+
const showsDescendants =
|
|
92
|
+
shown || style.display === 'contents' || (hidesOwnText && hasVisibleDescendant(element))
|
|
93
|
+
if (!showsDescendants) {
|
|
94
|
+
hidden.push(element)
|
|
95
|
+
return NodeFilter.FILTER_REJECT
|
|
96
|
+
}
|
|
97
|
+
if (element.shadowRoot) {
|
|
98
|
+
if (findInShadow(element) && hidesOwnText) hiddenText.push(element)
|
|
99
|
+
return NodeFilter.FILTER_REJECT
|
|
100
|
+
}
|
|
101
|
+
if (hidesOwnText) hiddenText.push(element)
|
|
102
|
+
return NodeFilter.FILTER_SKIP
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT, { acceptNode: decide }).nextNode()
|
|
106
|
+
return { hidden, hiddenText }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const hideText = element => {
|
|
110
|
+
let wrapped = 0
|
|
111
|
+
for (const node of [...element.childNodes]) {
|
|
112
|
+
if (node.nodeType !== Node.TEXT_NODE || !node.data.trim()) continue
|
|
113
|
+
const span = document.createElement('span')
|
|
114
|
+
span.hidden = true
|
|
115
|
+
node.replaceWith(span)
|
|
116
|
+
span.append(node)
|
|
117
|
+
wrapped++
|
|
118
|
+
}
|
|
119
|
+
return wrapped
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const { hidden, hiddenText } = findHidden(body)
|
|
123
|
+
for (const element of hidden) element.setAttribute('hidden', '')
|
|
124
|
+
return hiddenText.reduce((marked, element) => marked + hideText(element), hidden.length)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
module.exports = async page => {
|
|
128
|
+
try {
|
|
129
|
+
const marked = await page.evaluate(markHiddenInPage, VISIBILITY_OPTIONS)
|
|
130
|
+
debug({ marked })
|
|
131
|
+
return marked
|
|
132
|
+
} catch (error) {
|
|
133
|
+
debug('error', { message: error.message })
|
|
134
|
+
}
|
|
135
|
+
}
|