UNPKG

1.01 kBJavaScriptView Raw
1const htmlparser = require('htmlparser2')
2/**
3 * I promise you.
4 * @param {[type]} html [description]
5 * @return {[type]} [description]
6 */
7module.exports = (html) => {
8 return new Promise((resolve, reject) => {
9 const scriptSrcs = []
10 let width
11 let height
12 let title
13 let hasParsedTitle = false
14 var parser = new htmlparser.Parser({
15 onopentag: (name, attribs) => {
16 if (name === 'title') {
17 hasParsedTitle = true
18 } else if (name === 'script' && attribs.src) {
19 if (attribs.src.includes('blink-')) {
20 width = +attribs['data-width']
21 height = +attribs['data-height']
22 }
23 scriptSrcs.push(attribs.src)
24 }
25 },
26 ontext: (text) => {
27 if (hasParsedTitle && !title) {
28 title = text
29 }
30 },
31 onerror: (err) => {
32 reject(err)
33 }
34 }, { decodeEntities: true })
35 parser.write(html)
36 parser.end()
37 resolve({ title, scriptSrcs, width, height })
38 })
39}