html-get 2.28.1 → 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/index.d.ts +2 -0
- package/package.json +1 -1
- package/src/index.js +46 -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/index.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ declare namespace htmlGet {
|
|
|
16
16
|
statusCode: number | undefined
|
|
17
17
|
/** Redirect history */
|
|
18
18
|
redirects: Array<{ statusCode: number; url: string }>
|
|
19
|
+
/** Set when the body stopped arriving (a timeout or a dropped connection): `html` is what arrived before it */
|
|
20
|
+
partial?: true
|
|
19
21
|
/** Statistics about the request */
|
|
20
22
|
stats: {
|
|
21
23
|
mode: 'fetch' | 'prerender'
|
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
|
@@ -9,6 +9,7 @@ const PCancelable = require('p-cancelable')
|
|
|
9
9
|
const { AbortError } = require('p-retry')
|
|
10
10
|
const htmlEncode = require('html-encode')
|
|
11
11
|
const got = require('@kikobeats/got')
|
|
12
|
+
const { isResponseOk } = require('@kikobeats/got/dist/source/core/utils/is-response-ok')
|
|
12
13
|
const crypto = require('crypto')
|
|
13
14
|
const $ = require('tinyspawn')
|
|
14
15
|
const path = require('path')
|
|
@@ -20,6 +21,7 @@ const { extractSafe, isPdf } = require('./pdf')
|
|
|
20
21
|
const { firstImage } = require('./pdf/first-image')
|
|
21
22
|
const autoDomains = require('./auto-domains')
|
|
22
23
|
const addHtml = require('./html')
|
|
24
|
+
const markHidden = require('./mark-hidden')
|
|
23
25
|
|
|
24
26
|
const REQ_TIMEOUT = 8000
|
|
25
27
|
|
|
@@ -43,6 +45,42 @@ const inheritRequestFragment = (options, response) => {
|
|
|
43
45
|
if (!options.url.href.includes('#')) options.url.hash = new URL(response.url).hash
|
|
44
46
|
}
|
|
45
47
|
|
|
48
|
+
const HTML_DOCUMENT_TYPES = ['text/html', 'application/xhtml+xml']
|
|
49
|
+
|
|
50
|
+
const resolvesWithBody = res => isResponseOk(res) || !res.request.options.throwHttpErrors
|
|
51
|
+
|
|
52
|
+
const resolvesWithHtml = res =>
|
|
53
|
+
HTML_DOCUMENT_TYPES.includes(getContentType(res.headers)) && resolvesWithBody(res)
|
|
54
|
+
|
|
55
|
+
const recordHtmlBody = req => {
|
|
56
|
+
let chunks = []
|
|
57
|
+
req.on('request', () => {
|
|
58
|
+
chunks = []
|
|
59
|
+
})
|
|
60
|
+
req.on('response', res => {
|
|
61
|
+
if (resolvesWithHtml(res)) {
|
|
62
|
+
res.on('data', chunk => chunks.push(chunk))
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
return () => Buffer.concat(chunks)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const isBodyCut = error => error instanceof got.TimeoutError || error instanceof got.ReadError
|
|
69
|
+
|
|
70
|
+
const readCutHtml = async (error, readHtmlBody, toEncode) => {
|
|
71
|
+
if (!isBodyCut(error)) return
|
|
72
|
+
const body = readHtmlBody()
|
|
73
|
+
if (body.length === 0) return
|
|
74
|
+
try {
|
|
75
|
+
return await toEncode(body, error.response.headers['content-type'])
|
|
76
|
+
} catch (encodeError) {
|
|
77
|
+
debug('fetch:partial:error', {
|
|
78
|
+
url: error.response.url,
|
|
79
|
+
message: encodeError?.message || encodeError
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
46
84
|
const fetch = PCancelable.fn(
|
|
47
85
|
async (
|
|
48
86
|
url,
|
|
@@ -100,6 +138,7 @@ const fetch = PCancelable.fn(
|
|
|
100
138
|
|
|
101
139
|
const redirects = []
|
|
102
140
|
req.on('redirect', res => redirects.push({ statusCode: res.statusCode, url: res.url }))
|
|
141
|
+
const readHtmlBody = recordHtmlBody(req)
|
|
103
142
|
|
|
104
143
|
try {
|
|
105
144
|
const res = await req
|
|
@@ -194,6 +233,11 @@ const fetch = PCancelable.fn(
|
|
|
194
233
|
headers: error.response ? error.response.headers : {},
|
|
195
234
|
statusCode: error.response ? error.response.statusCode : undefined
|
|
196
235
|
}
|
|
236
|
+
const cutHtml = await readCutHtml(error, readHtmlBody, toEncode)
|
|
237
|
+
if (cutHtml) {
|
|
238
|
+
debug('fetch:partial', { url, bytes: Buffer.byteLength(cutHtml), reflect })
|
|
239
|
+
return { ...answer, html: cutHtml, partial: true, mode: 'fetch', redirects }
|
|
240
|
+
}
|
|
197
241
|
return reflect
|
|
198
242
|
? { isRejected: true, error, ...answer }
|
|
199
243
|
: { ...answer, html: '', mode: 'fetch', redirects }
|
|
@@ -237,6 +281,8 @@ const prerender = PCancelable.fn(
|
|
|
237
281
|
async (page, response) => {
|
|
238
282
|
if (!response) throw new AbortError('empty response')
|
|
239
283
|
|
|
284
|
+
await markHidden(page)
|
|
285
|
+
|
|
240
286
|
return {
|
|
241
287
|
headers: response.headers(),
|
|
242
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
|
+
}
|