diff --git a/src/index.js b/src/index.js index a5fc214..ba3df43 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,15 @@ const fromPairs = collection => return acc }, {}) +const collectScripts = (nodes, acc = []) => { + for (const node of nodes) { + if (node.tagName === 'script') acc.push(node) + if (node.children) collectScripts(node.children, acc) + } + return acc +} + module.exports = html => { - const json = parse(html).filter(({ tagName }) => tagName === 'script') - return json.map(({ attributes }) => fromPairs(attributes)) + const scripts = collectScripts(parse(html)) + return scripts.map(({ attributes }) => fromPairs(attributes)) } diff --git a/test/index.js b/test/index.js index 900519f..09a28e9 100644 --- a/test/index.js +++ b/test/index.js @@ -1,9 +1,16 @@ 'use strict' -const test = require('ava') +const test = require('ava').default const parseIframe = require('../') +test('detect scripts nested under html/body', t => { + const scripts = parseIframe( + '
' + ) + t.deepEqual(scripts, [{ src: '/head.js' }, { src: '/body.js' }]) +}) + test('detect scripts from twitter iframe', t => { const html = '\n\n'our new shiny website has landed https://t.co/KIrhYYcTRx pic.twitter.com/cM0se2UoIg
— microlink.io (@microlinkhq) August 23, 2018