1
- //! puppeteer-browser-ready v1.2.1 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
1
+ //! puppeteer-browser-ready v1.2.2 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
2
2
3
+ import { parse } from 'node-html-parser' ;
3
4
import cheerio from 'cheerio' ;
4
5
import express from 'express' ;
5
6
import httpTerminator from 'http-terminator' ;
@@ -42,10 +43,13 @@ const browserReady = {
42
43
return http . terminator . terminate ( ) ;
43
44
} ,
44
45
goto ( url , options ) {
45
- const defaults = { addCheerio : true , verbose : false } ;
46
+ const defaults = { addCheerio : true , parseHtml : true , verbose : false } ;
46
47
const settings = { ...defaults , ...options } ;
48
+ if ( options ?. addCheerio )
49
+ console . log ( 'puppeteer-browser-ready: Option "addCheerio" is deprecated, use "parseHtml" instead.' ) ;
47
50
const log = ( label , msg ) => settings . verbose &&
48
51
console . log ( ' ' , Date . now ( ) % 100000 , label + ':' , msg ) ;
52
+ const rootInfo = ( root ) => root . constructor . name + '/' + root . firstChild . toString ( ) ;
49
53
const web = async ( browser ) => {
50
54
log ( 'Connected' , browser . isConnected ( ) ) ;
51
55
try {
@@ -61,9 +65,10 @@ const browserReady = {
61
65
log ( 'Title' , title ) ;
62
66
const html = response && await response . text ( ) ;
63
67
log ( 'Bytes' , html ?. length ) ;
64
- const $ = html && settings . addCheerio ? cheerio . load ( html ) : null ;
65
- log ( '$' , $ && $ [ 'fn' ] . constructor . name ) ;
66
- return { browser, page, response, status, location, title, html, $ } ;
68
+ const $ = html && settings . addCheerio ? cheerio . load ( html ) : null ; //deprecated
69
+ const root = html && settings . parseHtml ? parse ( html ) : null ;
70
+ log ( 'DOM root' , root ? rootInfo ( root ) : null ) ;
71
+ return { browser, page, response, status, location, title, html, $, root } ;
67
72
}
68
73
catch ( error ) {
69
74
const status = browser . isConnected ( ) ? 'connected' : 'not connected' ;
0 commit comments