|
| 1 | +const puppeteer = require("puppeteer"); |
| 2 | + |
| 3 | +function InvalidUrlError({ url, statusCode, statusText }) { |
| 4 | + this.name = "InvalidUrlError"; |
| 5 | + this.message = `There was an error retrieving CSS from ${url}.\n\tHTTP status code: ${statusCode} (${statusText})`; |
| 6 | +} |
| 7 | + |
| 8 | +InvalidUrlError.prototype = Error.prototype; |
| 9 | + |
| 10 | +module.exports = async ( |
| 11 | + url, |
| 12 | + { debug = false, waitUntil = "networkidle2", customBrowser = {} } = {} |
| 13 | +) => { |
| 14 | + const browserOptions = { |
| 15 | + headless: debug !== true, |
| 16 | + puppeteer |
| 17 | + }; |
| 18 | + |
| 19 | + // Replace the puppeteer instance if a custom one is provided |
| 20 | + // This also means that the executablePath needs to be set to |
| 21 | + // a custom path where some chromium instance is running. |
| 22 | + if ( |
| 23 | + customBrowser && |
| 24 | + customBrowser.executablePath && |
| 25 | + customBrowser.customPuppeteer |
| 26 | + ) { |
| 27 | + browserOptions.executablePath = customBrowser.executablePath; |
| 28 | + browserOptions.puppeteer = customBrowser.customPuppeteer; |
| 29 | + } |
| 30 | + |
| 31 | + // Setup a browser instance |
| 32 | + const browser = await browserOptions.puppeteer.launch(browserOptions); |
| 33 | + |
| 34 | + // Create a new page and navigate to it |
| 35 | + const page = await browser.newPage(); |
| 36 | + |
| 37 | + // Start CSS coverage. This is the meat and bones of this module |
| 38 | + await page.coverage.startCSSCoverage(); |
| 39 | + const response = await page.goto(url, { waitUntil }); |
| 40 | + |
| 41 | + // Make sure that we only try to extract CSS from valid pages. |
| 42 | + // Bail out if the response is an invalid request (400, 500) |
| 43 | + if (response.status() >= 400) { |
| 44 | + await browser.close(); // Don't leave any resources behind |
| 45 | + |
| 46 | + return Promise.reject( |
| 47 | + new InvalidUrlError({ |
| 48 | + url, |
| 49 | + statusCode: response.status(), |
| 50 | + statusText: response.statusText() |
| 51 | + }) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + // Coverage contains a lot of <style> and <link> CSS, |
| 56 | + // but not all... |
| 57 | + const coverage = await page.coverage.stopCSSCoverage(); |
| 58 | + |
| 59 | + // Fetch all <style> tags from the page, because the coverage |
| 60 | + // API may have missed some JS-generated <style> tags. |
| 61 | + // Some of them *were* already caught by the coverage API, |
| 62 | + // but they will be removed later on to prevent duplicates. |
| 63 | + const styleTagsCss = (await page.$$eval("style", styles => { |
| 64 | + // Get the text inside each <style> tag and trim() the |
| 65 | + // results to prevent all the inside-html indentation |
| 66 | + // clogging up the results and making it look |
| 67 | + // bigger than it actually is |
| 68 | + return styles.map(style => style.innerHTML.trim()); |
| 69 | + })).join(""); |
| 70 | + |
| 71 | + await browser.close(); |
| 72 | + |
| 73 | + // Turn the coverage Array into a single string of CSS |
| 74 | + const coverageCss = coverage |
| 75 | + // Filter out the <style> tags that were found in the coverage |
| 76 | + // report since we've conducted our own search for them. |
| 77 | + // A coverage CSS item with the same url as the url of the page |
| 78 | + // we requested is an indication that this was a <style> tag |
| 79 | + .filter(styles => styles.url !== url) |
| 80 | + // The `text` property contains the actual CSS |
| 81 | + .map(({ text }) => text) |
| 82 | + .join(""); |
| 83 | + |
| 84 | + return Promise.resolve(coverageCss + styleTagsCss); |
| 85 | +}; |
0 commit comments