From 793d8605acfea34b27dc5d5d3d76b86d8b92d2a3 Mon Sep 17 00:00:00 2001 From: Ryan Long Date: Mon, 21 Oct 2024 16:29:51 -0400 Subject: [PATCH] feat: :sparkles: add puppeteer options --- .npmignore | 3 ++- package.json | 2 +- src/generateReport.ts | 18 +++++------------- src/index.ts | 7 ++++--- 4 files changed, 12 insertions(+), 18 deletions(-) diff --git a/.npmignore b/.npmignore index 992254b..60da008 100644 --- a/.npmignore +++ b/.npmignore @@ -11,4 +11,5 @@ eslint.config.js LICENSE tsconfig.json bin -output.json \ No newline at end of file +output.json +server \ No newline at end of file diff --git a/package.json b/package.json index 003444c..209ed76 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@helptheweb/scanner", - "version": "2.3.1", + "version": "2.4.0", "description": "service to scan websites and get accessibility violations", "main": "./dist/index.cjs", "module": "./dist/index.js", diff --git a/src/generateReport.ts b/src/generateReport.ts index a571d2c..626e5db 100644 --- a/src/generateReport.ts +++ b/src/generateReport.ts @@ -1,16 +1,11 @@ import { AxePuppeteer } from '@axe-core/puppeteer'; -import puppeteer from 'puppeteer'; - -import locateChrome from 'locate-chrome'; +import puppeteer, { type LaunchOptions } from 'puppeteer'; // Launches Puppeteer and runs the Axe Reporter for a SINGLE URL -export const generateReport = async (url: string): Promise => { - - const executablePath: string = await new Promise(resolve => locateChrome((arg: any) => resolve(arg))) || ''; +export const generateReport = async (url: string, puppeteerLaunchOptions?: LaunchOptions): Promise => { const browser = await puppeteer.launch({ - executablePath, - args: ['--no-sandbox', '--disable-setuid-sandbox'], + ...puppeteerLaunchOptions }); const page = await browser.newPage(); @@ -23,13 +18,10 @@ export const generateReport = async (url: string): Promise => { } // If the supplied URL is an array of URLs, parse through it -export const generateMultipleReports = async (urls: string[]): Promise => { - - const executablePath: string = await new Promise(resolve => locateChrome((arg: any) => resolve(arg))) || ''; +export const generateMultipleReports = async (urls: string[], puppeteerLaunchOptions?: LaunchOptions): Promise => { const browser = await puppeteer.launch({ - executablePath, - args: ['--no-sandbox', '--disable-setuid-sandbox'], + ...puppeteerLaunchOptions }); const page = await browser.newPage(); let combinedResults = []; diff --git a/src/index.ts b/src/index.ts index 7ea66f8..2612006 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,21 +3,22 @@ import { generateReport, generateMultipleReports } from './generateReport'; import { displayResults } from './displayResults'; import { getUrlsFromSitemap } from './getUrlsFromSitemap'; import type { ReportInterface } from './types'; +import { type LaunchOptions } from 'puppeteer'; -export const scanner = async (url: string): Promise => { +export const scanner = async (url: string, puppeteerLaunchOptions?: LaunchOptions): Promise => { let output: ReportInterface[] = []; if (url && URL.canParse(url)) { if (url.includes('.xml')) { const fullSitemap: string[] = await getUrlsFromSitemap(url); - const reports = await generateMultipleReports(fullSitemap); + const reports = await generateMultipleReports(fullSitemap, puppeteerLaunchOptions); for (let report of reports) { const formattedResults: ReportInterface = displayResults(report); output.push(formattedResults); } } else { - const report = await generateReport(url); + const report = await generateReport(url, puppeteerLaunchOptions); const formattedResults: ReportInterface = displayResults(report); output.push(formattedResults); }