From 86cd9098b18807a8553da05405e2111bc4a4b9ba Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Thu, 25 Nov 2021 20:25:01 +0100 Subject: [PATCH 1/2] adds option to exclude inline styles --- src/index.js | 25 ++++++++++++++----------- test/index.js | 10 ++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 23235de..1e014ef 100644 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ InvalidUrlError.prototype = Error.prototype * @param {string} waitUntil https://github.com/puppeteer/puppeteer/blob/master/docs/api.md#pagegotourl-options * @returns {string} All CSS that was found */ -module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} = {}) => { +module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude', inlineStyles = 'include'} = {}) => { // Setup a browser instance const browser = await puppeteer.launch() @@ -82,15 +82,18 @@ module.exports = async (url, {waitUntil = 'networkidle0', origins = 'exclude'} = // CSSRule: // [x-extract-css-inline-style] { color: red; } // - const inlineCssRules = await page.evaluate(() => { - return [...document.querySelectorAll('[style]')] - .map(element => element.getAttribute('style')) - // Filter out empty style="" attributes - .filter(Boolean) - }) - const inlineCss = inlineCssRules - .map(rule => `[x-extract-css-inline-style] { ${rule} }`) - .map(css => ({type: 'inline', href: url, css})) + let inlineCss = [] + if (inlineStyles !== 'exclude') { + const inlineCssRules = await page.evaluate(() => { + return [...document.querySelectorAll('[style]')] + .map(element => element.getAttribute('style')) + // Filter out empty style="" attributes + .filter(Boolean) + }) + inlineCss = inlineCssRules + .map(rule => `[x-extract-css-inline-style] { ${rule} }`) + .map(css => ({type: 'inline', href: url, css})) + } const links = coverage // Filter out the